Skip to content

Commit 8b1e49d

Browse files
committed
Fix HMACSHA512 for NONATIVEHASH && HAS_SPAN
1 parent eca59f5 commit 8b1e49d

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

NBitcoin/Crypto/Hashes.cs

+10-7
Original file line numberDiff line numberDiff line change
@@ -853,14 +853,17 @@ public static byte[] HMACSHA512(byte[] key, byte[] data)
853853
#if HAS_SPAN
854854
public static bool HMACSHA512(byte[] key, ReadOnlySpan<byte> data, Span<byte> output, out int outputLength)
855855
{
856-
var outputBuffer = HMACSHA512(key, data.ToArray());
857-
outputLength = output.Length;
858-
859-
if (outputLength > output.Length)
856+
outputLength = 0;
857+
var mac = new NBitcoin.BouncyCastle.Crypto.Macs.HMac(new Sha512Digest());
858+
var macSize = mac.GetMacSize();
859+
if (output.Length < macSize)
860860
return false;
861-
862-
outputBuffer.CopyTo(output);
863-
return true;
861+
mac.Init(new KeyParameter(key));
862+
mac.BlockUpdate(data.ToArray(), 0, data.Length);
863+
byte[] result = new byte[macSize];
864+
mac.DoFinal(result, 0);
865+
result.CopyTo(result);
866+
outputLength = result.Length;
864867
}
865868
#endif
866869

0 commit comments

Comments
 (0)