From d9e8f37b7f914cce8d0b006351787af0665b864c Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Sun, 2 Oct 2022 01:11:15 +0700 Subject: Missing file --- crypto/src/util/BigIntegers.cs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'crypto/src') 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 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 + /// /// Creates a Random BigInteger from the secure random of a given bit length. /// -- cgit 1.5.1