| | | 1 | | using System; |
| | | 2 | | using System.Security.Cryptography; |
| | | 3 | | |
| | | 4 | | namespace Renci.SshNet.Security.Org.BouncyCastle.Crypto.Prng |
| | | 5 | | { |
| | | 6 | | internal class CryptoApiRandomGenerator |
| | | 7 | | : IRandomGenerator |
| | | 8 | | { |
| | | 9 | | private readonly RandomNumberGenerator rndProv; |
| | | 10 | | |
| | | 11 | | public CryptoApiRandomGenerator() |
| | 1 | 12 | | : this(Abstractions.CryptoAbstraction.CreateRandomNumberGenerator()) |
| | 1 | 13 | | { |
| | 1 | 14 | | } |
| | | 15 | | |
| | 1 | 16 | | public CryptoApiRandomGenerator(RandomNumberGenerator rng) |
| | 1 | 17 | | { |
| | 1 | 18 | | this.rndProv = rng; |
| | 1 | 19 | | } |
| | | 20 | | |
| | | 21 | | #region IRandomGenerator Members |
| | | 22 | | |
| | | 23 | | public virtual void AddSeedMaterial(byte[] seed) |
| | 0 | 24 | | { |
| | | 25 | | // We don't care about the seed |
| | 0 | 26 | | } |
| | | 27 | | |
| | | 28 | | public virtual void AddSeedMaterial(long seed) |
| | 0 | 29 | | { |
| | | 30 | | // We don't care about the seed |
| | 0 | 31 | | } |
| | | 32 | | |
| | | 33 | | public virtual void NextBytes(byte[] bytes) |
| | 10 | 34 | | { |
| | 10 | 35 | | rndProv.GetBytes(bytes); |
| | 10 | 36 | | } |
| | | 37 | | |
| | | 38 | | public virtual void NextBytes(byte[] bytes, int start, int len) |
| | 0 | 39 | | { |
| | 0 | 40 | | if (start < 0) |
| | 0 | 41 | | throw new ArgumentException("Start offset cannot be negative", "start"); |
| | 0 | 42 | | if (bytes.Length < (start + len)) |
| | 0 | 43 | | throw new ArgumentException("Byte array too small for requested offset and length"); |
| | | 44 | | |
| | 0 | 45 | | if (bytes.Length == len && start == 0) |
| | 0 | 46 | | { |
| | 0 | 47 | | NextBytes(bytes); |
| | 0 | 48 | | } |
| | | 49 | | else |
| | 0 | 50 | | { |
| | 0 | 51 | | byte[] tmpBuf = new byte[len]; |
| | 0 | 52 | | NextBytes(tmpBuf); |
| | 0 | 53 | | Array.Copy(tmpBuf, 0, bytes, start, len); |
| | 0 | 54 | | } |
| | 0 | 55 | | } |
| | | 56 | | |
| | | 57 | | #endregion |
| | | 58 | | } |
| | | 59 | | } |