This commit is contained in:
2020-07-09 08:50:24 +08:00
parent 13d25f4707
commit c523462b82
1818 changed files with 174940 additions and 582 deletions

View File

@@ -0,0 +1,94 @@
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
#if !(NETCF_1_0 || PORTABLE)
using System;
#if NETFX_CORE
using Windows.Security.Cryptography;
#else
using System.Security.Cryptography;
#endif
namespace Org.BouncyCastle.Crypto.Prng
{
/// <summary>
/// Uses Microsoft's RNGCryptoServiceProvider
/// </summary>
public class CryptoApiRandomGenerator
: IRandomGenerator
{
#if !NETFX_CORE
private readonly RandomNumberGenerator rndProv;
#endif
public CryptoApiRandomGenerator()
#if !NETFX_CORE
: this(new RNGCryptoServiceProvider())
#endif
{
}
#if !NETFX_CORE
public CryptoApiRandomGenerator(RandomNumberGenerator rng)
{
this.rndProv = rng;
}
#endif
#region IRandomGenerator Members
public virtual void AddSeedMaterial(byte[] seed)
{
// We don't care about the seed
}
public virtual void AddSeedMaterial(long seed)
{
// We don't care about the seed
}
public virtual void NextBytes(byte[] bytes)
{
#if NETFX_CORE
var buffer = CryptographicBuffer.GenerateRandom((uint)bytes.Length);
byte[] finalBytes = null;
CryptographicBuffer.CopyToByteArray(buffer, out finalBytes);
finalBytes.CopyTo(bytes, 0);
#else
rndProv.GetBytes(bytes);
#endif
}
public virtual void NextBytes(byte[] bytes, int start, int len)
{
if (start < 0)
throw new ArgumentException("Start offset cannot be negative", "start");
if (bytes.Length < (start + len))
throw new ArgumentException("Byte array too small for requested offset and length");
if (bytes.Length == len && start == 0)
{
NextBytes(bytes);
}
else
{
#if NETFX_CORE
byte[] tmpBuf = null;
var buffer = CryptographicBuffer.GenerateRandom((uint)bytes.Length);
CryptographicBuffer.CopyToByteArray(buffer, out tmpBuf);
#else
byte[] tmpBuf = new byte[len];
NextBytes(tmpBuf);
#endif
Array.Copy(tmpBuf, 0, bytes, start, len);
}
}
#endregion
}
}
#endif
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a85dc7fc2dfec404b9369e35c37b344a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,131 @@
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
using System;
using Org.BouncyCastle.Crypto.Digests;
using Org.BouncyCastle.Crypto.Utilities;
namespace 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 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)
{
DigestUpdate(inSeed);
DigestUpdate(seed);
DigestDoFinal(seed);
}
}
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)
{
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++];
}
}
}
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();
}
}
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

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b19bcf854b6ad4170aea68f9ed2d954f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,30 @@
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
using System;
namespace Org.BouncyCastle.Crypto.Prng
{
/// <remarks>Generic interface for objects generating random bytes.</remarks>
public interface IRandomGenerator
{
/// <summary>Add more seed material to the generator.</summary>
/// <param name="seed">A byte array to be mixed into the generator's state.</param>
void AddSeedMaterial(byte[] seed);
/// <summary>Add more seed material to the generator.</summary>
/// <param name="seed">A long value to be mixed into the generator's state.</param>
void AddSeedMaterial(long seed);
/// <summary>Fill byte array with random values.</summary>
/// <param name="bytes">Array to be filled.</param>
void NextBytes(byte[] bytes);
/// <summary>Fill byte array with random values.</summary>
/// <param name="bytes">Array to receive bytes.</param>
/// <param name="start">Index to start filling at.</param>
/// <param name="len">Length of segment to fill.</param>
void NextBytes(byte[] bytes, int start, int len);
}
}
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 24336df0a122a42fcb50b10fced6ad7f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: