| | | 1 | | using System.Security.Cryptography; |
| | | 2 | | using System.Text; |
| | | 3 | | using Konscious.Security.Cryptography; |
| | | 4 | | |
| | | 5 | | namespace ArturRios.Util.Hashing; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Provides hashing helpers based on Argon2id for secure password / secret derivation. |
| | | 9 | | /// </summary> |
| | | 10 | | /// <remarks> |
| | | 11 | | /// All methods use a key length of 128 bytes and allow optional configuration overrides via <see cref="HashConfiguratio |
| | | 12 | | /// </remarks> |
| | | 13 | | public static class Hash |
| | | 14 | | { |
| | | 15 | | private const int Argon2IdKeyBytes = 128; |
| | | 16 | | private const int SaltByteSize = 16; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Hashes <paramref name="text"/> using Argon2id with a provided salt and optional configuration. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="text">The input text to hash (e.g. password).</param> |
| | | 22 | | /// <param name="salt">A cryptographically strong random salt.</param> |
| | | 23 | | /// <param name="configuration">Optional hashing configuration; defaults are used if null.</param> |
| | | 24 | | /// <returns>The derived hash bytes.</returns> |
| | | 25 | | public static byte[] EncodeWithSalt(string text, byte[] salt, HashConfiguration? configuration = null) |
| | 3 | 26 | | { |
| | 3 | 27 | | configuration ??= new HashConfiguration(); |
| | | 28 | | |
| | 3 | 29 | | Argon2id argon2Id = new(Encoding.UTF8.GetBytes(text)) |
| | 3 | 30 | | { |
| | 3 | 31 | | Salt = salt, |
| | 3 | 32 | | DegreeOfParallelism = configuration.DegreeOfParallelism, |
| | 3 | 33 | | Iterations = configuration.NumberOfIterations, |
| | 3 | 34 | | MemorySize = configuration.MemoryToUseInKb |
| | 3 | 35 | | }; |
| | | 36 | | |
| | 3 | 37 | | return argon2Id.GetBytes(Argon2IdKeyBytes); |
| | 3 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Hashes <paramref name="text"/> using Argon2id and a newly generated random 16-byte salt. |
| | | 42 | | /// </summary> |
| | | 43 | | /// <param name="text">The input text to hash.</param> |
| | | 44 | | /// <param name="salt">Outputs the generated salt used for hashing.</param> |
| | | 45 | | /// <param name="configuration">Optional hashing configuration.</param> |
| | | 46 | | /// <returns>The derived hash bytes.</returns> |
| | | 47 | | public static byte[] EncodeWithRandomSalt(string text, out byte[] salt, HashConfiguration? configuration = null) |
| | 5 | 48 | | { |
| | 5 | 49 | | configuration ??= new HashConfiguration(); |
| | 5 | 50 | | salt = CreateSalt(); |
| | | 51 | | |
| | 5 | 52 | | Argon2id argon2Id = new(Encoding.UTF8.GetBytes(text)) |
| | 5 | 53 | | { |
| | 5 | 54 | | Salt = salt, |
| | 5 | 55 | | DegreeOfParallelism = configuration.DegreeOfParallelism, |
| | 5 | 56 | | Iterations = configuration.NumberOfIterations, |
| | 5 | 57 | | MemorySize = configuration.MemoryToUseInKb |
| | 5 | 58 | | }; |
| | | 59 | | |
| | 5 | 60 | | return argon2Id.GetBytes(Argon2IdKeyBytes); |
| | 5 | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Verifies if the provided <paramref name="hash"/> matches hashing <paramref name="text"/> with <paramref name="sa |
| | | 65 | | /// </summary> |
| | | 66 | | /// <param name="text">Plain text to verify.</param> |
| | | 67 | | /// <param name="hash">Expected hash value.</param> |
| | | 68 | | /// <param name="salt">Salt associated with the stored hash.</param> |
| | | 69 | | /// <returns><c>true</c> if the hashes match; otherwise <c>false</c>.</returns> |
| | | 70 | | public static bool TextMatches(string text, byte[] hash, byte[] salt) |
| | 2 | 71 | | { |
| | 2 | 72 | | var hashToMatch = EncodeWithSalt(text, salt); |
| | | 73 | | |
| | 2 | 74 | | return hash.SequenceEqual(hashToMatch); |
| | 2 | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Creates a cryptographically strong random salt. |
| | | 79 | | /// </summary> |
| | | 80 | | /// <returns>A 16-byte salt.</returns> |
| | | 81 | | private static byte[] CreateSalt() |
| | 5 | 82 | | { |
| | 5 | 83 | | var buffer = new byte[SaltByteSize]; |
| | | 84 | | |
| | 5 | 85 | | using var rng = RandomNumberGenerator.Create(); |
| | 5 | 86 | | rng.GetBytes(buffer); |
| | | 87 | | |
| | 5 | 88 | | return buffer; |
| | 5 | 89 | | } |
| | | 90 | | } |