How to use crypto-js

Comprehensive crypto-js code examples:

How to use crypto-js.createVerify:

171
172
173
174
175
176
177
178
179
180
// console.log('verify', input, key, method, type, signature );
if(type === "hmac") {
  return (signature === sign(input, key, method, type));
}
else if(type == "sign") {
  return crypto.createVerify(method)
               .update(input)
               .verify(key, base64urlUnescape(signature), 'base64');
}
else {

How to use crypto-js.createSign:

187
188
189
190
191
192
193
194
195
196
if(type === "hmac") {
  hmac = cjshmac[method](input, key);
  base64str = crypto.enc.Base64.stringify(hmac)
}
else if(type == "sign") {
  base64str = crypto.createSign(method).update(input).sign(key, 'base64');
}
else {
  throw new Error('Algorithm type not recognized');
}

How to use crypto-js.createHash:

110
111
112
113
114
115
116
117
118

function hash(object, options) {
  var hashingStream;

  if (options.algorithm !== 'passthrough') {
    hashingStream = crypto.createHash(options.algorithm);
  } else {
    hashingStream = new PassThrough();
  }

How to use crypto-js.ECBEncrypt:

4
5
6
7
8
9
10
11
12
13
const Encrypt = require('jsencrypt');

/**
 *  3DES 加解密
    // 加密
    let a = CryptoJS.ECBEncrypt('123456');
    console.log('加密',a);
    // 解密
    let b = CryptoJS.ECBDecrypt(a);
    console.log('解密', b);

How to use crypto-js.ECBDecrypt:

7
8
9
10
11
12
13
14
15
16
 *  3DES 加解密
    // 加密
    let a = CryptoJS.ECBEncrypt('123456');
    console.log('加密',a);
    // 解密
    let b = CryptoJS.ECBDecrypt(a);
    console.log('解密', b);
 */
const key = CryptoJS.enc.Utf8.parse('123456789012345678901234');
const aseKey = '757da2be61249c188319a9269f1a6ccb';

How to use crypto-js.RabbitLegacy:

113
114
115
116
117
118
119
120
121
122
},
rabbit() {
  return CryptoJS.Rabbit.encrypt(this.text, this.serect).toString();
},
rabbitlegacy() {
  return CryptoJS.RabbitLegacy.encrypt(this.text, this.serect).toString();
},
base64() {
  let words = CryptoJS.enc.Utf8.parse(this.text);
  return CryptoJS.enc.Base64.stringify(words);

How to use crypto-js.Rabbit:

110
111
112
113
114
115
116
117
118
119
},
rc4() {
  return CryptoJS.RC4.encrypt(this.text, this.serect).toString();
},
rabbit() {
  return CryptoJS.Rabbit.encrypt(this.text, this.serect).toString();
},
rabbitlegacy() {
  return CryptoJS.RabbitLegacy.encrypt(this.text, this.serect).toString();
},

How to use crypto-js.HmacRIPEMD160:

98
99
100
101
102
103
104
105
106
107
},
hmacsha3() {
  return CryptoJS.HmacSHA3(this.text, this.serect).toString();
},
hmacripemd160() {
  return CryptoJS.HmacRIPEMD160(this.text, this.serect).toString();
},
aes() {
  return CryptoJS.AES.encrypt(this.text, this.serect).toString();
},

How to use crypto-js.RC4:

107
108
109
110
111
112
113
114
115
116
},
tripledes() {
  return CryptoJS.TripleDES.encrypt(this.text, this.serect).toString();
},
rc4() {
  return CryptoJS.RC4.encrypt(this.text, this.serect).toString();
},
rabbit() {
  return CryptoJS.Rabbit.encrypt(this.text, this.serect).toString();
},

How to use crypto-js.HmacSHA224:

83
84
85
86
87
88
89
90
91
92
},
hmacsha1() {
  return CryptoJS.HmacSHA1(this.text, this.serect).toString();
},
hmacsha224() {
  return CryptoJS.HmacSHA224(this.text, this.serect).toString();
},
hmacsha256() {
  return CryptoJS.HmacSHA256(this.text, this.serect).toString();
},

