| | | 1 | | namespace ArturRios.Util.Hashing; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Configuration values for controlling Argon2id hashing cost parameters. |
| | | 5 | | /// </summary> |
| | | 6 | | /// <remarks> |
| | | 7 | | /// The defaults are deliberately expensive: a single hash reserves |
| | | 8 | | /// <see cref="DefaultMemoryToUseInKb"/> kilobytes (roughly 600 MB) across |
| | | 9 | | /// <see cref="DefaultDegreeOfParallelism"/> lanes. That is appropriate for a login path that runs rarely, |
| | | 10 | | /// but a server hashing many secrets concurrently will exhaust its memory long before its CPU, so size the |
| | | 11 | | /// parameters against the expected concurrency. Note that changing them invalidates existing hashes: |
| | | 12 | | /// a stored hash can only be verified with the configuration it was produced with. |
| | | 13 | | /// </remarks> |
| | | 14 | | /// <param name="degreeOfParallelism">Number of threads (lanes) to use; defaults to <see cref="DefaultDegreeOfParallelis |
| | | 15 | | /// <param name="numberOfIterations">Number of iterations; defaults to <see cref="DefaultNumberOfIterations"/>.</param> |
| | | 16 | | /// <param name="memoryToUseInKb">Memory size in kilobytes; defaults to <see cref="DefaultMemoryToUseInKb"/>.</param> |
| | | 17 | | /// <exception cref="ArgumentOutOfRangeException">Any supplied value is less than one.</exception> |
| | 31 | 18 | | public class HashConfiguration( |
| | 31 | 19 | | int? degreeOfParallelism = null, |
| | 31 | 20 | | int? numberOfIterations = null, |
| | 31 | 21 | | int? memoryToUseInKb = null) |
| | | 22 | | { |
| | | 23 | | /// <summary> |
| | | 24 | | /// Default parallelism (threads) used when unspecified. |
| | | 25 | | /// </summary> |
| | | 26 | | public const int DefaultDegreeOfParallelism = 16; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Default recommended minimum iteration count. |
| | | 30 | | /// </summary> |
| | | 31 | | public const int DefaultNumberOfIterations = 4; |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Default memory cost (600 MB) expressed in kilobytes. |
| | | 35 | | /// </summary> |
| | | 36 | | public const int DefaultMemoryToUseInKb = 600000; |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Gets the configured degree of parallelism. |
| | | 40 | | /// </summary> |
| | 21 | 41 | | public int DegreeOfParallelism { get; } = |
| | 37 | 42 | | Positive(degreeOfParallelism ?? DefaultDegreeOfParallelism, nameof(degreeOfParallelism)); |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Gets the configured number of iterations. |
| | | 46 | | /// </summary> |
| | 21 | 47 | | public int NumberOfIterations { get; } = |
| | 35 | 48 | | Positive(numberOfIterations ?? DefaultNumberOfIterations, nameof(numberOfIterations)); |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Gets the configured memory usage in kilobytes. |
| | | 52 | | /// </summary> |
| | 21 | 53 | | public int MemoryToUseInKb { get; } = |
| | 33 | 54 | | Positive(memoryToUseInKb ?? DefaultMemoryToUseInKb, nameof(memoryToUseInKb)); |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Rejects cost parameters Argon2 cannot honour. |
| | | 58 | | /// </summary> |
| | | 59 | | private static int Positive(int value, string parameterName) |
| | | 60 | | { |
| | 105 | 61 | | ArgumentOutOfRangeException.ThrowIfLessThan(value, 1, parameterName); |
| | | 62 | | |
| | 99 | 63 | | return value; |
| | | 64 | | } |
| | | 65 | | } |