Skip to content

Commit 69ea33c

Browse files
committed
Refactor to use library function
1 parent 8ada552 commit 69ea33c

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

tools/hmac/calculateHmacPlatform.js

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
// node calculateHmacPlatform.js 11223344D785FBAE710E7F943F307971BB61B21281C98C9129B3D4018A57B2EB payload2.json
88

99
const fs = require('fs');
10-
const crypto = require('crypto');
10+
const { hmacValidator } = require('@adyen/api-library');
11+
1112

1213
// Ensure correct arguments
1314
if (process.argv.length !== 4) {
@@ -24,26 +25,33 @@ if (!fs.existsSync(payloadFile)) {
2425
process.exit(1);
2526
}
2627

27-
console.log(`Calculating HMAC signature with payload from '${payloadFile}'`);
28+
console.log(`Calculating HMAC signature...`);
2829

2930
// Load payload as raw string
3031
let payload;
32+
3133
try {
3234
payload = fs.readFileSync(payloadFile, 'utf8');
3335
} catch (error) {
3436
console.error(`Error reading file: ${error.message}`);
3537
process.exit(1);
3638
}
3739

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;
4550
46-
const signature = calculateHmacSignature(hmacKey, payload);
51+
console.log(`Newline count: ${newlineCount}`);
52+
console.log(`Space count: ${spaceCount}`);
53+
*/
54+
console.log('********');
4755

48-
console.log(`HMAC signature: ${signature}`);
56+
console.log(`HMAC signature: ${hmacSignature}`);
4957
process.exit(0);

0 commit comments

Comments
 (0)