-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
180 lines (156 loc) · 5.52 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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
const {randomBytes, timingSafeEqual} = require('crypto')
const argon2PackagePath = require.resolve('argon2/package.json').replace('/package.json', '')
const gypBuild = require('module').createRequire(argon2PackagePath)('node-gyp-build')
const {hash: bindingsHash} = gypBuild(argon2PackagePath)
const generateSalt = require('util').promisify(randomBytes)
const VERSION = 0x13
const types = Object.freeze({
'0': 0,
'1': 1,
'2': 2,
argon2d: 0,
argon2i: 1,
argon2id: 2
})
const names = Object.freeze({
[types.argon2d]: 'argon2d',
[types.argon2i]: 'argon2i',
[types.argon2id]: 'argon2id',
})
const limits = Object.freeze({
hashLength: { max: 4294967295, min: 4 },
memoryCost: { max: 4294967295, min: 2048 },
timeCost: { max: 4294967295, min: 2 },
parallelism: { max: 16777215, min: 1 },
passwordLength: { min: 0, max: 4294967295 }
})
// Attention, the old secure-password had different options
// timeCost=2, hashLength=32, memoryCost=65536
// memoryCost is now also in kilobytes instead of bytes
const defaults = Object.freeze({
hashLength: 32,
saltLength: 16,
timeCost: 3,
memoryCost: 65536,
parallelism: 1,
type: types.argon2id,
version: VERSION
})
const VALID = Symbol('VALID')
const INVALID = Symbol('INVALID')
const VALID_NEEDS_REHASH = Symbol('VALID_NEEDS_REHASH')
const INVALID_UNRECOGNIZED_HASH = Symbol('INVALID_UNRECOGNIZED_HASH')
securePassword.limits = limits
securePassword.defaults = defaults
securePassword.INVALID_UNRECOGNIZED_HASH = INVALID_UNRECOGNIZED_HASH
securePassword.INVALID = INVALID
securePassword.VALID = VALID
securePassword.VALID_NEEDS_REHASH = VALID_NEEDS_REHASH
securePassword.securePassword = securePassword
class AssertionError extends Error {}
AssertionError.prototype.name = 'AssertionError'
function assert (t, m) {
if (t) return
const err = new AssertionError(m)
Error.captureStackTrace(err, assert)
throw err
}
function assertBetween (value, {min, max}, key) {
if (value >= min && value <= max) return
const err = new AssertionError(`${key} (${value}), must be between ${min} and ${max}`)
Error.captureStackTrace(err, assertBetween)
throw err
}
const {serialize, deserialize: _phcDeserialize} = require('@phc/format')
// Removes trailing null bytes from a buffer and deserializes it
function deserialize (hashBuf) {
try {
const i = hashBuf.indexOf(0x00)
if (i !== -1) hashBuf = hashBuf.slice(0, i)
return _phcDeserialize(hashBuf.toString())
} catch (err) {
return
}
}
function needsRehash (deserializedHash, {version, memoryCost, timeCost}) {
const {version: v, params: {m, t}} = deserializedHash
return +v !== version || +m !== memoryCost || +t !== timeCost
}
function recognizedAlgorithm (deserializedHash) {
if (!deserializedHash) return false
return types[deserializedHash.id] !== undefined
}
async function argon2Verify (deserializedHash, passwordBuf, secret) {
const {id, version = 0x10, params: {m, t, p, data = ''}, salt, hash} = deserializedHash
return timingSafeEqual(
await bindingsHash({
password: passwordBuf,
salt,
secret,
data: Buffer.from(data, 'base64'),
hashLength: hash.byteLength,
m: +m,
t: +t,
p: +p,
version: +version,
type: types[id],
}),
hash
)
}
function securePassword (opts = {}) {
const options = Object.freeze({...defaults, ...opts})
const nullBuffer = Buffer.alloc(0)
const secret = options.secret ? Buffer.from(options.secret) : nullBuffer
const type = opts.type !== undefined ? types[opts.type] : defaults.type
assert(type, 'Invalid type, must be one of argon2d, argon2i or argon2id')
const serializeOpts = Object.freeze({
id: names[type],
version: VERSION,
params: {
m: options.memoryCost,
t: options.timeCost,
p: options.parallelism
}
})
assertBetween(options.hashLength, limits.hashLength, 'Invalid options.hashLength')
assertBetween(options.memoryCost, limits.memoryCost, 'Invalid options.memoryCost')
assertBetween(options.timeCost, limits.timeCost, 'Invalid options.timeCost')
assertBetween(options.parallelism, limits.parallelism, 'Invalid options.parallelism')
async function hash (passwordBuf) {
assert(passwordBuf instanceof Uint8Array, 'Invalid passwordBuf, must be Buffer or Uint8Array')
assertBetween(passwordBuf.length, limits.passwordLength, 'Invalid passwordBuf length')
const salt = await generateSalt(options.saltLength)
const hash = await bindingsHash({
password: passwordBuf,
salt,
secret,
data: nullBuffer,
hashLength: options.hashLength,
m: options.memoryCost,
t: options.timeCost,
p: options.parallelism,
version: options.version,
type: options.type,
})
return Buffer.from(serialize({
id: serializeOpts.id,
version: serializeOpts.version,
params: serializeOpts.params,
salt,
hash
}))
}
async function verify (passwordBuf, hashBuf) {
assert(passwordBuf instanceof Uint8Array, 'Invalid passwordBuf, must be Buffer or Uint8Array')
assert(hashBuf instanceof Uint8Array, 'Invalid hashBuf, must be Buffer or Uint8Array')
assertBetween(passwordBuf.length, limits.passwordLength, 'Invalid passwordBuf')
const deserializedHash = deserialize(hashBuf)
if (recognizedAlgorithm(deserializedHash) === false) return INVALID_UNRECOGNIZED_HASH
if (await argon2Verify(deserializedHash, passwordBuf, secret) === false) return INVALID
if (needsRehash(deserializedHash, options)) return VALID_NEEDS_REHASH
return VALID
}
return {hash, verify}
}
module.exports = securePassword