-
Notifications
You must be signed in to change notification settings - Fork 3
/
hkdf.js
31 lines (28 loc) · 960 Bytes
/
hkdf.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
const common = require('./common')
module.exports = {
/**
* Derive a cryptographically strong set of values using the contraction and
* expansion method of HKDF.
*
* @param {string} ikm - The initial key material. A shared secret or some
* other random value you know about.
* @param {number} size - The number of bytes to derive.
* @param {string} salt - Additional entropy.
* @param {string} info - Additional entropy to bind the bytes derived to a
* specific entity.
* @returns {Promise}
*/
derive: (ikm, size, salt = null, info = null) => {
const hkdf = require('futoin-hkdf')
return new Promise(async (resolve, reject) => {
if (salt === null || salt === void 0) {
salt = await common.random(32)
}
try {
return resolve(hkdf(ikm, size, salt, info, 'SHA-512'))
} catch (err) {
return reject(err)
}
})
}
}