Exemplo n.º 1
0
        public void IncorrectSeedClass()
        {
            AdvancedSecureRandom secureRandom  = new AdvancedSecureRandom(new SeedClass(null));
            AdvancedSecureRandom secureRandom2 = new AdvancedSecureRandom(new SeedClass(new byte[] { 1, 5, 4 }));

            Assert.AreNotEqual(secureRandom.Next(), secureRandom2.Next());
        }
Exemplo n.º 2
0
        public void CorrectSeedClass()
        {
            AdvancedSecureRandom secureRandom  = new AdvancedSecureRandom(new SeedClass(null));
            AdvancedSecureRandom secureRandom2 = new AdvancedSecureRandom(new SeedClass(null));

            Assert.AreEqual(secureRandom.Next(), secureRandom2.Next());
        }
Exemplo n.º 3
0
        public void DigestBitSizeDifferencesTest()
        {
            AdvancedSecureRandom sha3_512_random = new AdvancedSecureRandom(new Sha3Digest(512));
            AdvancedSecureRandom sha3_256_random = new AdvancedSecureRandom(new Sha3Digest(256));

            Assert.AreNotEqual(sha3_512_random.Next(), sha3_256_random.Next());
        }
        public void DigestIntGeneration()
        {
            AdvancedSecureRandom secureRandom  = new AdvancedSecureRandom(new Sha1Digest());
            AdvancedSecureRandom secureRandom2 = new AdvancedSecureRandom(new Sha1Digest());

            Assert.AreNotEqual(secureRandom.Next(), secureRandom2.Next());
            Assert.AreNotEqual(secureRandom.Next(100), secureRandom2.Next(100));
            Assert.AreNotEqual(secureRandom.Next(150, 397), secureRandom2.Next(150, 397));
        }
        public void CorrectDigestSeededIntGeneration()
        {
            AdvancedSecureRandom secureRandom  = new AdvancedSecureRandom(new Blake2bDigest(), "test seed", 312);
            AdvancedSecureRandom secureRandom2 = new AdvancedSecureRandom(new Blake2bDigest(), "test seed", 312);

            Assert.AreEqual(secureRandom.Next(), secureRandom2.Next());
            Assert.AreEqual(secureRandom.Next(100), secureRandom2.Next(100));
            Assert.AreEqual(secureRandom.Next(150, 397), secureRandom2.Next(150, 397));
        }
        public void IncorrectDigestSeededIntGeneration()
        {
            AdvancedSecureRandom secureRandom  = new AdvancedSecureRandom(new Sha3Digest(), "test seed", 312);
            AdvancedSecureRandom secureRandom2 = new AdvancedSecureRandom(new WhirlpoolDigest(), "test seed", 312);

            Assert.AreNotEqual(secureRandom.Next(), secureRandom2.Next());
            Assert.AreNotEqual(secureRandom.Next(100), secureRandom2.Next(100));
            Assert.AreNotEqual(secureRandom.Next(150, 397), secureRandom2.Next(150, 397));
        }
        public void IncorrectSeededByteGeneration()
        {
            AdvancedSecureRandom secureRandom  = new AdvancedSecureRandom(423);
            AdvancedSecureRandom secureRandom2 = new AdvancedSecureRandom("test seed", 312, 2.8372);

            Assert.AreNotEqual(secureRandom.Next(), secureRandom2.Next());
            Assert.AreNotEqual(secureRandom.Next(100), secureRandom2.Next(100));
            Assert.AreNotEqual(secureRandom.Next(150, 397), secureRandom2.Next(150, 397));
        }
