-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbrowser.js
22 lines (17 loc) · 1.22 KB
/
browser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/* global crypto */
function isArrayBufferOrTypedArray (input) {
return (typeof input.byteLength === 'number' && typeof input.slice === 'function')
}
function isTypedArray (input) {
return (typeof input.byteOffset === 'number' && typeof input.buffer === 'object')
}
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"')
if (isTypedArray(password)) password = password.buffer.slice(password.byteOffset, password.byteOffset + password.byteLength)
if (isTypedArray(salt)) salt = salt.buffer.slice(salt.byteOffset, salt.byteOffset + salt.byteLength)
return Promise.resolve()
.then(() => crypto.subtle.importKey('raw', password, { name: 'PBKDF2' }, false, ['deriveBits']))
.then(key => crypto.subtle.deriveBits({ name: 'PBKDF2', salt, iterations, hash: { name: digest } }, key, keylen << 3))
}