2 回答

TA貢獻(xiàn)1874條經(jīng)驗(yàn) 獲得超12個(gè)贊
你可以用加密來做到這一點(diǎn)(不需要安裝額外的庫(kù))
// 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)

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊
要按要求回答 crypto-js 的問題(請(qǐng)參閱https://github.com/brix/crypto-js),以下內(nèi)容可以解決問題:
// Javascript; example from datatrans documentation using a random key
stringSs ='3000017692850CHF91827364';
key='1ca12d7c0629194a9f9d0dbbc957709dd3aed385925b077e726813f0b452de6a38256abd1116138d21754cfb33964b6b1aaa375b74d3580fcda916898f553c92';
expectedSign='d7dee9ae1e542bc02bcb063a3dd3673871b2e43ccb4c230f26e8b85d14e25901';
signedString = CryptoJS.HmacSHA256(stringSs, CryptoJS.enc.Hex.parse(key));
resultSign = CryptoJS.enc.Hex.stringify(signedString);
// now resultSign == expectedSign is true :-)
忍者神龜?shù)姆椒◣缀跏钦_的,除了第 1 步,十六進(jìn)制到字節(jié)。改用 Crypto-JS 的內(nèi)置函數(shù),一切都按預(yù)期工作。
添加回答
舉報(bào)