How to use the HmacSHA1 function from crypto-js

Find comprehensive JavaScript crypto-js.HmacSHA1 code examples handpicked from public code repositorys.

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('/')
fork icon42
star icon110
watch icon10

329
330
331
332
333
334
335
336
337
338

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())
fork icon34
star icon57
watch icon0

+ 7 other calls in file

55
56
57
58
59
60
61
62
63
64
const uri = url.replace(/^.*\/\/[^\/]+/, '')
const timestamp = moment().toISOString()

// create the signature
const signature = [method, contentMd5, contentType, timestamp].join(',\n')
const encryptedSignature = publicKey + ':' + CryptoJS.HmacSHA1(signature, privateKey).toString(CryptoJS.enc.Base64)

// set or update the results as environment variables, to be used in the HTTP request.
process.env.TIMESTAMP = timestamp
process.env.ENCRYPTED_SIGNATURE = encryptedSignature
fork icon30
star icon44
watch icon14

+ 3 other calls in file

132
133
134
135
136
137
138
139
140
  let hash;
  if (algo === 'md5') {
    hash = CryptoJS.HmacMD5(msg_words, key_words)
    return wa2ba(hash.words)
  } else if (algo === 'sha1') {
    hash = CryptoJS.HmacSHA1(msg_words, key_words)
    return wa2ba(hash.words)
  }
}
fork icon21
star icon30
watch icon9

+ 3 other calls in file

23
24
25
26
27
28
29
30
31
Cryptographic functions are available from the CryptJS libaray at [https://code.google.com/p/crypto-js/](https://code.google.com/p/crypto-js/).

```javascript
var stringToSign = “string to sign”;
var secret = “my secret key”;
var signature = CryptoJS.HmacSHA1(stringToSign, secret);
```

## NodeJS
fork icon17
star icon2
watch icon4

+ 3 other calls in file

10
11
12
13
14
15
16
17
18

  var check = signed_query.queryJson
    + ":" + signed_query.userGuid
    + ((signed_query.orgGuid == "00000000-0000-0000-0000-000000000000") ? "" : (":" + signed_query.orgGuid))
    + ":" + signed_query.expiresAt;
  signed_query.digest = CryptoJS.HmacSHA1(check, CryptoJS.enc.Base64.parse(apiKey)).toString(CryptoJS.enc.Base64);

  return signed_query;
}
fork icon2
star icon4
watch icon1

52
53
54
55
56
57
58
59
60
61
const [clientId, random] = [`${deviceName}.${productKey}`, Date.now()];

const content = `deviceName${
  deviceName
}productKey${productKey}random${random}`;
const signedContent = cryptojs.HmacSHA1(content,productSecret).toString();
// const device = mqtt.connect(`wss://${productKey}.iot-as-mqtt.cn-shanghai.aliyuncs.com:443`,{
//   clientId: `${clientId}|securemode=-2,authType=regnwl,random=${random},signmethod=hmacsha1|`,
//   username: `${deviceName}&${productKey}`,
//   password: signedContent,
fork icon1
star icon19
watch icon2

16
17
18
19
20
21
22
23
24
25
request和setRequest配合着一起用就可以在请求发送前对其进行适当的编辑,比如增加一个签名,增加一个header之类。

```javascript
const crypto = hitchhiker.require('crypto-js');

const sign = crypto.HmacSHA1('test', 'asdf');

const req = hitchhiker.request;
url = `${url}?sign=${sign}`;
hitchhiker.setRequest({...hitchhiker.request, url});
fork icon2
star icon18
watch icon2

80
81
82
83
84
85
86
87
88
89
},
hmacmd5() {
  return CryptoJS.HmacMD5(this.text, this.serect).toString();
},
hmacsha1() {
  return CryptoJS.HmacSHA1(this.text, this.serect).toString();
},
hmacsha224() {
  return CryptoJS.HmacSHA224(this.text, this.serect).toString();
},
fork icon0
star icon1
watch icon1

+ 17 other calls in file

46
47
48
49
50
51
52
53
54
  let URL = path
  if (!/^http.*/.test(URL)) URL = BASE_URL + path

  return xFetch(URL, {
    headers: {
      'WECHATLOOK-HEADER': Crypto.HmacSHA1(URL, 'register.wechatlook_rn.js')
    }
  })
}
fork icon302
star icon0
watch icon59

87
88
89
90
91
92
93
94
95
96
};

/* SHA1 hashing */
exports.SHA1 = function(text, key) {
    try {
        return typeof key !== undefined ? CryptoJS.HmacSHA1(text, key) : CryptoJS.SHA1(text);
    }
    catch (err) {
        throw "ERROR - utils/utilidades.js _ SHA1 method";
    }
fork icon1
star icon0
watch icon2

+ 3 other calls in file