如何在 Postman 预请求脚本中将 cryptoJS.sha256 输出设置为二进制
How do I set cryptoJS.sha256 output to binary in Postman pre-request script
我正在尝试使用预请求脚本在 Postman 中创建 HMAC 签名。无需深入了解实施细节,
我已经确认我生成签名的方法有问题。我可以通过概念验证示例看到预期结果应该是什么,但我在某处遗漏了一些东西,无法判断它是否在转换中。我已经阅读了关于 的其他问题,简单地调用散列等同于请求为您完成转换的摘要。这是我在 postman 中尝试 运行 的代码和 nodeJS 中所示的工作实现代码。
var CryptoJS = require("crypto-js");
const d = new Date();
const timestamp = d.getTime();
const postData = {};
postData.nonce = 100; //timestamp * 1000; //nanosecond
postman.setEnvironmentVariable('nonce', postData.nonce);
const secret = CryptoJS.enc.Base64.parse(pm.environment.get("apiSecret"));
const path = pm.globals.get("balanceMethod");
const message = CryptoJS.SHA256( encodeURI(postData.nonce + postData)) ; // ...
const hmacDigest = CryptoJS.HmacSHA512(path + message, secret);
postman.setEnvironmentVariable('API-Signature', CryptoJS.enc.Base64.stringify(hmacDigest));
console.log(CryptoJS.enc.Base64.stringify(hmacDigest));
构建与 nodeJS 一起工作的实现的参考代码:
const getMessageSignature = (path, request, secret, nonce) => {
const message = qs.stringify(request);
const secret_buffer = new Buffer(secret, 'base64');
const hash = new crypto.createHash('sha256');
const hmac = new crypto.createHmac('sha512', secret_buffer);
const hash_digest = hash.update(nonce + message).digest('binary');
const hmac_digest = hmac.update(path + hash_digest, 'binary').digest('base64');
return hmac_digest;
};
在python3中构建实现的相同参考代码:
req['nonce'] = 100 #int(1000*time.time())
postdata = urllib.parse.urlencode(req)
# Unicode-objects must be encoded before hashing
encoded = (str(req['nonce']) + postdata).encode()
message = urlpath.encode() + hashlib.sha256(encoded).digest()
signature = hmac.new(base64.b64decode(self.secret),
message, hashlib.sha512)
sigdigest = base64.b64encode(signature.digest())
我发送的唯一 post 数据是 Nonce,我特意将其设置为 100 以便能够复制结果以修复生成的签名。似乎接近但不匹配的结果。 python 和 nodeJS 确实符合预期结果并正常工作。
查看 线程中的答案。它帮助我解决了我的问题,也可能是您的情况。只需将 HMAC 的输入分成两部分即可。
我正在尝试使用预请求脚本在 Postman 中创建 HMAC 签名。无需深入了解实施细节,
我已经确认我生成签名的方法有问题。我可以通过概念验证示例看到预期结果应该是什么,但我在某处遗漏了一些东西,无法判断它是否在转换中。我已经阅读了关于
var CryptoJS = require("crypto-js");
const d = new Date();
const timestamp = d.getTime();
const postData = {};
postData.nonce = 100; //timestamp * 1000; //nanosecond
postman.setEnvironmentVariable('nonce', postData.nonce);
const secret = CryptoJS.enc.Base64.parse(pm.environment.get("apiSecret"));
const path = pm.globals.get("balanceMethod");
const message = CryptoJS.SHA256( encodeURI(postData.nonce + postData)) ; // ...
const hmacDigest = CryptoJS.HmacSHA512(path + message, secret);
postman.setEnvironmentVariable('API-Signature', CryptoJS.enc.Base64.stringify(hmacDigest));
console.log(CryptoJS.enc.Base64.stringify(hmacDigest));
构建与 nodeJS 一起工作的实现的参考代码:
const getMessageSignature = (path, request, secret, nonce) => {
const message = qs.stringify(request);
const secret_buffer = new Buffer(secret, 'base64');
const hash = new crypto.createHash('sha256');
const hmac = new crypto.createHmac('sha512', secret_buffer);
const hash_digest = hash.update(nonce + message).digest('binary');
const hmac_digest = hmac.update(path + hash_digest, 'binary').digest('base64');
return hmac_digest;
};
在python3中构建实现的相同参考代码:
req['nonce'] = 100 #int(1000*time.time())
postdata = urllib.parse.urlencode(req)
# Unicode-objects must be encoded before hashing
encoded = (str(req['nonce']) + postdata).encode()
message = urlpath.encode() + hashlib.sha256(encoded).digest()
signature = hmac.new(base64.b64decode(self.secret),
message, hashlib.sha512)
sigdigest = base64.b64encode(signature.digest())
我发送的唯一 post 数据是 Nonce,我特意将其设置为 100 以便能够复制结果以修复生成的签名。似乎接近但不匹配的结果。 python 和 nodeJS 确实符合预期结果并正常工作。
查看