From da32e5f1e8d71b8bc1d573eb5ab41a6da26f49b3 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Mon, 9 Jan 2023 14:04:06 +0700 Subject: Add FromUnsignedByteArray methods --- crypto/src/util/BigIntegers.cs | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/crypto/src/util/BigIntegers.cs b/crypto/src/util/BigIntegers.cs index 668d92246..9c5477e6a 100644 --- a/crypto/src/util/BigIntegers.cs +++ b/crypto/src/util/BigIntegers.cs @@ -139,11 +139,7 @@ namespace Org.BouncyCastle.Utilities * @param random the source of randomness * @return a random BigInteger value in the range [min,max] */ - public static BigInteger CreateRandomInRange( - BigInteger min, - BigInteger max, - // TODO Should have been just Random class - SecureRandom random) + public static BigInteger CreateRandomInRange(BigInteger min, BigInteger max, SecureRandom random) { int cmp = min.CompareTo(max); if (cmp >= 0) @@ -155,23 +151,39 @@ namespace Org.BouncyCastle.Utilities } if (min.BitLength > max.BitLength / 2) - { return CreateRandomInRange(BigInteger.Zero, max.Subtract(min), random).Add(min); - } for (int i = 0; i < MaxIterations; ++i) { BigInteger x = new BigInteger(max.BitLength, random); if (x.CompareTo(min) >= 0 && x.CompareTo(max) <= 0) - { return x; - } } // fall back to a faster (restricted) method return new BigInteger(max.Subtract(min).BitLength - 1, random).Add(min); } + public static BigInteger FromUnsignedByteArray(byte[] buf) + { + return new BigInteger(1, buf); + } + + public static BigInteger FromUnsignedByteArray(byte[] buf, int off, int length) + { + return new BigInteger(1, buf, off, length); + } + + public static int GetByteLength(BigInteger n) + { + return n.GetLengthofByteArray(); + } + + public static int GetUnsignedByteLength(BigInteger n) + { + return n.GetLengthofByteArrayUnsigned(); + } + public static BigInteger ModOddInverse(BigInteger M, BigInteger X) { if (!M.TestBit(0)) @@ -253,15 +265,5 @@ namespace Org.BouncyCastle.Utilities return Nat.ToBigInteger(len, z); } } - - public static int GetByteLength(BigInteger n) - { - return n.GetLengthofByteArray(); - } - - public static int GetUnsignedByteLength(BigInteger n) - { - return n.GetLengthofByteArrayUnsigned(); - } } } -- cgit 1.4.1