コード例 #1
0
        public bool ValidatePassword(string encryptedPassword, string plainPassword)
        {
            Span <byte> encryptedBytes = stackalloc byte[m_OutputSize];

            HexStringConverter.GetBytes(encryptedPassword, encryptedBytes);

            ushort      iterations = BinaryPrimitives.ReadUInt16LittleEndian(encryptedBytes.Slice(0, 2));
            Span <byte> salt       = encryptedBytes.Slice(2, m_SaltSize);

            ReadOnlySpan <byte> hash =
                new Rfc2898DeriveBytes(plainPassword, salt.ToArray(), iterations, m_Algorithm).GetBytes(m_HashSize);

            return(hash.SequenceEqual(encryptedBytes.Slice(m_SaltSize + 2)));
        }
コード例 #2
0
        public bool ValidatePassword(string encryptedPassword, string plainPassword)
        {
            Span <byte> encryptedBytes = stackalloc byte[m_OutputSize];

            encryptedPassword.GetBytes(encryptedBytes);

            var iterations = BinaryPrimitives.ReadUInt16LittleEndian(encryptedBytes.SliceToLength(2));
            var salt       = encryptedBytes.Slice(2, m_SaltSize);

            ReadOnlySpan <byte> hash =
                new Rfc2898DeriveBytes(plainPassword, salt.ToArray(), iterations, HashAlgorithmName.SHA256).GetBytes(m_HashSize);

            return(hash.SequenceEqual(encryptedBytes.Slice(m_SaltSize + 2)));
        }
コード例 #3
0
        public bool CheckPassword(string passwordAttempt, string storedHash)
        {
            //Split the stored hash and parse the contained information
            var parts = storedHash.Split(".");

            if (parts.Length != 3)
            {
                return(false);
            }

            var salt       = Convert.FromBase64String(parts[0]);
            var iterations = int.Parse(parts[1]);
            var storedKey  = Convert.FromBase64String(parts[2]);

            var attemptKey = new Rfc2898DeriveBytes(passwordAttempt, salt, iterations, HashAlgorithmName.SHA512)
                             .GetBytes(storedKey.Length);

            return(attemptKey.SequenceEqual(storedKey));
        }
コード例 #4
0
        public bool VerifyHash(string text, byte[] hash, byte[] salt)
        {
            var hashedText = new Rfc2898DeriveBytes(text, salt, IterationsCount).GetBytes(HashLength);

            return(hashedText.SequenceEqual(hash));
        }