Skip to content
This repository has been archived by the owner on Nov 25, 2021. It is now read-only.

added JS example of signed POST data payload, clarified functions. #118

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions encryption_example/Encryption.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,45 @@ var params = {
"api_key" : apiKey,
};

console.log(getSignature(params, secret));
var signature = getSignature(params, secret);

console.log("signature of params: ");
console.log(signature);
console.log();

console.log("ordered params - querystring: ");
console.log(getQueryStringSigned(params, secret));
console.log();

console.log("ordered params - serialized to JSON for POST: ");
console.log(getPostDataSigned(params, secret));
console.log();

function getQueryStringSigned(parameters, secret) {
const sign = getSignature(parameters, secret);
const qs = getOrderedParams(params, secret) + "&sign=" + signature
return qs;
}

function getPostDataSigned(parameters, secret) {
const sign = getSignature(parameters, secret);

parameters.sign = sign;

return parameters;
}

function getSignature(parameters, secret) {
var orderedParams = "";
let orderedParams = getOrderedParams(parameters);
return crypto.createHmac('sha256', secret).update(orderedParams).digest('hex');
}

function getOrderedParams(parameters) {
let orderedParams = "";
Object.keys(parameters).sort().forEach(function(key) {
orderedParams += key + "=" + parameters[key] + "&";
});
orderedParams = orderedParams.substring(0, orderedParams.length - 1);

return crypto.createHmac('sha256', secret).update(orderedParams).digest('hex');
return orderedParams;
}