7
7
// node calculateHmacPlatform.js 11223344D785FBAE710E7F943F307971BB61B21281C98C9129B3D4018A57B2EB payload2.json
8
8
9
9
const fs = require ( 'fs' ) ;
10
- const crypto = require ( 'crypto' ) ;
10
+ const { hmacValidator } = require ( '@adyen/api-library' ) ;
11
+
11
12
12
13
// Ensure correct arguments
13
14
if ( process . argv . length !== 4 ) {
@@ -24,26 +25,33 @@ if (!fs.existsSync(payloadFile)) {
24
25
process . exit ( 1 ) ;
25
26
}
26
27
27
- console . log ( `Calculating HMAC signature with payload from ' ${ payloadFile } ' ` ) ;
28
+ console . log ( `Calculating HMAC signature... ` ) ;
28
29
29
30
// Load payload as raw string
30
31
let payload ;
32
+
31
33
try {
32
34
payload = fs . readFileSync ( payloadFile , 'utf8' ) ;
33
35
} catch ( error ) {
34
36
console . error ( `Error reading file: ${ error . message } ` ) ;
35
37
process . exit ( 1 ) ;
36
38
}
37
39
38
- // Calculate HMAC signature
39
- function calculateHmacSignature ( hmacKey , payload ) {
40
- const key = Buffer . from ( hmacKey , 'hex' ) ;
41
- const hmac = crypto . createHmac ( 'sha256' , key ) ;
42
- hmac . update ( payload , 'utf8' ) ;
43
- return hmac . digest ( 'base64' ) ;
44
- }
40
+ const validator = new hmacValidator ( )
41
+ const hmacSignature = validator . calculateHmac ( payload , hmacKey ) ;
42
+
43
+ console . log ( '********' ) ;
44
+ console . log ( `Payload file: ${ payloadFile } ` ) ;
45
+ console . log ( `Payload length: ${ payload . length } characters` ) ;
46
+ /*
47
+ // uncomment if needed
48
+ const newlineCount = (payload.match(/\n/g) || []).length;
49
+ const spaceCount = (payload.match(/ /g) || []).length;
45
50
46
- const signature = calculateHmacSignature ( hmacKey , payload ) ;
51
+ console.log(`Newline count: ${newlineCount}`);
52
+ console.log(`Space count: ${spaceCount}`);
53
+ */
54
+ console . log ( '********' ) ;
47
55
48
- console . log ( `HMAC signature: ${ signature } ` ) ;
56
+ console . log ( `HMAC signature: ${ hmacSignature } ` ) ;
49
57
process . exit ( 0 ) ;
0 commit comments