1+ using System . Text ;
12using Adyen . Core . Utilities ;
23using Microsoft . VisualStudio . TestTools . UnitTesting ;
34
@@ -6,6 +7,67 @@ namespace Adyen.Test.Core.Utilities
67 [ TestClass ]
78 public class HmacValidatorUtilityTest
89 {
10+ private const string _targetPayload = "{\" test\" :\" value\" }" ;
11+ private const string _hmacKey = "00112233445566778899AABBCCDDEEFF" ;
12+
13+ /// <summary>
14+ /// Helper function that computes expected HMAC.
15+ /// </summary>
16+ private string ComputeExpectedHmac ( string payload , string hexKey )
17+ {
18+ byte [ ] key = new byte [ hexKey . Length / 2 ] ;
19+ for ( int i = 0 ; i < hexKey . Length ; i += 2 )
20+ {
21+ key [ i / 2 ] = Convert . ToByte ( hexKey . Substring ( i , 2 ) , 16 ) ;
22+ }
23+
24+ byte [ ] data = Encoding . UTF8 . GetBytes ( payload ) ;
25+ using var hmac = new System . Security . Cryptography . HMACSHA256 ( key ) ;
26+ var raw = hmac . ComputeHash ( data ) ;
27+ return Convert . ToBase64String ( raw ) ;
28+ }
29+
30+
31+ [ TestMethod ]
32+ public void Given_GenerateBase64Sha256HmacSignature_When_Hmac_Provided_Returns_Correct_Signature ( )
33+ {
34+ // Arrange
35+ string expected = ComputeExpectedHmac ( _targetPayload , _hmacKey ) ;
36+
37+ // Act
38+ string actual = HmacValidatorUtility . GenerateBase64Sha256HmacSignature ( _targetPayload , _hmacKey ) ;
39+
40+ // Assert
41+ Assert . AreEqual ( expected , actual ) ;
42+ }
43+
44+ [ TestMethod ]
45+ public void Given_GenerateBase64Sha256HmacSignature__When_Odd_Length_HmacKey_Returns_Correct_Signature ( )
46+ {
47+ // Arrange
48+ string paddedKey = "ABC0" ; // Expected padded version
49+ string expected = ComputeExpectedHmac ( _targetPayload , paddedKey ) ;
50+
51+ // Act
52+ string actual = HmacValidatorUtility . GenerateBase64Sha256HmacSignature ( _targetPayload , "ABC" ) ; // Will be padded to "ABC0"
53+
54+ // Assert
55+ Assert . AreEqual ( expected , actual ) ;
56+ }
57+
58+
59+ [ TestMethod ]
60+ public void Given_IsHmacSignatureValid_When_Signature_Is_NotEqual_Returns_False ( )
61+ {
62+ // Arrange
63+ string invalidSignature = "InvalidSignature123==" ;
64+
65+ // Act
66+ // Assert
67+ Assert . IsFalse ( HmacValidatorUtility . IsHmacSignatureValid ( invalidSignature , _hmacKey , _targetPayload ) ) ;
68+ }
69+
70+
971 [ TestMethod ]
1072 public void TestBalancePlatformHmac ( )
1173 {
@@ -23,7 +85,7 @@ public void TestBalancePlatformHmac()
2385 }
2486
2587 [ TestMethod ]
26- public void GenerateBase64Sha256HmacSignature_ReturnsCorrectSignature ( )
88+ public void Given_GenerateBase64Sha256HmacSignature_When_BalancePlatform_Webhook_Returns_Correct_Signature ( )
2789 {
2890 // Arrange
2991 string notification =
0 commit comments