中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
加解密HmacSha1 、HmacSha256、aes参考Demo
微信扫码分享
// 可理解的字符串转成字节流 function stringToUint8Array(str: string) { let arr = new Uint8Array(str.length); for (let i = 0, j = str.length; i < j; ++i) { arr[i] = str.charCodeAt(i); } return arr; } async function getHmac(message: string) { try { let macAlgName = 'SHA1'; let mac = cryptoFramework.createMac(macAlgName) let arr = stringToUint8Array('30a86dc9056c44cc05420fec269270214bbb6914954e871e83771c9810ac1db0') let KeyBlob: cryptoFramework.DataBlob = { data: arr }; let symKeyGenerator = cryptoFramework.createSymKeyGenerator('HMAC'); const symKey = await symKeyGenerator.convertKey(KeyBlob); await mac.init(symKey) await mac.update({ data: stringToUint8Array(message) }); let macOutpt = await mac.doFinal(); const res = buffer.from(macOutpt.data).toString('hex'); console.log('Hmac—:' + res); } catch (err) { console.log('err:' + err) } }