summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-10-02 01:11:15 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-10-02 01:11:15 +0700
commitd9e8f37b7f914cce8d0b006351787af0665b864c (patch)
tree23d51e6102b803b3ac2aa69111ec50bfae3fb11e
parentSupport encoding without allocation (diff)
downloadBouncyCastle.NET-ed25519-d9e8f37b7f914cce8d0b006351787af0665b864c.tar.xz
Missing file
-rw-r--r--crypto/src/util/BigIntegers.cs27
1 files changed, 27 insertions, 0 deletions
diff --git a/crypto/src/util/BigIntegers.cs b/crypto/src/util/BigIntegers.cs
index a61824394..1d706beb4 100644
--- a/crypto/src/util/BigIntegers.cs
+++ b/crypto/src/util/BigIntegers.cs
@@ -66,6 +66,9 @@ namespace Org.BouncyCastle.Utilities
          */
         public static void AsUnsignedByteArray(BigInteger value, byte[] buf, int off, int len)
         {
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+            AsUnsignedByteArray(value, buf.AsSpan(off, len));
+#else
             byte[] bytes = value.ToByteArrayUnsigned();
             if (bytes.Length == len)
             {
@@ -82,8 +85,32 @@ namespace Org.BouncyCastle.Utilities
             int padLen = len - count;
             Arrays.Fill(buf, off, off + padLen, 0);
             Array.Copy(bytes, start, buf, off + padLen, count);
+#endif
         }
 
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+        public static void AsUnsignedByteArray(BigInteger value, Span<byte> buf)
+        {
+            int len = buf.Length;
+            byte[] bytes = value.ToByteArrayUnsigned();
+            if (bytes.Length == len)
+            {
+                bytes.CopyTo(buf);
+                return;
+            }
+
+            int start = bytes[0] == 0 ? 1 : 0;
+            int count = bytes.Length - start;
+
+            if (count > len)
+                throw new ArgumentException("standard length exceeded for value");
+
+            int padLen = len - count;
+            buf[..padLen].Fill(0x00);
+            bytes.AsSpan(start, count).CopyTo(buf[padLen..]);
+        }
+#endif
+
         /// <summary>
         /// Creates a Random BigInteger from the secure random of a given bit length.
         /// </summary>