如何为datatrans生成HMAC-SHA-256登录javascript?

如何为datatrans生成HMAC-SHA-256登录javascript?,javascript,angular,sha256,cryptojs,Javascript,Angular,Sha256,Cryptojs,我有一个角度的项目,我必须实施付款。但我无法生成付款签名 我遵循这个链接()上给出的过程来生成签名 但我无法实现它 我使用angular library crypto js生成HMAC-SHA-256签名字符串 这是我的javascript代码 const merchantId = 'xxxxxxx'; const refNo = '1234567890'; const amount = 0; const currency = 'CHF'; const theme = 'DT2015'; con

我有一个角度的项目,我必须实施付款。但我无法生成付款签名

我遵循这个链接()上给出的过程来生成签名

但我无法实现它

我使用angular library 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);
你能帮我解释一下我做错了什么,或者用什么方法来实现它吗。

你可以用crypto来实现(不需要额外的库来安装)

您可以使用crypto(无需安装额外的库)完成此操作

// 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');
}
// Javascript
const crypto = require('crypto')

function signKey (clientKey, msg) {
    const key = new Buffer(clientKey, 'hex');
    return crypto.createHmac('sha256', key).update(msg).digest('hex');
}
signKey(s, stringSs)