forked from krharsh17/rapyd-api-signatures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsign-node.js
23 lines (18 loc) · 846 Bytes
/
sign-node.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const crypto = require('crypto');
function generateSignature(httpMethod, urlPath, salt, timestamp, accessKey, secretKey, body) {
try {
let bodyString = "";
if (body) {
bodyString = JSON.stringify(body);
bodyString = bodyString == "{}" ? "" : bodyString; // If body was empty, the body string should also be empty (and not curly braces)
}
let toSign = httpMethod.toLowerCase() + urlPath + salt + timestamp + accessKey + secretKey + bodyString;
let hash = crypto.createHmac('sha256', secretKey);
hash.update(toSign);
const signature = Buffer.from(hash.digest("hex")).toString("base64")
return signature;
} catch (error) {
console.error("An error occurred when generating the signature");
throw error;
}
}