예제 #1
0
 /// <summary>
 /// Implements a <see cref="PBKDF2"/> algorthim with a user definded MAC method.
 /// </summary>
 /// <param name="method">the HMAC method to use.</param>
 /// <param name="password">the password to use</param>
 /// <param name="salt">the salt. Must be at least 64-bit</param>
 /// <param name="iterations">the number of iterations. Must be at least 1000</param>
 /// <param name="length">the number of bytes to return</param>
 /// <returns>
 /// A salted password based on the specified length.
 /// </returns>
 public static byte[] ComputeSaltedPassword(HMACMethod method, byte[] password, byte[] salt, int iterations, int length)
 {
     using (var kdf = new PBKDF2(method, password, salt, iterations))
     {
         return(kdf.GetBytes(length));
     }
 }
예제 #2
0
        /// <summary>
        /// Implements a <see cref="PBKDF2"/> algorthim with a user definded MAC method.
        /// </summary>
        /// <param name="method">the HMAC method to use.</param>
        /// <param name="password">the password to use</param>
        /// <param name="salt">the salt. recommended to be at least 64-bit</param>
        /// <param name="iterations">the number of iterations. Recommended to be at least 1000</param>
        public PBKDF2(HMACMethod method, byte[] password, byte[] salt, int iterations)
        {
            if (password == null)
            {
                throw new ArgumentNullException("password");
            }

            switch (method)
            {
            case HMACMethod.MD5:
                Initialize(new HMac(new MD5Digest()), password, salt, iterations);
                break;

            case HMACMethod.TripleDES:
                //Initialize(new MACTripleDES(password), salt, iterations);
                break;

            case HMACMethod.RIPEMD160:
                Initialize(new HMac(new RipeMD160Digest()), password, salt, iterations);
                //Initialize(new HMACRIPEMD160(password), salt, iterations);
                break;

            case HMACMethod.SHA1:
                Initialize(new HMac(new Sha1Digest()), password, salt, iterations);
                //Initialize(new HMAC<SHA1Core>(password), salt, iterations);
                break;

            case HMACMethod.SHA256:
                Initialize(new HMac(new Sha256Digest()), password, salt, iterations);
                //Initialize(new HMAC<SHA256Core>(password), salt, iterations);
                break;

            case HMACMethod.SHA384:
                Initialize(new HMac(new Sha384Digest()), password, salt, iterations);
                //Initialize(new HMACSHA384(password), salt, iterations);
                break;

            case HMACMethod.SHA512:
                Initialize(new HMac(new Sha512Digest()), password, salt, iterations);
                //Initialize(new HMAC<SHA512Core>(password), salt, iterations);
                //Initialize(new HMACSHA512(password), salt, iterations);
                break;

            default:
                throw new ArgumentOutOfRangeException("method");
            }
        }
        /// <summary>
        /// Implements a <see cref="PBKDF2"/> algorthim with a user definded MAC method.
        /// </summary>
        /// <param name="method">the HMAC method to use.</param>
        /// <param name="password">the password to use</param>
        /// <param name="salt">the salt. recommended to be at least 64-bit</param>
        /// <param name="iterations">the number of iterations. Recommended to be at least 1000</param>
        public PBKDF2(HMACMethod method, byte[] password, byte[] salt, int iterations)
        {
            if (password == null)
                throw new ArgumentNullException("password");

            switch (method)
            {
                case HMACMethod.MD5:
                    Initialize(new HMac(new MD5Digest()), password, salt, iterations);
                    break;
                case HMACMethod.TripleDES:
                    //Initialize(new MACTripleDES(password), salt, iterations);
                    break;
                case HMACMethod.RIPEMD160:
                    Initialize(new HMac(new RipeMD160Digest()), password, salt, iterations);
                    //Initialize(new HMACRIPEMD160(password), salt, iterations);
                    break;
                case HMACMethod.SHA1:
                    Initialize(new HMac(new Sha1Digest()), password, salt, iterations);
                    //Initialize(new HMAC<SHA1Core>(password), salt, iterations);
                    break;
                case HMACMethod.SHA256:
                    Initialize(new HMac(new Sha256Digest()), password, salt, iterations);
                    //Initialize(new HMAC<SHA256Core>(password), salt, iterations);
                    break;
                case HMACMethod.SHA384:
                    Initialize(new HMac(new Sha384Digest()), password, salt, iterations);
                    //Initialize(new HMACSHA384(password), salt, iterations);
                    break;
                case HMACMethod.SHA512:
                    Initialize(new HMac(new Sha512Digest()), password, salt, iterations);
                    //Initialize(new HMAC<SHA512Core>(password), salt, iterations);
                    //Initialize(new HMACSHA512(password), salt, iterations);
                    break;
                default:
                    throw new ArgumentOutOfRangeException("method");
            }
        }
 /// <summary>
 /// Implements a <see cref="PBKDF2"/> algorthim with a user definded MAC method.
 /// </summary>
 /// <param name="method">the HMAC method to use.</param>
 /// <param name="password">the password to use</param>
 /// <param name="salt">the salt. Must be at least 64-bit</param>
 /// <param name="iterations">the number of iterations. Must be at least 1000</param>
 /// <param name="length">the number of bytes to return</param>
 /// <returns>
 /// A salted password based on the specified length.
 /// </returns>
 public static byte[] ComputeSaltedPassword(HMACMethod method, byte[] password, byte[] salt, int iterations, int length)
 {
     using (var kdf = new PBKDF2(method, password, salt, iterations))
     {
         return kdf.GetBytes(length);
     }
 }