Skip to content

Commit

Permalink
Properly dispose of managed digest instances.
Browse files Browse the repository at this point in the history
  • Loading branch information
JVanloofsvelt committed Feb 1, 2021
1 parent e62a152 commit e9f4bce
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
12 changes: 9 additions & 3 deletions NBitcoin/BIP39/Mnemonic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,17 @@ public byte[] DeriveSeed(string passphrase = null)
#if NO_NATIVE_HMACSHA512
#if NONATIVEHASH
var mac = new NBitcoin.BouncyCastle.Crypto.Macs.HMac(new NBitcoin.BouncyCastle.Crypto.Digests.Sha512Digest());
#else
var mac = new NBitcoin.BouncyCastle.Crypto.Macs.HMac(new Crypto.digests.ManagedSha512Digest());
#endif
mac.Init(new NBitcoin.BouncyCastle.Crypto.Parameters.KeyParameter(bytes));
return Pbkdf2.ComputeDerivedKey(mac, salt, 2048, 64);
#else
using (var sha512 = new Crypto.NativeDigests.ManagedSha512Digest())
{
var mac = new NBitcoin.BouncyCastle.Crypto.Macs.HMac(sha512);
mac.Init(new NBitcoin.BouncyCastle.Crypto.Parameters.KeyParameter(bytes));
return Pbkdf2.ComputeDerivedKey(mac, salt, 2048, 64);
}
#endif

#elif NO_NATIVE_RFC2898_HMACSHA512
return NBitcoin.Crypto.Pbkdf2.ComputeDerivedKey(new System.Security.Cryptography.HMACSHA512(bytes), salt, 2048, 64);
#else
Expand Down
30 changes: 18 additions & 12 deletions NBitcoin/Crypto/Hashes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -835,12 +835,15 @@ public static byte[] HMACSHA256(byte[] key, byte[] data)
#elif NO_NATIVE_HMACSHA512 // There is a native hash, but no native HMAC.
public static byte[] HMACSHA512(byte[] key, byte[] data)
{
var mac = new NBitcoin.BouncyCastle.Crypto.Macs.HMac(new ManagedSha512Digest());
mac.Init(new KeyParameter(key));
mac.BlockUpdate(data, 0, data.Length);
byte[] result = new byte[mac.GetMacSize()];
mac.DoFinal(result, 0);
return result;
using (var sha512 = new ManagedSha512Digest())
{
var mac = new NBitcoin.BouncyCastle.Crypto.Macs.HMac(sha512);
mac.Init(new KeyParameter(key));
mac.BlockUpdate(data, 0, data.Length);
byte[] result = new byte[mac.GetMacSize()];
mac.DoFinal(result, 0);
return result;
}
}

#if HAS_SPAN
Expand All @@ -859,12 +862,15 @@ public static bool HMACSHA512(byte[] key, ReadOnlySpan<byte> data, Span<byte> ou

public static byte[] HMACSHA256(byte[] key, byte[] data)
{
var mac = new NBitcoin.BouncyCastle.Crypto.Macs.HMac(new ManagedSha256Digest());
mac.Init(new KeyParameter(key));
mac.BlockUpdate(data, 0, data.Length);
byte[] result = new byte[mac.GetMacSize()];
mac.DoFinal(result, 0);
return result;
using (var sha256 = new ManagedSha256Digest())
{
var mac = new NBitcoin.BouncyCastle.Crypto.Macs.HMac(sha256);
mac.Init(new KeyParameter(key));
mac.BlockUpdate(data, 0, data.Length);
byte[] result = new byte[mac.GetMacSize()];
mac.DoFinal(result, 0);
return result;
}
}
#else // There is no native hash and no native HMAC
public static byte[] HMACSHA512(byte[] key, byte[] data)
Expand Down

0 comments on commit e9f4bce

Please sign in to comment.