Exemple #1
0
        /// <summary>
        /// Computes a derived key.
        /// </summary>
        /// <param name="hmacAlgorithm">
        /// </param>
        /// <param name="salt">
        ///     The salt.
        ///     A unique salt means a unique derived key, even if the original key is identical.
        /// </param>
        /// <param name="iterations">The number of iterations to apply.</param>
        /// <param name="derivedKeyLength">The desired length of the derived key.</param>
        /// <returns>The derived key.</returns>
#if USEBC || WINDOWS_UWP || NETCORE
        public static byte[] ComputeDerivedKey(nStratis.BouncyCastle.crypto.IMac hmacAlgorithm, byte[] salt, int iterations,
                                               int derivedKeyLength)
        {
            nStratis.Crypto.Cryptsharp.Check.Range("derivedKeyLength", derivedKeyLength, 0, int.MaxValue);

            using (Pbkdf2 kdf = new Pbkdf2(hmacAlgorithm, salt, iterations))
            {
                return(kdf.Read(derivedKeyLength));
            }
        }
Exemple #2
0
        /// <summary>
        /// Creates a new PBKDF2 stream.
        /// </summary>
        /// <param name="hmacAlgorithm">
        /// </param>
        /// <param name="salt">
        ///     The salt.
        ///     A unique salt means a unique PBKDF2 stream, even if the original key is identical.
        /// </param>
        /// <param name="iterations">The number of iterations to apply.</param>
#if USEBC || WINDOWS_UWP || NETCORE
        public Pbkdf2(nStratis.BouncyCastle.crypto.IMac hmacAlgorithm, byte[] salt, int iterations)
        {
            nStratis.Crypto.Cryptsharp.Check.Null("hmacAlgorithm", hmacAlgorithm);
            nStratis.Crypto.Cryptsharp.Check.Null("salt", salt);
            nStratis.Crypto.Cryptsharp.Check.Length("salt", salt, 0, int.MaxValue - 4);
            nStratis.Crypto.Cryptsharp.Check.Range("iterations", iterations, 1, int.MaxValue);
            int hmacLength = hmacAlgorithm.GetMacSize();

            _saltBuffer = new byte[salt.Length + 4];
            Array.Copy(salt, _saltBuffer, salt.Length);
            _iterations    = iterations;
            _hmacAlgorithm = hmacAlgorithm;
            _digest        = new byte[hmacLength];
            _digestT1      = new byte[hmacLength];
        }
Exemple #3
0
        public Pbkdf2(KeyedHashAlgorithm hmacAlgorithm, byte[] salt, int iterations)
        {
            Check.Null("hmacAlgorithm", hmacAlgorithm);
            Check.Null("salt", salt);
            Check.Length("salt", salt, 0, int.MaxValue - 4);
            Check.Range("iterations", iterations, 1, int.MaxValue);
            if (hmacAlgorithm.HashSize == 0 || hmacAlgorithm.HashSize % 8 != 0)
            {
                throw Exceptions.Argument("hmacAlgorithm", "Unsupported hash size.");
            }

            int hmacLength = hmacAlgorithm.HashSize / 8;

            _saltBuffer = new byte[salt.Length + 4]; Array.Copy(salt, _saltBuffer, salt.Length);
            _iterations = iterations; _hmacAlgorithm = hmacAlgorithm;
            _digest     = new byte[hmacLength]; _digestT1 = new byte[hmacLength];
        }