Skip to content

Commit

Permalink
Thread-safe hash calculation (#193)
Browse files Browse the repository at this point in the history
Use static `HMACSHA256.TryHashData` to calculate hash instead of instance method.
  • Loading branch information
ShortDevelopment authored Jan 27, 2025
1 parent a6a318b commit 21d9cbf
Showing 1 changed file with 3 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public sealed class CdpCryptor : IDisposable

readonly Aes _ivAes;
readonly Aes _aes;
readonly HMACSHA256 _hmac;
readonly ReadOnlyMemory<byte> _hmac;
public CdpCryptor(byte[] sharedSecret)
{
_aes = Aes.Create();
Expand All @@ -22,7 +22,7 @@ public CdpCryptor(byte[] sharedSecret)
_ivAes = Aes.Create();
_ivAes.Key = sharedSecret[16..32];

_hmac = new(sharedSecret[^32..^0]);
_hmac = sharedSecret.AsMemory()[^32..^0];
}

void GenerateIV(CommonHeader header, Span<byte> destination)
Expand All @@ -43,7 +43,7 @@ void ComputeHmac(ReadOnlySpan<byte> buffer, Span<byte> destination)
{
Debug.Assert(destination.Length == Constants.HMacSize);

var isSuccess = _hmac.TryComputeHash(buffer, destination, out var bytesWritten);
var isSuccess = HMACSHA256.TryHashData(_hmac.Span, buffer, destination, out var bytesWritten);

Debug.Assert(isSuccess);
Debug.Assert(bytesWritten == destination.Length);
Expand Down Expand Up @@ -160,6 +160,5 @@ public void Dispose()
{
_ivAes.Dispose();
_aes.Dispose();
_hmac.Dispose();
}
}

0 comments on commit 21d9cbf

Please sign in to comment.