-
Notifications
You must be signed in to change notification settings - Fork 3
/
hmac.js
49 lines (45 loc) · 1.13 KB
/
hmac.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const crypto = require('crypto')
/**
*
* @param digest
* @param text
* @param secret
* @returns {Promise<any>}
*/
const hmac = (digest, text, secret) => {
return new Promise((resolve, reject) => {
return resolve(crypto.createHmac(digest, secret).update(text).digest())
})
}
module.exports = {
/**
* Compute an HMAC using SHA-256 of a given string.
*
* @param {string} text - The text to calculate into a hash.
* @param {string} secret - The shared secret.
* @returns {Promise}
*/
hmac256: (text, secret) => {
return hmac('sha256', text, secret)
},
/**
* Compute an HMAC using SHA-384 of a given string.
*
* @param {string} text - The text to calculate into a hash.
* @param {string} secret - The shared secret.
* @returns {Promise}
*/
hmac384: (text, secret) => {
return hmac('sha384', text, secret)
},
/**
* Compute an HMAC using SHA-512 of a given string.
*
* @param {string} text - The text to calculate into a hash.
* @param {string} secret - The shared secret.
* @returns {Promise}
*/
hmac512: (text, secret) => {
return hmac('sha512', text, secret)
}
}