From 584ada5e755ef20e9b2824f11b034afaf32b3fda Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Sun, 23 Oct 2022 16:01:11 +0700 Subject: Change BigInteger arbitrary random source --- crypto/src/security/SecureRandom.cs | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) (limited to 'crypto/src/security') diff --git a/crypto/src/security/SecureRandom.cs b/crypto/src/security/SecureRandom.cs index 10b837b03..53e5be069 100644 --- a/crypto/src/security/SecureRandom.cs +++ b/crypto/src/security/SecureRandom.cs @@ -27,18 +27,7 @@ namespace Org.BouncyCastle.Security DigestRandomGenerator prng = new DigestRandomGenerator(digest); if (autoSeed) { - prng.AddSeedMaterial(NextCounterValue()); - - int seedLength = digest.GetDigestSize(); -#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER - Span seed = seedLength <= 128 - ? stackalloc byte[seedLength] - : new byte[seedLength]; -#else - byte[] seed = new byte[seedLength]; -#endif - Master.NextBytes(seed); - prng.AddSeedMaterial(seed); + AutoSeed(prng, digest.GetDigestSize()); } return prng; } @@ -103,6 +92,14 @@ namespace Org.BouncyCastle.Security this.generator = generator; } + public SecureRandom(IRandomGenerator generator, int autoSeedLengthInBytes) + : base(0) + { + AutoSeed(generator, autoSeedLengthInBytes); + + this.generator = generator; + } + public virtual byte[] GenerateSeed(int length) { return GetNextBytes(Master, length); @@ -246,5 +243,20 @@ namespace Org.BouncyCastle.Security NextBytes(bytes); return (long)Pack.BE_To_UInt64(bytes); } + + private static void AutoSeed(IRandomGenerator generator, int seedLength) + { + generator.AddSeedMaterial(NextCounterValue()); + +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + Span seed = seedLength <= 128 + ? stackalloc byte[seedLength] + : new byte[seedLength]; +#else + byte[] seed = new byte[seedLength]; +#endif + Master.NextBytes(seed); + generator.AddSeedMaterial(seed); + } } } -- cgit 1.4.1