如何解决如何为数据传输生成HMAC-SHA-256登录JavaScript?
我有一个Angular项目,我必须在其中执行datatrans付款。但是我无法生成付款标志。
我正在遵循此链接(enter link description here)上给出的过程来生成符号。
但是我无法实现。
我正在使用角度库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);
您能帮助我提出建议吗,或者我可以通过什么方式实现这一目标。
解决方法
您可以使用加密来实现(无需安装额外的库)
// 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,key).update(msg).digest('hex');
}
signKey(s,stringSs)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。