Exemplo n.º 8
0
 /// <summary>
 /// Initializes the <see cref="CrossPlatformEncryptor"/> given the <see cref="AdvancedSecureRandom"/> instance to use for our encryption.
 /// </summary>
 /// <param name="secureRandom"> The <see cref="AdvancedSecureRandom"/> instance to use for our encryption. </param>
 protected CrossPlatformEncryptor(AdvancedSecureRandom secureRandom) : this(
         secureRandom.NextBytes(2),
         secureRandom.NextBytes(4),
         secureRandom.NextBytes(8),
         secureRandom.NextBytes(16),
         secureRandom.NextBytes(32),
         secureRandom.NextBytes(64),
         secureRandom.NextBytes(128),
         secureRandom.NextBytes(256))
 {
 }
        public void CorrectSeededByteGeneration()
        {
            AdvancedSecureRandom secureRandom  = new AdvancedSecureRandom("test seed", 312);
            AdvancedSecureRandom secureRandom2 = new AdvancedSecureRandom("test seed", 312);

            const int size = 32;

            byte[] randomBytes  = secureRandom.NextBytes(size);
            byte[] randomBytes2 = secureRandom2.NextBytes(size);

            for (int i = 0; i < size; i++)
            {
                Assert.AreEqual(randomBytes[i], randomBytes2[i]);
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Gets the next random integer given the <see cref="IDigest"/> hash function to use.
        /// </summary>
        /// <param name="seed"> The <see langword="byte"/>[] seed to use to produce the random integer. </param>
        /// <param name="minValue"> The inclusive minimum value of the random integer. </param>
        /// <param name="maxValue"> The exclusive maximum value of the random integer. </param>
        /// <param name="digest"> The <see cref="IDigest"/> to use to derive our random integer. </param>
        /// <returns> The randomly generated integer. </returns>
        private static int InternalGetInt(byte[] seed, int?minValue, int?maxValue, IDigest digest)
        {
            AdvancedSecureRandom secureRandom = (seed == null ? new AdvancedSecureRandom(digest) : new AdvancedSecureRandom(digest, seed));

            if (minValue.HasValue && maxValue.HasValue)
            {
                return(secureRandom.Next(minValue.Value, maxValue.Value));
            }
            else if (maxValue.HasValue)
            {
                return(secureRandom.Next(maxValue.Value));
            }
            else
            {
                return(secureRandom.Next());
            }
        }
Exemplo n.º 11
0
        /// <summary>
        /// Gets the next random double given the <see cref="IDigest"/> hash function to use.
        /// </summary>
        /// <param name="seed"> The <see langword="byte"/>[] seed to use to produce the random double. </param>
        /// <param name="minValue"> The inclusive minimum value of the random double. </param>
        /// <param name="maxValue"> The exclusive maximum value of the random double. </param>
        /// <param name="digest"> The <see cref="IDigest"/> to use to derive our random double. </param>
        /// <returns> The randomly generated double. </returns>
        private static double InternalGetDouble(byte[] seed, double?minValue, double?maxValue, IDigest digest)
        {
            AdvancedSecureRandom secureRandom = (seed == null ? new AdvancedSecureRandom(digest) : new AdvancedSecureRandom(digest, seed));

            if (minValue.HasValue && maxValue.HasValue)
            {
                return(secureRandom.Next((int)minValue.Value, (int)maxValue.Value - 1) + secureRandom.NextDouble());
            }
            else if (maxValue.HasValue)
            {
                return(secureRandom.Next((int)maxValue.Value - 1) + secureRandom.NextDouble());
            }
            else
            {
                return(secureRandom.NextDouble());
            }
        }
 /// <summary>
 /// Gets the salted password hash of a password.
 /// </summary>
 /// <param name="password"> The password to get the salted hash for. </param>
 /// <param name="iterations"> The number of iterations to apply to the encryption. </param>
 /// <param name="saltSize"> The size of the salt. </param>
 /// <param name="hashSize"> The size of the hash. </param>
 /// <returns> The salted hash as a <see langword="byte"/>[]. </returns>
 private byte[] InternalGetSaltedPasswordHash(byte[] password, int iterations, int saltSize, int hashSize)
 {
     byte[] salt = new AdvancedSecureRandom(engine.PBKDF2Digest).NextBytes(saltSize <= MIN_SALT_SIZE ? MIN_SALT_SIZE : saltSize);
     return(salt.Concat(GeneratePasswordHash(password, salt, iterations, hashSize)).ToArray());
 }
Exemplo n.º 13
0
 /// <summary>
 /// Initializes the <see cref="DataEncryptor"/> given the <see cref="AdvancedSecureRandom"/> instance to use for our encryption.
 /// </summary>
 /// <param name="secureRandom"> The <see cref="AdvancedSecureRandom"/> instance to use for our encryption. </param>
 public DataEncryptor(AdvancedSecureRandom secureRandom) : base(secureRandom)
 {
 }
Exemplo n.º 14
0
 public SecretEncryptor(AdvancedSecureRandom secureRandom) : base(secureRandom)
 {
 }