Nin*_*tle 2 javascript sha256 cryptojs angular
我有一个 Angular 项目,我必须在其中实现数据传输付款。但我无法生成付款标志。
我正在按照此链接(在此处输入链接描述)给出的流程来生成标志。
但我无法实现它。
我正在使用 Angular 库 crypto-js 生成 HMAC-SHA-256 签名字符串。
这是我的 JavaScript 代码。
const merchantId = 'xxxxxxx';
const refNo = '1234567890';
const amount = 0;
const currency = 'CHF';
const theme = 'DT2015';
const paymentmethod = 'VIS';
const stringSs = merchantId+amount+currency+refNo;
const base = 16;
// My Hmac Key
const s = 'fa3d0ea1772cf21e53158283e4f123ebf1eb1ccfb15619e2fc91ee6860a2e5e48409e902b610ce5dc6f7f77fab8affb60d69b2a7aa9acf56723d868d36ab3f32';
// Step 1: Code to generate hex to byte of hmac key
const a = s.replace(/../g, '$&_').slice (0, -1).split ('_').map ((x) => parseInt (x, base));
// Step 3: Sign the string with HMAC-SHA-256 together with your HMAC key
const signedString = HmacSHA256(a, stringSs);
// Step 4: Translate the signature from byte to hex format
const signString = enc.Hex.stringify(signedString);
Run Code Online (Sandbox Code Playgroud)
你能帮我解决这个问题,建议我做错了什么或者以什么方式可以实现它。
小智 7
您可以使用加密来完成(无需安装额外的库)
// Typescript
import * as crypto from 'crypto';
function signKey (clientKey: string, msg: string) {
const key = new Buffer(clientKey, 'hex');
return crypto.createHmac('sha256', key).update(msg).digest('hex');
}
Run Code Online (Sandbox Code Playgroud)
// Javascript
const crypto = require('crypto')
function signKey (clientKey, msg) {
const key = new Buffer(clientKey, 'hex');
return crypto.createHmac('sha256', key).update(msg).digest('hex');
}
Run Code Online (Sandbox Code Playgroud)
signKey(s, stringSs)
Run Code Online (Sandbox Code Playgroud)