< Summary

Information
Class: Renci.SshNet.Security.Org.BouncyCastle.Security.SecureRandom
Assembly: Renci.SshNet
File(s): \home\appveyor\projects\ssh-net\src\Renci.SshNet\Security\BouncyCastle\security\SecureRandom.cs
Line coverage
32%
Covered lines: 31
Uncovered lines: 65
Coverable lines: 96
Total lines: 179
Line coverage: 32.2%
Branch coverage
10%
Covered branches: 2
Total branches: 20
Branch coverage: 10%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.cctor()100%1100%
NextCounterValue()100%1100%
get_Master()100%1100%
CreatePrng(...)100%2100%
GetNextBytes(...)100%1100%
.ctor()100%1100%
.ctor(...)100%1100%
GenerateSeed(...)100%10%
SetSeed(...)100%10%
SetSeed(...)100%10%
Next()100%10%
Next(...)0%80%
Next(...)0%100%
NextBytes(...)100%1100%
NextBytes(...)100%10%
NextDouble()100%10%
NextInt()100%10%
NextLong()100%10%

File(s)

\home\appveyor\projects\ssh-net\src\Renci.SshNet\Security\BouncyCastle\security\SecureRandom.cs

#LineLine coverage
 1using System;
 2using System.Threading;
 3
 4using Renci.SshNet.Security.Org.BouncyCastle.Crypto;
 5using Renci.SshNet.Security.Org.BouncyCastle.Crypto.Digests;
 6using Renci.SshNet.Security.Org.BouncyCastle.Crypto.Prng;
 7using Renci.SshNet.Security.Org.BouncyCastle.Utilities;
 8
 9namespace Renci.SshNet.Security.Org.BouncyCastle.Security
 10{
 11    internal class SecureRandom
 12        : Random
 13    {
 114        private static long counter = Times.NanoTime();
 15
 16       private static long NextCounterValue()
 1017        {
 1018            return Interlocked.Increment(ref counter);
 1019        }
 20
 121        private static readonly SecureRandom master = new SecureRandom(new CryptoApiRandomGenerator());
 22        private static SecureRandom Master
 23        {
 3024            get { return master; }
 25        }
 26
 27        private static DigestRandomGenerator CreatePrng(IDigest digest, bool autoSeed)
 1028        {
 1029            DigestRandomGenerator prng = new DigestRandomGenerator(digest);
 1030            if (autoSeed)
 1031            {
 1032                prng.AddSeedMaterial(NextCounterValue());
 1033                prng.AddSeedMaterial(GetNextBytes(Master, digest.GetDigestSize()));
 1034            }
 1035            return prng;
 1036        }
 37
 38        public static byte[] GetNextBytes(SecureRandom secureRandom, int length)
 1039        {
 1040            byte[] result = new byte[length];
 1041            secureRandom.NextBytes(result);
 1042            return result;
 1043        }
 44
 45        protected readonly IRandomGenerator generator;
 46
 47        public SecureRandom()
 1048            : this(CreatePrng(new Sha256Digest(), true))
 1049        {
 1050        }
 51
 52        /// <summary>Use the specified instance of IRandomGenerator as random source.</summary>
 53        /// <remarks>
 54        /// This constructor performs no seeding of either the <c>IRandomGenerator</c> or the
 55        /// constructed <c>SecureRandom</c>. It is the responsibility of the client to provide
 56        /// proper seed material as necessary/appropriate for the given <c>IRandomGenerator</c>
 57        /// implementation.
 58        /// </remarks>
 59        /// <param name="generator">The source to generate all random bytes from.</param>
 60        public SecureRandom(IRandomGenerator generator)
 61#pragma warning disable CA5394 // Do not use insecure randomness
 1162            : base(0)
 63#pragma warning restore CA5394 // Do not use insecure randomness
 1164        {
 1165            this.generator = generator;
 1166        }
 67
 68        public virtual byte[] GenerateSeed(int length)
 069        {
 070            return GetNextBytes(Master, length);
 071        }
 72
 73        public virtual void SetSeed(byte[] seed)
 074        {
 075            generator.AddSeedMaterial(seed);
 076        }
 77
 78        public virtual void SetSeed(long seed)
 079        {
 080            generator.AddSeedMaterial(seed);
 081        }
 82
 83        public override int Next()
 084        {
 085            return NextInt() & int.MaxValue;
 086        }
 87
 88        public override int Next(int maxValue)
 089        {
 90
 091            if (maxValue < 2)
 092            {
 093                if (maxValue < 0)
 094                    throw new ArgumentOutOfRangeException("maxValue", "cannot be negative");
 95
 096                return 0;
 97            }
 98
 99            int bits;
 100
 101            // Test whether maxValue is a power of 2
 0102            if ((maxValue & (maxValue - 1)) == 0)
 0103            {
 0104                bits = NextInt() & int.MaxValue;
 0105                return (int)(((long)bits * maxValue) >> 31);
 106            }
 107
 108            int result;
 109            do
 0110            {
 0111                bits = NextInt() & int.MaxValue;
 0112                result = bits % maxValue;
 0113            }
 0114            while (bits - result + (maxValue - 1) < 0); // Ignore results near overflow
 115
 0116            return result;
 0117        }
 118
 119        public override int Next(int minValue, int maxValue)
 0120        {
 0121            if (maxValue <= minValue)
 0122            {
 0123                if (maxValue == minValue)
 0124                    return minValue;
 125
 0126                throw new ArgumentException("maxValue cannot be less than minValue");
 127            }
 128
 0129            int diff = maxValue - minValue;
 0130            if (diff > 0)
 0131                return minValue + Next(diff);
 132
 133            for (;;)
 0134            {
 0135                int i = NextInt();
 136
 0137                if (i >= minValue && i < maxValue)
 0138                    return i;
 0139            }
 0140        }
 141
 142        public override void NextBytes(byte[] buf)
 19143        {
 19144            generator.NextBytes(buf);
 19145        }
 146
 147        public virtual void NextBytes(byte[] buf, int off, int len)
 0148        {
 0149            generator.NextBytes(buf, off, len);
 0150        }
 151
 1152        private static readonly double DoubleScale = System.Math.Pow(2.0, 64.0);
 153
 154        public override double NextDouble()
 0155        {
 0156            return Convert.ToDouble((ulong) NextLong()) / DoubleScale;
 0157        }
 158
 159        public virtual int NextInt()
 0160        {
 0161            byte[] bytes = new byte[4];
 0162            NextBytes(bytes);
 163
 0164            uint result = bytes[0];
 0165            result <<= 8;
 0166            result |= bytes[1];
 0167            result <<= 8;
 0168            result |= bytes[2];
 0169            result <<= 8;
 0170            result |= bytes[3];
 0171            return (int)result;
 0172        }
 173
 174        public virtual long NextLong()
 0175        {
 0176            return ((long)(uint) NextInt() << 32) | (long)(uint) NextInt();
 0177        }
 178    }
 179}