public static StoredPassword From(byte[] hash, byte[] salt, TargetHashConfigration opts) { return(new StoredPassword { Hash = hash, Salt = salt, Options = opts, }); }
public Task <byte[]> Hash(byte[] password, byte[] salt, TargetHashConfigration options) { return(Task.Factory.StartNew(() => KeyDerivation.Pbkdf2( password: BitConverter.ToString(password), salt: salt, prf: Prf(options), iterationCount: options.IterationCount ?? throw new InvalidOperationException($"{nameof(options.IterationCount)} required for PBKDF2"), numBytesRequested: options.PasswordHashBytes ?? throw new InvalidOperationException($"{nameof(options.PasswordHashBytes)} required for PBKDF2")) )); }
public Task <byte[]> Hash(byte[] password, byte[] salt, TargetHashConfigration options) { switch (options.Algorithm ?? throw new InvalidOperationException($"{nameof(options.Algorithm)} required for hashing passwords")) { case HashingAlgorithm.SHA1: return(sha1.Hash(password, salt, options)); case HashingAlgorithm.PBKDF2: return(pbkdf2.Hash(password, salt, options)); default: throw new NotImplementedException($"Hashing algorithm {options.Algorithm} not yet implemented"); } }
public async Task <byte[]> Hash(byte[] password, byte[] salt, TargetHashConfigration options) { using (var stream = new MemoryStream(password.Length + salt.Length)) { await stream.WriteAsync(password, 0, password.Length); await stream.WriteAsync(salt, 0, salt.Length); stream.Seek(0, SeekOrigin.Begin); using (var sha = new SHA1CryptoServiceProvider()) { return(sha.ComputeHash(stream)); } } }
private KeyDerivationPrf Prf(TargetHashConfigration options) { switch (options.Algorithm2 ?? throw new InvalidOperationException($"{nameof(options.IterationCount)} required for PBKDF2")) { case HashingAlgorithm.SHA1: return(KeyDerivationPrf.HMACSHA1); case HashingAlgorithm.SHA256: return(KeyDerivationPrf.HMACSHA256); case HashingAlgorithm.SHA512: return(KeyDerivationPrf.HMACSHA512); default: throw new IndexOutOfRangeException($"{options.Algorithm2.Value} is unsupported Algorithm2 for PBKDF2"); } }