private static byte[] Hi1(UsernamePasswordCredential credential, byte[] salt, int iterations) { var passwordDigest = AuthenticationHelper.MongoPasswordDigest(credential.Username, credential.Password); using (var deriveBytes = new Rfc2898DeriveBytes(passwordDigest, salt, iterations, HashAlgorithmName.SHA1)) { // 20 is the length of output of a sha-1 hmac return(deriveBytes.GetBytes(20)); } }
private static byte[] Hi256(char[] passwordChars, byte[] salt, int iterations) { var passwordBytes = new byte[Utf8Encodings.Strict.GetByteCount(passwordChars)]; var passwordBytesHandle = GCHandle.Alloc(passwordBytes, GCHandleType.Pinned); try { Utf8Encodings.Strict.GetBytes(passwordChars, 0, passwordChars.Length, passwordBytes, 0); using (var deriveBytes = new Rfc2898DeriveBytes(passwordBytes, salt, iterations, HashAlgorithmName.SHA256)) { // 32 is the length of output of a sha-256 hmac return(deriveBytes.GetBytes(32)); } } finally { Array.Clear(passwordBytes, 0, passwordBytes.Length); passwordBytesHandle.Free(); } }