123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
- #pragma warning disable
- using System;
- using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
- using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
- namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Prng
- {
- /**
- * Random generation based on the digest with counter. Calling AddSeedMaterial will
- * always increase the entropy of the hash.
- * <p>
- * Internal access to the digest is synchronized so a single one of these can be shared.
- * </p>
- */
- public sealed class DigestRandomGenerator
- : IRandomGenerator
- {
- private const long CYCLE_COUNT = 10;
- private long stateCounter;
- private long seedCounter;
- private IDigest digest;
- private byte[] state;
- private byte[] seed;
- public DigestRandomGenerator(IDigest digest)
- {
- this.digest = digest;
- this.seed = new byte[digest.GetDigestSize()];
- this.seedCounter = 1;
- this.state = new byte[digest.GetDigestSize()];
- this.stateCounter = 1;
- }
- public void AddSeedMaterial(byte[] inSeed)
- {
- lock (this)
- {
- if (!Arrays.IsNullOrEmpty(inSeed))
- {
- DigestUpdate(inSeed);
- }
- DigestUpdate(seed);
- DigestDoFinal(seed);
- }
- }
- #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
- public void AddSeedMaterial(ReadOnlySpan<byte> inSeed)
- {
- lock (this)
- {
- if (!inSeed.IsEmpty)
- {
- DigestUpdate(inSeed);
- }
- DigestUpdate(seed);
- DigestDoFinal(seed);
- }
- }
- #endif
- public void AddSeedMaterial(long rSeed)
- {
- lock (this)
- {
- DigestAddCounter(rSeed);
- DigestUpdate(seed);
- DigestDoFinal(seed);
- }
- }
- public void NextBytes(byte[] bytes)
- {
- NextBytes(bytes, 0, bytes.Length);
- }
- public void NextBytes(byte[] bytes, int start, int len)
- {
- #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
- NextBytes(bytes.AsSpan(start, len));
- #else
- lock (this)
- {
- int stateOff = 0;
- GenerateState();
- int end = start + len;
- for (int i = start; i < end; ++i)
- {
- if (stateOff == state.Length)
- {
- GenerateState();
- stateOff = 0;
- }
- bytes[i] = state[stateOff++];
- }
- }
- #endif
- }
- #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
- public void NextBytes(Span<byte> bytes)
- {
- lock (this)
- {
- int stateOff = 0;
- GenerateState();
- for (int i = 0; i < bytes.Length; ++i)
- {
- if (stateOff == state.Length)
- {
- GenerateState();
- stateOff = 0;
- }
- bytes[i] = state[stateOff++];
- }
- }
- }
- #endif
- private void CycleSeed()
- {
- DigestUpdate(seed);
- DigestAddCounter(seedCounter++);
- DigestDoFinal(seed);
- }
- private void GenerateState()
- {
- DigestAddCounter(stateCounter++);
- DigestUpdate(state);
- DigestUpdate(seed);
- DigestDoFinal(state);
- if ((stateCounter % CYCLE_COUNT) == 0)
- {
- CycleSeed();
- }
- }
- #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
- private void DigestAddCounter(long seedVal)
- {
- Span<byte> bytes = stackalloc byte[8];
- Pack.UInt64_To_LE((ulong)seedVal, bytes);
- digest.BlockUpdate(bytes);
- }
- private void DigestUpdate(ReadOnlySpan<byte> inSeed)
- {
- digest.BlockUpdate(inSeed);
- }
- private void DigestDoFinal(Span<byte> result)
- {
- digest.DoFinal(result);
- }
- #else
- private void DigestAddCounter(long seedVal)
- {
- byte[] bytes = new byte[8];
- Pack.UInt64_To_LE((ulong)seedVal, bytes);
- digest.BlockUpdate(bytes, 0, bytes.Length);
- }
- private void DigestUpdate(byte[] inSeed)
- {
- digest.BlockUpdate(inSeed, 0, inSeed.Length);
- }
- private void DigestDoFinal(byte[] result)
- {
- digest.DoFinal(result, 0);
- }
- #endif
- }
- }
- #pragma warning restore
- #endif
|