Skip to content

Commit 1a58ec0

Browse files
committed
Script to calculate KCV
1 parent 6cca0dd commit 1a58ec0

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

tools/hmac/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,13 @@ npm list @adyen/api-library // check version of library and update if needed
3232
3333
node calculateHmacPlatform.js 11223344D785FBAE710E7F943F307971BB61B21281C98C9129B3D4018A57B2EB payload2.json
3434
```
35+
36+
### Calculate KCV of the HMAC key
37+
38+
Run `calculateKcv` passing the HMAC key:
39+
`node calculateKcv.js {hmacKey}`
40+
```
41+
cd tools/hmac
42+
43+
node calculateKcv.js 00727DB817A85C8503AD29EAD1523DB869AF0E536893BB3046C92DE7CB045CB1
44+
```

tools/hmac/calculateKcv.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// script to calculate the KVC of the HMAC key
2+
//
3+
// Run with: `node calculateKcv.js {hmacKey}
4+
//
5+
// cd tools/hmac
6+
// node calculateKcv.js 00727DB817A85C8503AD29EAD1523DB869AF0E536893BB3046C92DE7CB045CB1 should return 9540DA
7+
const crypto = require('crypto');
8+
9+
// Ensure correct arguments
10+
if (process.argv.length !== 3) {
11+
console.error("Usage: node calculateKcv.js <base64HmacKey>");
12+
process.exit(1);
13+
}
14+
15+
console.log(`Calculating KCV...`);
16+
17+
const hmacKey = process.argv[2];
18+
// Convert the hex stringc
19+
const keyBuffer = Buffer.from(hmacKey, 'hex');
20+
21+
const algorithm = 'sha256';
22+
23+
const hmacSignature = crypto.createHmac(algorithm, keyBuffer).update("00000000").digest();
24+
25+
// Take the last 3 bytes (6 hex characters) for the KCV
26+
const kcv = Buffer.from(hmacSignature.subarray(hmacSignature.length - 3)).toString('hex').toUpperCase();
27+
28+
console.log('********');
29+
console.log(`KCV: ${kcv}`);

0 commit comments

Comments
 (0)