-
Notifications
You must be signed in to change notification settings - Fork 849
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adapt Hashes.HMACSHA256() so that it uses native hashes when they are… #965
Open
JVanloofsvelt
wants to merge
4
commits into
MetacoSA:master
Choose a base branch
from
JVanloofsvelt:feature/use-native-hash-with-BC-HMac
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7101bd1
Adapt Hashes.HMACSHA256() so that it uses native hashes when they are…
JVanloofsvelt e8b64ad
Fix using statements according to target platform.
JVanloofsvelt eca59f5
Properly dispose of managed digest instances.
JVanloofsvelt 8b1e49d
Fix HMACSHA512 for NONATIVEHASH && HAS_SPAN
JVanloofsvelt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using NBitcoin.BouncyCastle.Crypto; | ||
using System; | ||
|
||
namespace NBitcoin.Crypto.NativeDigests | ||
{ | ||
#if !NETSTANDARD1X && !NONATIVEHASH | ||
|
||
using System.Security.Cryptography; | ||
|
||
/// <summary> | ||
/// A wrapper around the native SHA256, implements BouncyCastle's IDigest interface in order | ||
/// to be compatible with BouncyCastle's HMac implementation. | ||
/// </summary> | ||
internal class ManagedSha256Digest : IDigest, IDisposable | ||
{ | ||
private const int DigestLength = 32; | ||
SHA256Managed nativeSha256 = null; | ||
byte[] input; | ||
|
||
public ManagedSha256Digest() | ||
{ | ||
Reset(); | ||
} | ||
|
||
public string AlgorithmName => "SHA-256"; | ||
|
||
public void BlockUpdate(byte[] input, int inOff, int length) | ||
{ | ||
this.input = new byte[length]; | ||
Array.Copy(input, inOff, this.input, 0, length); | ||
} | ||
|
||
public int DoFinal(byte[] output, int outOff) | ||
{ | ||
var hash = nativeSha256.ComputeHash(input, 0, input.Length); | ||
Array.Copy(hash, 0, output, outOff, hash.Length); | ||
Reset(); | ||
return DigestLength; | ||
} | ||
|
||
public int GetByteLength() => 64; | ||
|
||
public int GetDigestSize() => DigestLength; | ||
|
||
public void Reset() | ||
{ | ||
nativeSha256?.Dispose(); | ||
nativeSha256 = new SHA256Managed(); | ||
input = null; | ||
} | ||
|
||
public void Update(byte input) | ||
{ | ||
throw new NotImplementedException("Code monkey didn't expect you would need this."); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
nativeSha256?.Dispose(); | ||
} | ||
} | ||
#endif | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using NBitcoin.BouncyCastle.Crypto; | ||
using System; | ||
|
||
namespace NBitcoin.Crypto.NativeDigests | ||
{ | ||
#if !NETSTANDARD1X && !NONATIVEHASH | ||
using System.Security.Cryptography; | ||
|
||
/// <summary> | ||
/// A wrapper around the native SHA512, implements BouncyCastle's IDigest interface in order | ||
/// to be compatible with BouncyCastle's HMac implementation. | ||
/// </summary> | ||
internal class ManagedSha512Digest : IDigest, IDisposable | ||
{ | ||
private const int DigestLength = 64; | ||
SHA512Managed nativeSha512 = null; | ||
byte[] input; | ||
|
||
public ManagedSha512Digest() | ||
{ | ||
Reset(); | ||
} | ||
|
||
public string AlgorithmName => "SHA-512"; | ||
|
||
public void BlockUpdate(byte[] input, int inOff, int length) | ||
{ | ||
this.input = new byte[length]; | ||
Array.Copy(input, inOff, this.input, 0, length); | ||
} | ||
|
||
public int DoFinal(byte[] output, int outOff) | ||
{ | ||
var hash = nativeSha512.ComputeHash(input, 0, input.Length); | ||
Array.Copy(hash, 0, output, outOff, hash.Length); | ||
Reset(); | ||
return DigestLength; | ||
} | ||
|
||
public int GetByteLength() => 64; | ||
|
||
public int GetDigestSize() => DigestLength; | ||
|
||
public void Reset() | ||
{ | ||
nativeSha512?.Dispose(); | ||
nativeSha512 = new SHA512Managed(); | ||
input = null; | ||
} | ||
|
||
public void Update(byte input) | ||
{ | ||
throw new NotImplementedException("Code monkey didn't expect you would need this."); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
nativeSha512?.Dispose(); | ||
} | ||
} | ||
#endif | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you instead rely on NBitcoin.Secp256k1.SHA256 class which properly implement DoFinal and BlockUpdate and is widely tested and memory efficient?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, I'll take care of it tonight.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@NicolasDorier I found the NBitcoin.Secpk1.SHA class, but it doesn't look like it implements the IDigest interface, nor the BlockUpdate/DoFinal methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not saying to use it instead of ManagedSha256Digest, but to use it rather than that
nativeSha256
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello @NicolasDorier, I understand now. There is no NBitcoin.Secp512k1.SHA512 equivalent in the Secpk1 project though (to go along with the SHA256). Besides, wouldn't it be great if NBitcoin could rely on the framework's builtins?
It's not clear to me if DoFinal() and BlockUpdate() are BouncyCastle-specific interface methods, or if they are a more wider known concept. I'm not sure how to implement them correctly. I was hoping someone here would review it, and fill in the gaps for me.