@znode/sign
Version:
signature algorithm
36 lines (35 loc) • 1.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.sign = void 0;
const hmac_1 = require("@zodash/hmac");
const qs = require("@zcorky/query-string");
function sign(data, secret) {
// 1. alphabet order
const _query = !data.query
? {}
: Object.keys(data.query)
.sort((a, b) => a.localeCompare(b))
.reduce((all, key) => {
all[key] = data.query[key];
return all;
}, {});
const _body = !data.body
? {}
: Object.keys(data.body)
.sort((a, b) => a.localeCompare(b))
.reduce((all, key) => {
all[key] = data.body[key];
return all;
}, {});
// 2. concat
const $query = qs.stringify(_query);
const $body = qs.stringify(_body);
//
const qst = [$query, $body].filter((e) => !!e).join('&');
// 3. with timestamp
const text = `${qst}|${data.timestamp}`;
// console.log('text:', text);
return hmac_1.hmacSHA256(text, secret);
}
exports.sign = sign;
exports.default = sign;