summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--crypto/src/security/DotNetUtilities.cs14
-rw-r--r--crypto/src/util/BigIntegers.cs11
2 files changed, 8 insertions, 17 deletions
diff --git a/crypto/src/security/DotNetUtilities.cs b/crypto/src/security/DotNetUtilities.cs
index df9d327de..f0064fb3d 100644
--- a/crypto/src/security/DotNetUtilities.cs
+++ b/crypto/src/security/DotNetUtilities.cs
@@ -9,6 +9,7 @@ using Org.BouncyCastle.Asn1.X509;
 using Org.BouncyCastle.Crypto;
 using Org.BouncyCastle.Crypto.Parameters;
 using Org.BouncyCastle.Math;
+using Org.BouncyCastle.Utilities;
 using Org.BouncyCastle.X509;
 
 namespace Org.BouncyCastle.Security
@@ -223,20 +224,9 @@ namespace Org.BouncyCastle.Security
             return rp;
         }
 
-        // TODO Move functionality to more general class
         private static byte[] ConvertRSAParametersField(BigInteger n, int size)
         {
-            byte[] bs = n.ToByteArrayUnsigned();
-
-            if (bs.Length == size)
-                return bs;
-
-            if (bs.Length > size)
-                throw new ArgumentException("Specified size too small", "size");
-
-            byte[] padded = new byte[size];
-            Array.Copy(bs, 0, padded, size - bs.Length, bs.Length);
-            return padded;
+            return BigIntegers.AsUnsignedByteArray(size, n);
         }
 
         private static RSA CreateRSAProvider(RSAParameters rp)
diff --git a/crypto/src/util/BigIntegers.cs b/crypto/src/util/BigIntegers.cs
index d9c898676..a61824394 100644
--- a/crypto/src/util/BigIntegers.cs
+++ b/crypto/src/util/BigIntegers.cs
@@ -38,15 +38,16 @@ namespace Org.BouncyCastle.Utilities
         public static byte[] AsUnsignedByteArray(int length, BigInteger n)
         {
             byte[] bytes = n.ToByteArrayUnsigned();
+            int bytesLength = bytes.Length;
 
-            if (bytes.Length > length)
-                throw new ArgumentException("standard length exceeded", "n");
-
-            if (bytes.Length == length)
+            if (bytesLength == length)
                 return bytes;
 
+            if (bytesLength > length)
+                throw new ArgumentException("standard length exceeded", "n");
+
             byte[] tmp = new byte[length];
-            Array.Copy(bytes, 0, tmp, tmp.Length - bytes.Length, bytes.Length);
+            Array.Copy(bytes, 0, tmp, length - bytesLength, bytesLength);
             return tmp;
         }