我面对的是fiserv-ucom所有api的401,我不知道HMAC签名出了什么问题



我正在尝试在节点js中制作HMAC签名。正如文件中提到的那样,我做得很好。但无法创建签名。请找到下面的代码,

let computedHash = CryptoJS.algo.HMAC.create(
CryptoJS.algo.SHA256,
key
);
computedHash.update(rawSignature);
computedHash = computedHash.finalize();
const computedHmac = CryptoJS.enc.Base64.stringify(computedHash);

由于密钥和rawSignature是"base64编码的";你需要";转换";在使用之前

也许这次演示会对有所帮助

const expectedOutput = btoa( "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843".replace(/../g, (hex) => String.fromCharCode(parseInt(hex, 16))));
console.log("Test vector result should be");
console.log(expectedOutput);
(() => {
// mock inputs as regular strings
const key = "Jefe"
const rawSignature = "what do ya want for nothing?"

// --- your code  
let computedHash = CryptoJS.algo.HMAC.create(
CryptoJS.algo.SHA256,
key
);
computedHash.update(rawSignature);
computedHash = computedHash.finalize();
let computedHmac = CryptoJS.enc.Base64.stringify(computedHash);
console.log("inputs are strings");
console.log(computedHmac);
})();
(() => {
// mock inputs as base64 encoded strings
const key = btoa("Jefe");
const rawSignature = btoa("what do ya want for nothing?");

// --- your code  
let computedHash = CryptoJS.algo.HMAC.create(
CryptoJS.algo.SHA256,
key
);
computedHash.update(rawSignature);
computedHash = computedHash.finalize();
computedHmac = CryptoJS.enc.Base64.stringify(computedHash);
console.log("inputs are base64 encoded - not decoding them where they are used");
console.log(computedHmac);
})();
(() => {
// mock inputs as base64 encoded strings
const key = btoa("Jefe");
const rawSignature = btoa("what do ya want for nothing?");

// --- your code  
let computedHash = CryptoJS.algo.HMAC.create(
CryptoJS.algo.SHA256,
CryptoJS.enc.Utf8.stringify(CryptoJS.enc.Base64.parse(key))
);
computedHash.update(CryptoJS.enc.Utf8.stringify(CryptoJS.enc.Base64.parse(rawSignature)));
computedHash = computedHash.finalize();
computedHmac = CryptoJS.enc.Base64.stringify(computedHash);
console.log("inputs are base64 encoded - decoding them where they are used");
console.log(computedHmac);
})();
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script>

最新更新