Esempio n. 1
0
        public static Pbkdf2Result Hash(string password, int?saltLength, int?iteration, int?keyLength)
        {
            Pbkdf2Parameter parameter = new Pbkdf2Parameter();

            parameter.Password   = BytesEx.FromString(password);
            parameter.SaltLength = saltLength;
            parameter.Iteration  = iteration;
            parameter.KeyLength  = keyLength;
            Pbkdf2Result pbkdf2 = Hash(parameter);

            return(pbkdf2);
        }
Esempio n. 2
0
        public static Pbkdf2Result Hash(Pbkdf2Parameter parameter)
        {
            if (parameter == null || parameter.Password == null)
            {
                throw new ArgumentNullException();
            }
            if (parameter.Salt == null && parameter.SaltLength == null)
            {
                parameter.SaltLength = 16;
            }

            byte[] passwordBytes = parameter.Password.Values;
            byte[] saltBytes     = parameter.SaltLength.HasValue ?
                                   RandomExpress.RandomizeBytes(parameter.SaltLength.Value)
          : parameter.Salt.Values;
            int iteration             = parameter.Iteration ?? 17;
            int keyLength             = parameter.KeyLength ?? 16;
            Rfc2898DeriveBytes hasher = new Rfc2898DeriveBytes(passwordBytes, saltBytes, iteration);

            byte[]       keyBytes = hasher.GetBytes(keyLength);
            Pbkdf2Result pbkdf2   = new Pbkdf2Result(passwordBytes, saltBytes, iteration, keyBytes);

            return(pbkdf2);
        }