예제 #1
0
        public NerdyRfc2898DeriveBytes(string password, int saltSize, int iterations, HashAlgorithmName hashAlgorithm)
        {
            switch (saltSize)
            {
            case < 0:
                throw new ArgumentOutOfRangeException(nameof(saltSize), $"saleSize must a positive number");

            case < MinimumSaltSize:
                throw new ArgumentException($"salt minimum length is {MinimumSaltSize}", nameof(saltSize));
            }

            if (iterations <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(iterations), "iterations must be a positive number");
            }

            this.salt = new byte[saltSize + sizeof(uint)];
            using (var rng = new NerdyRandomNumberGenerator())
            {
                rng.NextBytes(this.salt);
            }

            this.iterations    = (uint)iterations;
            this.password      = Encoding.UTF8.GetBytes(password);
            this.HashAlgorithm = hashAlgorithm;
            this.hmac          = this.OpenHmac();

            // this.blockSize is in bytes, HashSize is in bits.
            this.blockSize = this.hmac.HashSize >> 3;

            this.Initialize();
        }
예제 #2
0
        public AesDeriveBytes(byte[] password, long iterations, HashAlgorithmName hashAlgorithm)
        {
            if (iterations <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(iterations), "iterations must be a positive number");
            }

            if (password == null)
            {
                throw new ArgumentNullException(nameof(password));
            }

            this.key = new byte[MinimumKeySize];
            using (var rng = new NerdyRandomNumberGenerator())
            {
                rng.NextBytes(this.key);
            }

            this.iv = new byte[16];
            Array.Clear(this.iv, 0, this.iv.Length);

            this.password      = (byte[])password.Clone();
            this.HashAlgorithm = hashAlgorithm;
            this.hmac          = this.CreateHmac();
            this.blockSize     = this.hmac.HashSize >> 3;
            this.Initialize();
        }
 protected static byte[] GenerateSalt(int length)
 {
     using (var rng = new NerdyRandomNumberGenerator())
     {
         return(rng.NextBytes(length));
     }
 }
예제 #4
0
        /// <summary>
        ///  Initializes a new instance of the <see cref="ChaCha20"/> class.
        /// </summary>
        protected ChaCha20()
        {
#if !NETCOREAPP
            this.LegalBlockSizesValue = ChaChaLegalBlockSizes;
            this.LegalKeySizesValue   = ChaChaLegalKeySizes;
#endif
            this.BlockSize = 64;
            this.KeySize   = 256;
            this.rng       = new NerdyRandomNumberGenerator();
        }
예제 #5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Salsa20"/> class.
        /// </summary>
        protected Salsa20()
        {
#if !NETCOREAPP
            this.LegalBlockSizesValue = SalsaLegalBlockSizes;
            this.LegalKeySizesValue   = SalsaLegalKeySizes;
#endif
            this.BlockSize = 64;
            this.KeySize   = 256;
            this.Rounds    = Salsa20Round.Twenty;
            this.rng       = new NerdyRandomNumberGenerator();
        }
예제 #6
0
        public AesDeriveBytes(byte[] password, byte[] key, byte[] iv, long iterations, HashAlgorithmName hashAlgorithm)
        {
            if (key != null && key.Length != 0 && key.Length < MinimumKeySize)
            {
                throw new ArgumentException($"salt minimum length is {MinimumKeySize}", nameof(key));
            }

            if (iterations <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(iterations), "iterations must be a positive number");
            }

            if (password == null)
            {
                throw new ArgumentNullException(nameof(password));
            }

            this.key = new byte[32];
            if (this.key == null || this.key.Length == 0)
            {
                using (var rng = new NerdyRandomNumberGenerator())
                {
                    rng.NextBytes(this.key);
                }
            }
            else
            {
                Array.Copy(key, this.key, this.key.Length);
            }

            this.iv = new byte[16];
            Array.Clear(this.iv, 0, this.iv.Length);

            if (iv != null && iv.Length > 0)
            {
                Array.Copy(iv, this.iv, iv.Length);
            }

            this.password      = (byte[])password.Clone();
            this.HashAlgorithm = hashAlgorithm;
            this.hmac          = this.CreateHmac();
            this.blockSize     = this.hmac.HashSize >> 3;
            this.Initialize();
        }
예제 #7
0
        public static char[] Generate(int length, char[] characters = null, Func <char[], bool> validate = null)
        {
            if (length < 1)
            {
                throw new IndexOutOfRangeException($"length must be 1 or greater. {length}");
            }

            if (characters == null)
            {
                characters = Combine(LatinAlphaUpperCase, LatinAlphaLowerCase, Digits, "#@_-^+");
            }

            if (validate == null)
            {
                validate = Validate;
            }

            var password = new char[length];
            var bytes    = new byte[length];

            while (validate(password) == false)
            {
                using (var rng = new NerdyRandomNumberGenerator())
                {
                    rng.NextBytes(bytes);
                }

                for (var i = 0; i < length; i++)
                {
                    var randomIndex = bytes[i] % characters.Length;
                    password[i] = characters[randomIndex];
                }
            }

            return(password);
        }
예제 #8
0
 private static byte[] GetRandomBytes(NerdyRandomNumberGenerator rng, int byteCount)
 {
     byte[] bytes = new byte[byteCount];
     rng.NextBytes(bytes);
     return(bytes);
 }