コード例 #1
0
        public static string GenerateHashedPassword(string password, string salt, string secretKey)
        {
            byte[] secretKeyBytes = new System.Text.UTF8Encoding().GetBytes(secretKey);
            byte[] passwordBytes  = new System.Text.UTF8Encoding().GetBytes(password);

            // Because the salt is stored in base-64 we just need to convert it back to
            // an array of bytes
            byte[] saltBytes = Convert.FromBase64String(salt);

            var hashInput = new byte[passwordBytes.Length + saltBytes.Length];

            passwordBytes.CopyTo(hashInput, 0);
            saltBytes.CopyTo(hashInput, passwordBytes.Length);

            using (var hash = new HMACSHA256(secretKeyBytes))
            {
                // Compute a hash based on the password + salt and convert to base64 string
                byte[] hashBytes = hash.ComputeHash(hashInput);
                string hashValue = Convert.ToBase64String(hashBytes);

                return(hashValue);
            }
        }