How to use crypto-js.HmacSHA3:

95
96
97
98
99
100
101
102
103
104
},
hmacsha512() {
  return CryptoJS.HmacSHA512(this.text, this.serect).toString();
},
hmacsha3() {
  return CryptoJS.HmacSHA3(this.text, this.serect).toString();
},
hmacripemd160() {
  return CryptoJS.HmacRIPEMD160(this.text, this.serect).toString();
},

How to use crypto-js.PBKDF2:

242
243
244
245
246
247
248
249
250
251

function pbkdf2Encrypt() {
    var text = "I love Python!"
    var salt = "43215678"
    // key 长度 128,10 次重复运算
    var encryptedData = CryptoJS.PBKDF2(text, salt, {keySize: 128/32,iterations: 10});
    return encryptedData.toString()
}

console.log(pbkdf2Encrypt())  // 7fee6e8350cfe96314c76aaa6e853a50

How to use crypto-js.DES:

6
7
8
9
10
11
12
13
14
15
//required
//<script src="../js/rollups/tripledes.js"></script>
//<script src="../js/components/mode-ecb-min.js"></script>
var key = 'Pog4iu6OqIkKRpDT';
var keyHex = CryptoJS.enc.Utf8.parse(key);
var encrypted = CryptoJS.DES.encrypt(message, keyHex, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
});
// console.log('直接 toString' + encrypted.toString()) // 对加密结果直接 toString 就是 base64

How to use crypto-js.HmacSHA512:

35
36
37
38
39
40
41
42
43
44
};

var cjshmac = {
  sha256: crypto.HmacSHA256,
  sha384: crypto.HmacSHA384,
  sha512: crypto.HmacSHA512
}


