diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2020-07-30 15:33:18 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2020-07-30 15:33:18 +0700 |
commit | 5e0b449eae68f6adf1f988f9601a299f3aa6c468 (patch) | |
tree | cc622c2de05cd5bd27f1044b6bb614c520d2cd7e /crypto/src/util | |
parent | EdDSA updates (diff) | |
download | BouncyCastle.NET-ed25519-5e0b449eae68f6adf1f988f9601a299f3aa6c468.tar.xz |
Misc. updates from bc-java
Diffstat (limited to 'crypto/src/util')
-rw-r--r-- | crypto/src/util/BigIntegers.cs | 45 | ||||
-rw-r--r-- | crypto/src/util/Integers.cs | 14 |
2 files changed, 53 insertions, 6 deletions
diff --git a/crypto/src/util/BigIntegers.cs b/crypto/src/util/BigIntegers.cs index 6674750bb..bac5f12c0 100644 --- a/crypto/src/util/BigIntegers.cs +++ b/crypto/src/util/BigIntegers.cs @@ -15,7 +15,7 @@ namespace Org.BouncyCastle.Utilities /** * Return the passed in value as an unsigned byte array. * - * @param value value to be converted. + * @param value the value to be converted. * @return a byte array without a leading zero byte if present in the signed encoding. */ public static byte[] AsUnsignedByteArray( @@ -25,11 +25,11 @@ namespace Org.BouncyCastle.Utilities } /** - * Return the passed in value as an unsigned byte array of specified length, zero-extended as necessary. - * - * @param length desired length of result array. - * @param n value to be converted. - * @return a byte array of specified length, with leading zeroes as necessary given the size of n. + * Return the passed in value as an unsigned byte array of the specified length, padded with + * leading zeros as necessary. + * @param length the fixed length of the result. + * @param n the value to be converted. + * @return a byte array padded to a fixed length with leading zeros. */ public static byte[] AsUnsignedByteArray(int length, BigInteger n) { @@ -46,6 +46,39 @@ namespace Org.BouncyCastle.Utilities return tmp; } + /** + * Write the passed in value as unsigned bytes to the specified buffer range, padded with + * leading zeros as necessary. + * + * @param value + * the value to be converted. + * @param buf + * the buffer to which the value is written. + * @param off + * the start offset in array <code>buf</code> at which the data is written. + * @param len + * the fixed length of data written (possibly padded with leading zeros). + */ + public static void AsUnsignedByteArray(BigInteger value, byte[] buf, int off, int len) + { + byte[] bytes = value.ToByteArrayUnsigned(); + if (bytes.Length == len) + { + Array.Copy(bytes, 0, buf, off, len); + 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; + Arrays.Fill(buf, off, off + padLen, 0); + Array.Copy(bytes, start, buf, off + padLen, count); + } + /// <summary> /// Creates a Random BigInteger from the secure random of a given bit length. /// </summary> diff --git a/crypto/src/util/Integers.cs b/crypto/src/util/Integers.cs index bd05a053e..afb4b827f 100644 --- a/crypto/src/util/Integers.cs +++ b/crypto/src/util/Integers.cs @@ -19,6 +19,20 @@ namespace Org.BouncyCastle.Utilities return n; } + public static int NumberOfTrailingZeros(int i) + { + if (i == 0) + return 32; + + int count = 0; + while ((i & 1) == 0) + { + i >>= 1; + ++count; + } + return count; + } + public static int RotateLeft(int i, int distance) { return (i << distance) ^ (int)((uint)i >> -distance); |