diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2015-11-16 21:30:04 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2015-11-16 21:30:04 +0700 |
commit | 8bda1de4207632587d2df5fbd08797e22d5f8463 (patch) | |
tree | 206f6e7b0427cd96e7203d580ac08ffb4557180b /crypto/src/security | |
parent | Remove redundant semicolons (diff) | |
download | BouncyCastle.NET-ed25519-8bda1de4207632587d2df5fbd08797e22d5f8463.tar.xz |
Finish port of latest PRNG/DRBG stuff from Java
Diffstat (limited to 'crypto/src/security')
-rw-r--r-- | crypto/src/security/SecureRandom.cs | 35 |
1 files changed, 14 insertions, 21 deletions
diff --git a/crypto/src/security/SecureRandom.cs b/crypto/src/security/SecureRandom.cs index 4894a93e6..875709e57 100644 --- a/crypto/src/security/SecureRandom.cs +++ b/crypto/src/security/SecureRandom.cs @@ -164,13 +164,7 @@ namespace Org.BouncyCastle.Security public override int Next() { - for (;;) - { - int i = NextInt() & int.MaxValue; - - if (i != int.MaxValue) - return i; - } + return NextInt() & int.MaxValue; } public override int Next(int maxValue) @@ -184,11 +178,9 @@ namespace Org.BouncyCastle.Security } // Test whether maxValue is a power of 2 - if ((maxValue & -maxValue) == maxValue) + if ((maxValue & (maxValue - 1)) == 0) { - int val = NextInt() & int.MaxValue; - long lr = ((long) maxValue * (long) val) >> 31; - return (int) lr; + return NextInt() & (maxValue - 1); } int bits, result; @@ -244,16 +236,17 @@ namespace Org.BouncyCastle.Security public virtual int NextInt() { - byte[] intBytes = new byte[4]; - NextBytes(intBytes); - - int result = 0; - for (int i = 0; i < 4; i++) - { - result = (result << 8) + (intBytes[i] & 0xff); - } - - return result; + byte[] bytes = new byte[4]; + NextBytes(bytes); + + uint result = bytes[0]; + result <<= 8; + result |= bytes[1]; + result <<= 8; + result |= bytes[2]; + result <<= 8; + result |= bytes[3]; + return (int)result; } public virtual long NextLong() |