-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
106 additions
and
23 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
namespace Libplanet.Crypto | ||
{ | ||
/// <summary> | ||
/// Libplanet cryptography configuration information. | ||
/// </summary> | ||
public static class CryptoConfig | ||
{ | ||
private static ICryptoBackend _cryptoBackend; | ||
|
||
/// <summary> | ||
/// Global cryptography backend to sign and verify messages. | ||
/// </summary> | ||
public static ICryptoBackend CryptoBackend | ||
{ | ||
get => _cryptoBackend ?? (_cryptoBackend = new DefaultCryptoBackend()); | ||
set => _cryptoBackend = value; | ||
} | ||
} | ||
} |
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,35 @@ | ||
using System.IO; | ||
using System.Security.Cryptography; | ||
using Org.BouncyCastle.Asn1; | ||
using Org.BouncyCastle.Crypto.Signers; | ||
|
||
namespace Libplanet.Crypto | ||
{ | ||
public class DefaultCryptoBackend : ICryptoBackend | ||
{ | ||
public bool Verify( | ||
HashDigest<SHA256> messageHash, | ||
byte[] signature, | ||
PublicKey publicKey) | ||
{ | ||
try | ||
{ | ||
Asn1Sequence asn1Sequence = (Asn1Sequence)Asn1Object.FromByteArray(signature); | ||
|
||
var rs = new[] | ||
{ | ||
((DerInteger)asn1Sequence[0]).Value, | ||
((DerInteger)asn1Sequence[1]).Value, | ||
}; | ||
var verifier = new ECDsaSigner(); | ||
verifier.Init(false, publicKey.KeyParam); | ||
|
||
return verifier.VerifySignature(messageHash.ToByteArray(), rs[0], rs[1]); | ||
} | ||
catch (IOException) | ||
{ | ||
return false; | ||
} | ||
} | ||
} | ||
} |
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,26 @@ | ||
using System.Security.Cryptography; | ||
|
||
namespace Libplanet.Crypto | ||
{ | ||
/// <summary> | ||
/// Cryptography backend interface. | ||
/// </summary> | ||
public interface ICryptoBackend | ||
{ | ||
/// <summary> | ||
/// Verifies whether a <paramref name="signature"/> was created from | ||
/// a <paramref name="messageHash"/> with the corresponding <see cref="PrivateKey"/>. | ||
/// </summary> | ||
/// <param name="messageHash">A 32 bytes message hash digest hashed with SHA256.</param> | ||
/// <param name="signature">A signature that was created from the | ||
/// <paramref name="messageHash"/>.</param> | ||
/// <param name="publicKey"><see cref="PublicKey"/> used for verification.</param> | ||
/// <returns><c>true</c> if the <paramref name="signature"/> was created | ||
/// from the <paramref name="messageHash"/> with the corresponding | ||
/// <see cref="PrivateKey"/>. Otherwise <c>false</c>.</returns> | ||
bool Verify( | ||
HashDigest<SHA256> messageHash, | ||
byte[] signature, | ||
PublicKey publicKey); | ||
} | ||
} |
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