| | 1 | | namespace NLightning.Domain.Crypto.ValueObjects; |
| | 2 | |
|
| | 3 | | using Constants; |
| | 4 | | using Utils.Extensions; |
| | 5 | |
|
| | 6 | | public readonly struct Secret : IEquatable<Secret> |
| | 7 | | { |
| | 8 | | private readonly byte[] _value; |
| 64 | 9 | | public static Secret Empty => new(new byte[CryptoConstants.SecretLen]); |
| | 10 | |
|
| | 11 | | public Secret(byte[] value) |
| | 12 | | { |
| 960 | 13 | | if (value.Length < CryptoConstants.SecretLen) |
| 0 | 14 | | throw new ArgumentOutOfRangeException(nameof(value), value.Length, |
| 0 | 15 | | $"Hash must have {CryptoConstants.SecretLen} bytes."); |
| | 16 | |
|
| 960 | 17 | | _value = value; |
| 960 | 18 | | } |
| | 19 | |
|
| 816 | 20 | | public static implicit operator Secret(byte[] bytes) => new(bytes); |
| 712 | 21 | | public static implicit operator byte[](Secret hash) => hash._value; |
| | 22 | |
|
| 0 | 23 | | public static implicit operator ReadOnlyMemory<byte>(Secret hash) => hash._value; |
| 4488 | 24 | | public static implicit operator ReadOnlySpan<byte>(Secret hash) => hash._value; |
| | 25 | |
|
| | 26 | | public bool Equals(Secret other) |
| | 27 | | { |
| 76 | 28 | | return _value.SequenceEqual(other._value); |
| | 29 | | } |
| | 30 | |
|
| | 31 | | public override bool Equals(object? obj) |
| | 32 | | { |
| 0 | 33 | | return obj is Secret other && Equals(other); |
| | 34 | | } |
| | 35 | |
|
| | 36 | | public override int GetHashCode() |
| | 37 | | { |
| 0 | 38 | | return _value.GetByteArrayHashCode(); |
| | 39 | | } |
| | 40 | | } |