summary refs log tree commit diff
path: root/crypto/src/security/SecureRandom.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--crypto/src/security/SecureRandom.cs36
1 files changed, 24 insertions, 12 deletions
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<byte> 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<byte> seed = seedLength <= 128
+                ? stackalloc byte[seedLength]
+                : new byte[seedLength];
+#else
+                byte[] seed = new byte[seedLength];
+#endif
+            Master.NextBytes(seed);
+            generator.AddSeedMaterial(seed);
+        }
     }
 }