| | | 1 | | namespace Elsa.Identity.Models; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Represents a hashed secret. |
| | | 5 | | /// </summary> |
| | | 6 | | /// <param name="Secret">A base64 encoded string representing the hashed secret.</param> |
| | | 7 | | /// <param name="Salt">A base64 encoded string representing the salt.</param> |
| | 0 | 8 | | public record HashedSecret(byte[] Secret, byte[] Salt) |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Creates a new instance of <see cref="HashedSecret"/> from a byte array representing the hashed secret and the sa |
| | | 12 | | /// </summary> |
| | | 13 | | /// <param name="secret">The hashed secret.</param> |
| | | 14 | | /// <param name="salt">The salt.</param> |
| | | 15 | | /// <returns>A new instance of <see cref="HashedSecret"/>.</returns> |
| | 0 | 16 | | public static HashedSecret FromBytes(byte[] secret, byte[] salt) => new(secret, salt); |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Creates a new instance of <see cref="HashedSecret"/> from a string representing the hashed secret and the salt. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="secret">The hashed secret.</param> |
| | | 22 | | /// <param name="salt">The salt.</param> |
| | | 23 | | /// <returns>A new instance of <see cref="HashedSecret"/>.</returns> |
| | 0 | 24 | | public static HashedSecret FromString(string secret, string salt) => new(Decode(secret), Decode(salt)); |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Encodes the secret using base64. |
| | | 28 | | /// </summary> |
| | | 29 | | /// <returns>The base64-encoded secret.</returns> |
| | 0 | 30 | | public string EncodeSecret() => Encode(Secret); |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Encodes the salt using base64. |
| | | 34 | | /// </summary> |
| | | 35 | | /// <returns>The base64-encoded salt.</returns> |
| | 0 | 36 | | public string EncodeSalt() => Encode(Salt); |
| | | 37 | | |
| | 0 | 38 | | private static byte[] Decode(string value) => Convert.FromBase64String(value); |
| | 0 | 39 | | private static string Encode(byte[] value) => Convert.ToBase64String(value); |
| | | 40 | | } |