angular6引入CryptoJS,使用HmacSHA1加密

1.npm安装CryptoJS

npm install crypto-js

2.在项目中引用

import CryptoJS from 'crypto-js'  //注意,没有{ }

getSign() {
    var data = new Date().getTime()
    var value = "123456";
    var key = "GBSJSNKKM@hsdjdkmmkklcld"
    var sha1_result = CryptoJS.HmacSHA1(value, key).toString(CryptoJS.enc.Hex);
    return sha1_result;
}

也可以在项目中如下引用

import sha256 from 'crypto-js/sha256';
import hmacSHA512 from 'crypto-js/hmac-sha512';
import Base64 from 'crypto-js/enc-base64';

const message, nonce, path, privateKey; // ...
const hashDigest = sha256(nonce + message);
const hmacDigest = Base64.stringify(hmacSHA512(path + hashDigest, privateKey));

你可能感兴趣的:(javascript,前端,angular,加密)