/**

How to use crypto-js.SHA384:

120
121
122
123
124
125
126
127
128
129

SHA512: function(data, secret) {
  return CryptoJS.SHA512(secret + data);
},
SHA384: function(data, secret) {
  return CryptoJS.SHA384(secret + data);
},
SHA256: function(data, secret) {
  return CryptoJS.SHA256(secret + data);
},

How to use crypto-js.SHA224:

126
127
128
129
130
131
132
133
134
135
},
SHA256: function(data, secret) {
  return CryptoJS.SHA256(secret + data);
},
SHA224: function(data, secret) {
  return CryptoJS.SHA224(secret + data);
},

SHA3: function(data, secret) {
  return Telepathy.algorithms.SHA3_512(data, secret);

How to use crypto-js.SHA512:

117
118
119
120
121
122
123
124
125
126
  const random = '5gkjB6SpmC9s'
  token = `tk01wcdf61cb3a8nYUtHcmhSUFFCfddDPRvKvYaMjHkxo6Aj7dhzO+GXGFa9nPXfcgT+mULoF1b1YIS1ghvSlbwhE0Xc`
  fingerprint = 9686767825751161
  // $.fingerprint = 7811850938414161
  const str = `${token}${fingerprint}${timestamp}${appId}${random}`
  hash1 = CryptoJS.SHA512(str, token).toString(CryptoJS.enc.Hex)
}
let st: string = ''
stk.split(',').map((item, index) => {
  st += `${item}:${getQueryString(url, item)}${index === stk.split(',').length - 1 ? '' : '&'}`

How to use crypto-js.RIPEMD160:

24
25
26
27
28
29
30
31
32
33
function ripemd160(str) {
  return CryptoJS.RIPEMD160(str).toString();
}

function ripemd160Hex(hexStr) {
  return CryptoJS.RIPEMD160(cryptoHexStringParse(hexStr)).toString();
}

module.exports = {
  sha256,

How to use crypto-js.HmacMD5:

328
329
330
331
332
333
334
335
336
var CryptoJS = require('crypto-js')

function HMACEncrypt() {
    var text = "I love python!"
    var key = "secret"
    return CryptoJS.HmacMD5(text, key).toString();
    // return CryptoJS.HmacSHA1(text, key).toString();
    // return CryptoJS.HmacSHA256(text, key).toString();
}

How to use crypto-js.HmacSHA384:

8
9
10
11
12
13
14
15
16
17
}

get(action, callback, data = {}) {
    let dataJSON = JSON.stringify(data);

    let sign = CryptoJS.HmacSHA384(dataJSON, this.privateKey);
    sign = CryptoJS.enc.Base64.stringify(sign);

    request.post(this.apiUrl + action, {
        headers: {

How to use crypto-js.SHA1:

282
283
284
285
286
287
288
289
290
291
// 引用 crypto-js 加密模块
var CryptoJS = require('crypto-js')

function SHA1Encrypt() {
    var text = "I love python!"
    return CryptoJS.SHA1(text).toString();
}

console.log(SHA1Encrypt())  // 23c02b203bd2e2ca19da911f1d270a06d86719fb
```

How to use crypto-js.TripleDES:

0
1
2
3
4
5
6
7
8
9
var CryptoJS = require('crypto-js')

function I(X, l) {
    var C = X
        , q = CryptoJS.enc.Utf8.parse(C)
        , v = CryptoJS.TripleDES.decrypt(l, q, {
            'mode': CryptoJS.mode.ECB,
            'padding': CryptoJS.pad.Pkcs7
        });
    return v.toString(CryptoJS.enc.Utf8);

How to use crypto-js.HmacSHA1:

1
2
3
4
5
6
7
8
9
10
const os = require('os');
const axios = require('axios');
const qs = require('qs');

function hmacSign(type, secret, content) {
  return cryptojs.HmacSHA1(content,secret).toString();
}

function mqttMatch(filter, topic) {
  const filterArray = filter.split('/')

How to use crypto-js.SHA3:

136
137
138
139
140
141
142
143
144
145
},
SHA3_512: function(data, secret) {
  return CryptoJS.SHA3(secret + data, { outputLength: 512 });
},
SHA3_384: function(data, secret) {
  return CryptoJS.SHA3(secret + data, { outputLength: 384 });
},
SHA3_256: function(data, secret) {
  return CryptoJS.SHA3(secret + data, { outputLength: 256 });
},

How to use crypto-js.HmacSHA256:

330
331
332
333
334
335
336
337
338
339
function HMACEncrypt() {
    var text = "I love python!"
    var key = "secret"
    return CryptoJS.HmacMD5(text, key).toString();
    // return CryptoJS.HmacSHA1(text, key).toString();
    // return CryptoJS.HmacSHA256(text, key).toString();
}

console.log(HMACEncrypt())
```

How to use crypto-js.algo:

115
116
117
118
119
120
121
122
123
124

/*
 * @since crypto 1.1.2
 */
this.CRYPTOJSMESSAGEDIGESTNAME = {
    'md5':          CryptoJS.algo.MD5,
    'sha1':         CryptoJS.algo.SHA1,
    'sha224':       CryptoJS.algo.SHA224,
    'sha256':       CryptoJS.algo.SHA256,
    'sha384':       CryptoJS.algo.SHA384,

How to use crypto-js.lib:

83
84
85
86
87
88
89
90
91
92
}

function ripemd160(data) {
    let temp = data;
    if (!Buffer.isBuffer(data) && data instanceof Uint8Array) {
        temp = CryptoJS.lib.WordArray.create(temp);
    } else if (Buffer.isBuffer(data)) {
        temp = CryptoJS.lib.WordArray.create(new Uint8Array(temp));
    }
    

How to use crypto-js.pad:

26
27
28
29
30
31
32
33
34
35
encrypt: function(b, c, a) {
    if (c) {
        return (CryptoJS.TripleDES.encrypt(b, CryptoJS.enc.Utf8.parse(c), {
            iv: CryptoJS.enc.Utf8.parse(a || DES3.iv()),
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        })).toString()
    }
    return ""
},

How to use crypto-js.mode:

25
26
27
28
29
30
31
32
33
34
},
encrypt: function(b, c, a) {
    if (c) {
        return (CryptoJS.TripleDES.encrypt(b, CryptoJS.enc.Utf8.parse(c), {
            iv: CryptoJS.enc.Utf8.parse(a || DES3.iv()),
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        })).toString()
    }
    return ""