-
Notifications
You must be signed in to change notification settings - Fork 60
/
async.js
98 lines (95 loc) · 2.62 KB
/
async.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
var checkParameters = require('./precondition')
var defaultEncoding = require('./default-encoding')
var sync = require('./sync')
var Buffer = require('safe-buffer').Buffer
var ZERO_BUF
var subtle = global.crypto && global.crypto.subtle
var toBrowser = {
'sha': 'SHA-1',
'sha-1': 'SHA-1',
'sha1': 'SHA-1',
'sha256': 'SHA-256',
'sha-256': 'SHA-256',
'sha384': 'SHA-384',
'sha-384': 'SHA-384',
'sha-512': 'SHA-512',
'sha512': 'SHA-512'
}
var checks = []
function checkNative (algo) {
if (global.process && !global.process.browser) {
return Promise.resolve(false)
}
if (!subtle || !subtle.importKey || !subtle.deriveBits) {
return Promise.resolve(false)
}
if (checks[algo] !== undefined) {
return checks[algo]
}
ZERO_BUF = ZERO_BUF || Buffer.alloc(8)
var prom = browserPbkdf2(ZERO_BUF, ZERO_BUF, 10, 128, algo)
.then(function () {
return true
}).catch(function () {
return false
})
checks[algo] = prom
return prom
}
function browserPbkdf2 (password, salt, iterations, length, algo) {
return subtle.importKey(
'raw', password, {name: 'PBKDF2'}, false, ['deriveBits']
).then(function (key) {
return subtle.deriveBits({
name: 'PBKDF2',
salt: salt,
iterations: iterations,
hash: {
name: algo
}
}, key, length << 3)
}).then(function (res) {
return Buffer.from(res)
})
}
function resolvePromise (promise, callback) {
promise.then(function (out) {
process.nextTick(function () {
callback(null, out)
})
}, function (e) {
process.nextTick(function () {
callback(e)
})
})
}
module.exports = function (password, salt, iterations, keylen, digest, callback) {
if (!Buffer.isBuffer(password)) password = Buffer.from(password, defaultEncoding)
if (!Buffer.isBuffer(salt)) salt = Buffer.from(salt, defaultEncoding)
checkParameters(iterations, keylen)
if (typeof digest === 'function') {
callback = digest
digest = undefined
}
if (typeof callback !== 'function') throw new Error('No callback provided to pbkdf2')
digest = digest || 'sha1'
var algo = toBrowser[digest.toLowerCase()]
if (!algo || typeof global.Promise !== 'function') {
return process.nextTick(function () {
var out
try {
out = sync(password, salt, iterations, keylen, digest)
} catch (e) {
return callback(e)
}
callback(null, out)
})
}
resolvePromise(checkNative(algo).then(function (resp) {
if (resp) {
return browserPbkdf2(password, salt, iterations, keylen, algo)
} else {
return sync(password, salt, iterations, keylen, digest)
}
}), callback)
}