-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
233f932
commit 400ff37
Showing
4 changed files
with
181 additions
and
92 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
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,27 @@ | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
|
||
namespace Pandatech.Crypto; | ||
|
||
public static class Sha2 | ||
{ | ||
public static byte[] ComputeHmacSha256(byte[] key, params string[] messages) | ||
{ | ||
using var hmac = new HMACSHA256(key); | ||
|
||
var concatenatedMessage = Encoding.UTF8.GetBytes(string.Concat(messages)); | ||
return hmac.ComputeHash(concatenatedMessage); | ||
} | ||
|
||
public static string GetHmacSha256Hex(byte[] key, params string[] messages) | ||
{ | ||
var hash = ComputeHmacSha256(key, messages); | ||
return BitConverter.ToString(hash).Replace("-", "").ToLower(); | ||
} | ||
|
||
public static string GetHmacSha256Base64(byte[] key, params string[] messages) | ||
{ | ||
var hash = ComputeHmacSha256(key, messages); | ||
return Convert.ToBase64String(hash); | ||
} | ||
} |
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,64 @@ | ||
namespace Pandatech.Crypto.Tests; | ||
|
||
public class Sha2Tests | ||
{ | ||
[Fact] | ||
public void HmacSha256_ValidInput_ReturnsExpectedHash() | ||
{ | ||
// Arrange | ||
var key = "secret"u8.ToArray(); | ||
var messages = new[] { "Hello", "World" }; | ||
const string expectedHashHex = "2e91612bb72b29d82f32789d063de62d5897a4ee5d3b5d34459801b94397b099"; | ||
|
||
// Act | ||
var hashHex = Sha2.GetHmacSha256Hex(key, messages); | ||
|
||
// Assert | ||
Assert.Equal(expectedHashHex, hashHex); | ||
} | ||
|
||
[Fact] | ||
public void HmacSha256_EmptyMessage_ReturnsHash() | ||
{ | ||
// Arrange | ||
var key = "secret"u8.ToArray(); | ||
var messages = Array.Empty<string>(); | ||
|
||
// Act | ||
var hash = Sha2.ComputeHmacSha256(key, messages); | ||
|
||
// Assert | ||
Assert.NotNull(hash); | ||
Assert.NotEmpty(hash); | ||
} | ||
|
||
[Fact] | ||
public void HmacSha256_ConsistentOutput_ForSameInputs() | ||
{ | ||
// Arrange | ||
var key = "secret"u8.ToArray(); | ||
var messages = new[] { "Test", "Message" }; | ||
|
||
// Act | ||
var hash1 = Sha2.GetHmacSha256Hex(key, messages); | ||
var hash2 = Sha2.GetHmacSha256Hex(key, messages); | ||
|
||
// Assert | ||
Assert.Equal(hash1, hash2); | ||
} | ||
|
||
[Fact] | ||
public void HmacSha256Base64_ValidInput_ReturnsExpectedBase64() | ||
{ | ||
// Arrange | ||
var key = "secret"u8.ToArray(); | ||
var messages = new[] { "Hello", "World" }; | ||
const string expectedBase64 = "LpFhK7crKdgvMnidBj3mLViXpO5dO100RZgBuUOXsJk="; | ||
|
||
// Act | ||
var base64Hash = Sha2.GetHmacSha256Base64(key, messages); | ||
|
||
// Assert | ||
Assert.Equal(expectedBase64, base64Hash); | ||
} | ||
} |