Exemple #1
0
 /// <param name="prf">
 ///    The Pseudo Random Function to use for calculating the derived key
 /// </param>
 /// <param name="salt">
 ///    The initial salt to use in calculating the derived key
 /// </param>
 /// <param name="iterationCount">
 ///    Number of iterations. RFC 2898 recommends a minimum of 1000
 ///    iterations (in the year 2000) ideally with number of iterations
 ///    adjusted on a regular basis (e.g. each year).
 /// </param>
 public PBKDF2DeriveBytes(
     IPseudoRandomFunction prf, byte[] salt, long iterationCount)
 {
     if (prf == null)
     {
         throw new ArgumentNullException("prf");
     }
     if (salt == null)
     {
         throw new ArgumentNullException("salt");
     }
     this.prf            = prf;
     this.salt           = salt;
     this.iterationCount = iterationCount;
     // Prepare combined salt = concat(original salt, block number)
     saltAndBlockNumber = new byte[salt.Length + 4];
     Buffer.BlockCopy(salt, 0, saltAndBlockNumber, 0, salt.Length);
     Reset();
 }
Exemple #2
0
        /// <param name="prf">
        ///    The Pseudo Random Function to use for calculating the derived key
        /// </param>
        /// <param name="salt">
        ///    The initial salt to use in calculating the derived key
        /// </param>
        /// <param name="iterationCount">
        ///    Number of iterations. RFC 2898 recommends a minimum of 1000
        ///    iterations (in the year 2000) ideally with number of iterations
        ///    adjusted on a regular basis (e.g. each year).
        /// </param>
        public PBKDF2DeriveBytes(IPseudoRandomFunction prf, byte[] salt, long iterationCount)
        {
            if (prf == null)
            {
                throw new ArgumentNullException("prf");
            }

            if (salt == null)
            {
                throw new ArgumentNullException("salt");
            }

            this.prf = prf;
            this.salt = salt;
            this.iterationCount = iterationCount;

            // Prepare combined salt = concat(original salt, block number)
            saltAndBlockNumber = new byte[salt.Length + 4];
            Buffer.BlockCopy(salt, 0, saltAndBlockNumber, 0, salt.Length);

            Reset();
        }
Exemple #3
0
        private static void ParseArguments(IEnumerable<string> args)
        {
            // Somewhat misusing ArgumentException here to throw parameter parser errors...

            try
            {
                options.Parse(args);
            }
            catch (OptionException e)
            {
                throw new ArgumentException(e.Message);
            }

            if (help)
            {
                ShowHelp();
                Environment.Exit(0);
            }

            if (password == null)
            {
                throw new ArgumentException("Missing password parameter");
            }

            if (salt == null)
            {
                throw new ArgumentException("Missing salt parameter");
            }

            byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
            saltBytes = Encoding.UTF8.GetBytes(salt);

            string normalizedAlgorithmName = algorithmName.Replace("-", "").ToUpperInvariant();

            switch (normalizedAlgorithmName)
            {
                case "MD5":
                    prf = new HMACPseudoRandomFunction<HMACMD5>(passwordBytes);
                    break;
                case "SHA1":
                    prf = new HMACPseudoRandomFunction<HMACSHA1>(passwordBytes);
                    break;
                case "SHA256":
                    prf = new HMACPseudoRandomFunction<HMACSHA256>(passwordBytes);
                    break;
                case "SHA384":
                    prf = new HMACPseudoRandomFunction<HMACSHA384>(passwordBytes);
                    break;
                case "SHA512":
                    prf = new HMACPseudoRandomFunction<HMACSHA512>(passwordBytes);
                    break;
                default:
                    throw new ArgumentException(String.Format("Unsupported algorithm: {0}", algorithmName));
            }

            // Use hash size as default number of output bytes
            outputBytes = outputBytes == 0 ? prf.HashSize : outputBytes;
        }
Exemple #4
0
        private static void ParseArguments(IEnumerable <string> args)
        {
            // Somewhat misusing ArgumentException here to throw parameter parser errors...

            try
            {
                options.Parse(args);
            }
            catch (OptionException e)
            {
                throw new ArgumentException(e.Message);
            }

            if (help)
            {
                ShowHelp();
                Environment.Exit(0);
            }

            if (password == null)
            {
                throw new ArgumentException("Missing password parameter");
            }

            if (salt == null)
            {
                throw new ArgumentException("Missing salt parameter");
            }

            byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
            saltBytes = Encoding.UTF8.GetBytes(salt);

            string normalizedAlgorithmName = algorithmName.Replace("-", "").ToUpperInvariant();

            switch (normalizedAlgorithmName)
            {
            case "MD5":
                prf = new HMACPseudoRandomFunction <HMACMD5>(passwordBytes);
                break;

            case "SHA1":
                prf = new HMACPseudoRandomFunction <HMACSHA1>(passwordBytes);
                break;

            case "SHA256":
                prf = new HMACPseudoRandomFunction <HMACSHA256>(passwordBytes);
                break;

            case "SHA384":
                prf = new HMACPseudoRandomFunction <HMACSHA384>(passwordBytes);
                break;

            case "SHA512":
                prf = new HMACPseudoRandomFunction <HMACSHA512>(passwordBytes);
                break;

            default:
                throw new ArgumentException(String.Format("Unsupported algorithm: {0}", algorithmName));
            }

            // Use hash size as default number of output bytes
            outputBytes = outputBytes == 0 ? prf.HashSize : outputBytes;
        }