@@ -839,12 +839,15 @@ public static byte[] HMACSHA256(byte[] key, byte[] data)
839
839
#elif NO_NATIVE_HMACSHA512 // There is a native hash, but no native HMAC.
840
840
public static byte [ ] HMACSHA512 ( byte [ ] key , byte [ ] data )
841
841
{
842
- var mac = new NBitcoin . BouncyCastle . Crypto . Macs . HMac ( new ManagedSha512Digest ( ) ) ;
843
- mac . Init ( new KeyParameter ( key ) ) ;
844
- mac . BlockUpdate ( data , 0 , data . Length ) ;
845
- byte [ ] result = new byte [ mac . GetMacSize ( ) ] ;
846
- mac . DoFinal ( result , 0 ) ;
847
- return result ;
842
+ using ( var sha512 = new ManagedSha512Digest ( ) )
843
+ {
844
+ var mac = new NBitcoin . BouncyCastle . Crypto . Macs . HMac ( sha512 ) ;
845
+ mac . Init ( new KeyParameter ( key ) ) ;
846
+ mac . BlockUpdate ( data , 0 , data . Length ) ;
847
+ byte [ ] result = new byte [ mac . GetMacSize ( ) ] ;
848
+ mac . DoFinal ( result , 0 ) ;
849
+ return result ;
850
+ }
848
851
}
849
852
850
853
#if HAS_SPAN
@@ -863,12 +866,15 @@ public static bool HMACSHA512(byte[] key, ReadOnlySpan<byte> data, Span<byte> ou
863
866
864
867
public static byte [ ] HMACSHA256 ( byte [ ] key , byte [ ] data )
865
868
{
866
- var mac = new NBitcoin . BouncyCastle . Crypto . Macs . HMac ( new ManagedSha256Digest ( ) ) ;
867
- mac . Init ( new KeyParameter ( key ) ) ;
868
- mac . BlockUpdate ( data , 0 , data . Length ) ;
869
- byte [ ] result = new byte [ mac . GetMacSize ( ) ] ;
870
- mac . DoFinal ( result , 0 ) ;
871
- return result ;
869
+ using ( var sha256 = new ManagedSha256Digest ( ) )
870
+ {
871
+ var mac = new NBitcoin . BouncyCastle . Crypto . Macs . HMac ( sha256 ) ;
872
+ mac . Init ( new KeyParameter ( key ) ) ;
873
+ mac . BlockUpdate ( data , 0 , data . Length ) ;
874
+ byte [ ] result = new byte [ mac . GetMacSize ( ) ] ;
875
+ mac . DoFinal ( result , 0 ) ;
876
+ return result ;
877
+ }
872
878
}
873
879
#else // There is no native hash and no native HMAC
874
880
public static byte [ ] HMACSHA512 ( byte [ ] key , byte [ ] data )
0 commit comments