Exemplo n.º 1
0
        public static string MakePbkdf2String(this IKeyedHashFunction hash, string input, byte[] salt, int length, int iterationCount)
        {
            var data   = Encoding.UTF8.GetBytes(input);
            var result = hash.MakePbkdf2(data, salt, length, iterationCount);

            return(Convert.ToBase64String(result));
        }
Exemplo n.º 2
0
        public static bool VerifyPbkdf2String(this IKeyedHashFunction hash, string hashedString, string otherString, string saltString, int iterationCount)
        {
            var hashed    = Convert.FromBase64String(hashedString);
            var otherData = Encoding.UTF8.GetBytes(otherString);
            var salt      = Convert.FromBase64String(saltString);

            return(hash.VerifyPbkdf2(hashed, otherData, salt, iterationCount));
        }
Exemplo n.º 3
0
        public static byte[] MakePbkdf2(this IKeyedHashFunction hash, byte[] salt, int length, int iterationCount)
        {
            var hashKey = hash.Key;

            if (hashKey == null || hashKey.Length == 0)
            {
                throw new InvalidOperationException("Key was not specified for the hash.");
            }

            return(MakePbkdf2(hash, hashKey, salt, length, iterationCount));
        }
Exemplo n.º 4
0
        public static bool VerifyPbkdf2(this IKeyedHashFunction hash, byte[] hashed, byte[] otherData, byte[] salt, int iterationCount)
        {
            int length = hashed.Length;

            var otherHashed = hash.MakePbkdf2(otherData, salt, length, iterationCount);

            if (otherHashed.Length != hashed.Length)
            {
                return(false);
            }

            return(ByteArraysEqual(hashed, otherHashed));
        }
Exemplo n.º 5
0
        public static byte[] MakePbkdf2(this IKeyedHashFunction hash, byte[] data, byte[] salt, int length, int iterationCount)
        {
            hash.Key = data;

            int hashLength = hash.HashSize / 8;

            if ((hash.HashSize & 7) != 0)
            {
                hashLength++;
            }

            int keyLength = length / hashLength;

            if (length > (0xFFFFFFFFL * hashLength) || length < 0)
            {
                throw new ArgumentOutOfRangeException("length");
            }

            if (length % hashLength != 0)
            {
                keyLength++;
            }

            byte[] extendedkey = new byte[salt.Length + 4];
            Buffer.BlockCopy(salt, 0, extendedkey, 0, salt.Length);

            using (var ms = new MemoryStream()) {
                for (int i = 0; i < keyLength; i++)
                {
                    extendedkey[salt.Length]     = (byte)(((i + 1) >> 24) & 0xFF);
                    extendedkey[salt.Length + 1] = (byte)(((i + 1) >> 16) & 0xFF);
                    extendedkey[salt.Length + 2] = (byte)(((i + 1) >> 8) & 0xFF);
                    extendedkey[salt.Length + 3] = (byte)(((i + 1)) & 0xFF);

                    byte[] u = hash.Compute(extendedkey);
                    Array.Clear(extendedkey, salt.Length, 4);

                    byte[] f = u;
                    for (int j = 1; j < iterationCount; j++)
                    {
                        u = hash.Compute(u);
                        for (int k = 0; k < f.Length; k++)
                        {
                            f[k] ^= u[k];
                        }
                    }

                    ms.Write(f, 0, f.Length);
                    Array.Clear(u, 0, u.Length);
                    Array.Clear(f, 0, f.Length);
                }

                byte[] dk = new byte[length];
                ms.Position = 0;
                ms.Read(dk, 0, length);
                ms.Position = 0;

                for (long i = 0; i < ms.Length; i++)
                {
                    ms.WriteByte(0);
                }

                Array.Clear(extendedkey, 0, extendedkey.Length);
                return(dk);
            }
        }
Exemplo n.º 6
0
 public static byte[] MakePbkdf2(this IKeyedHashFunction hash, byte[] data, byte[] salt, int length)
 {
     return(MakePbkdf2(hash, data, salt, length, DefaultIterationCount));
 }
Exemplo n.º 7
0
 public static string MakePbkdf2String(this IKeyedHashFunction hash, string input, byte[] salt, int length)
 {
     return(MakePbkdf2String(hash, input, salt, length, DefaultIterationCount));
 }
Exemplo n.º 8
0
 public static string MakePbkdf2String(this IKeyedHashFunction hash, string input, string salt, int length, int iterationCount)
 {
     return(hash.MakePbkdf2String(input, Convert.FromBase64String(salt), length, iterationCount));
 }
Exemplo n.º 9
0
 public static bool VerifyPbkdf2String(this IKeyedHashFunction hash, string hashedString, string otherString, string saltString)
 {
     return(VerifyPbkdf2String(hash, hashedString, otherString, saltString, DefaultIterationCount));
 }
Exemplo n.º 10
0
 public static bool VerifyPbkdf2String(this IKeyedHashFunction hash, string hashedString, string otherString, byte[] salt, int iterationCount)
 {
     return(hash.VerifyPbkdf2String(hashedString, otherString, Convert.ToBase64String(salt), iterationCount));
 }
Exemplo n.º 11
0
 public static bool VerifyPbkdf2(this IKeyedHashFunction hash, byte[] hashed, byte[] otherData, byte[] salt)
 {
     return(VerifyPbkdf2(hash, hashed, otherData, salt, DefaultIterationCount));
 }