Example #1
0
        public static byte[] DeriveKeyHash(string username, string password, int iterationCount)
        {
            var key = DeriveKey(username, password, iterationCount);

            return(iterationCount == 1
                ? Crypto.Sha256(key.ToHex() + password)
                : Pbkdf2.GenerateSha256(key, password.ToBytes(), 1, 32));
        }
Example #2
0
 internal static byte[] DeriveMasterPasswordAuthKey(string userId,
                                                    byte[] encryptionKey,
                                                    DatabaseInfo databaseInfo)
 {
     return(Pbkdf2.GenerateSha256(password: encryptionKey,
                                  salt: Encoding.Unicode.GetBytes(userId),
                                  iterationCount: 1500,
                                  byteCount: 64));
 }
Example #3
0
        public static byte[] DeriveKey(string username, string password, int iterationCount)
        {
            if (iterationCount <= 0)
            {
                throw new InternalErrorException("Iteration count should be positive");
            }

            return(iterationCount == 1
                ? Crypto.Sha256(username + password)
                : Pbkdf2.GenerateSha256(password.ToBytes(), username.ToBytes(), iterationCount, 32));
        }
Example #4
0
        //
        // Internal
        //

        internal static byte[] HashPassword(string password, AuthInfo authInfo)
        {
            var passwordBytes = password.ToBytes();

            if (authInfo.IsMd5)
            {
                passwordBytes = Crypto.Md5(passwordBytes);
            }

            return(Pbkdf2.GenerateSha256(passwordBytes, authInfo.Salt, authInfo.IterationCount, 32));
        }
Example #5
0
        public void GenerateSha256_returns_correct_result()
        {
            foreach (var i in _testDataSha256)
            {
                var expected = i.Expected.DecodeHex();

                Assert.AreEqual(expected,
                                Pbkdf2.GenerateSha256(i.Password, i.Salt, i.IterationCount, expected.Length));
                Assert.AreEqual(expected,
                                Pbkdf2.Generate <HMACSHA256>(i.Password, i.Salt, i.IterationCount, expected.Length));
            }
        }
Example #6
0
            public byte[] Derive(byte[] password, byte[] salt)
            {
                switch (HashMethod)
                {
                case HashMethodType.Sha1:
                    return(Pbkdf2.GenerateSha1(password, salt, Iterations, 32));

                case HashMethodType.Sha256:
                    return(Pbkdf2.GenerateSha256(password, salt, Iterations, 32));
                }

                throw new InternalErrorException($"Unknown hash method {HashMethod}");
            }
Example #7
0
 public static byte[] HashPassword(string password, byte[] key)
 {
     return(Pbkdf2.GenerateSha256(key, password.ToBytes(), 1, 32));
 }
Example #8
0
 public static byte[] DeriveKey(string username, string password, int iterations)
 {
     return(Pbkdf2.GenerateSha256(password.ToBytes(), username.ToLower().Trim().ToBytes(), iterations, 32));
 }