This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class HexUtils | |
{ | |
private const string HexAlphabet = "0123456789abcdef"; | |
public static string ToHexString(byte[] bytes) | |
{ | |
return string.Create(bytes.Length * 2, bytes, (chars, buffer) => | |
{ | |
var charIndex = 0; | |
var bufferIndex = 0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class HexUtils | |
{ | |
public static string ToHexString(byte[] bytes) | |
{ | |
return string.Create(bytes.Length * 2, bytes, (chars, buffer) => | |
{ | |
for (var i = 0; i < buffer.Length; i += 2) | |
{ | |
var b = buffer[i / 2]; | |
chars[i] = GetHexChar(b / 16); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class Match | |
{ | |
public static Matcher<T, TResult> From<T, TResult>() => new Matcher<T, TResult>(); | |
} | |
public sealed class Matcher<T, TResult> | |
{ | |
public TResult DefaultValue { get; set; } | |
public List<(TResult Value, Func<T,bool> Test)> Tests { get; } = new List<(TResult Value, Func<T,bool> Test)>(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public sealed class PasswordHasher : IPasswordHasher | |
{ | |
private const int SaltSize = 16; // 128 bit | |
private const int KeySize = 32; // 256 bit | |
public PasswordHasher(IOptions<HashingOptions> options) | |
{ | |
Options = options.Value; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public (bool Verified, bool NeedsUpgrade) Check(string hash, string password) | |
{ | |
var parts = hash.Split('.', 3); | |
if (parts.Length != 3) | |
{ | |
throw new FormatException("Unexpected hash format. " + | |
"Should be formatted as `{iterations}.{salt}.{hash}`"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public string Hash(string password) | |
{ | |
using (var algorithm = new Rfc2898DeriveBytes( | |
password, | |
SaltSize, | |
Options.Iterations, | |
HashAlgorithmName.SHA256)) | |
{ | |
var key = Convert.ToBase64String(algorithm.GetBytes(KeySize)); | |
var salt = Convert.ToBase64String(algorithm.Salt); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public sealed class PasswordHasher : IPasswordHasher | |
{ | |
private const int SaltSize = 16; // 128 bit | |
private const int KeySize = 32; // 256 bit | |
public PasswordHasher(IOptions<HashingOptions> options) | |
{ | |
Options = options.Value; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public sealed class HashingOptions | |
{ | |
public int Iterations { get; set; } = 10000; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IPasswordHasher | |
{ | |
string Hash(string password); | |
(bool Verified, bool NeedsUpgrade) Check(string hash, string password); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IRateLimiter | |
{ | |
Task<T> RunRateLimited<T>(Func<Task<T>> func); | |
} |
NewerOlder