-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
23 lines (18 loc) · 1.02 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const crypto = require('crypto')
function isArrayBufferOrTypedArray (input) {
return (typeof input.byteLength === 'number' && typeof input.slice === 'function')
}
module.exports = function pbkdf2 (password, salt, iterations, keylen, digest) {
if (!isArrayBufferOrTypedArray(password)) throw new TypeError('Expected "password" to be an ArrayBuffer, Uint8Array or Buffer')
if (!isArrayBufferOrTypedArray(salt)) throw new TypeError('Expected "salt" to be an ArrayBuffer, Uint8Array or Buffer')
if (digest !== 'SHA-256' && digest !== 'SHA-384' && digest !== 'SHA-512') throw new TypeError('Expected "digest" to be one of "SHA-256", "SHA-384" or "SHA-512"')
password = Buffer.from(password)
salt = Buffer.from(salt)
digest = digest.replace('SHA-', 'sha')
return new Promise(function (resolve, reject) {
crypto.pbkdf2(password, salt, iterations, keylen, digest, function (err, result) {
if (err) return reject(err)
resolve(result.buffer.slice(result.byteOffset, result.byteOffset + result.byteLength))
})
})
}