-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
171 lines (137 loc) · 4.44 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
var assert = require('nanoassert')
const b4a = require('b4a')
var wasm = require('./argon2')({
imports: {
debug: {
log (...args) {
console.log(...args.map(int => (int >>> 0).toString(16).padStart(8, '0')))
},
log_tee (arg) {
console.log((arg >>> 0).toString(16).padStart(8, '0'))
return arg
}
}
}
})
const TYPES = ['d', 'i', 'id']
// var head = 64
var freeList = []
module.exports = argon2
module.exports.ARGON2I = 1
module.exports.ARGON2ID = 2
module.exports.verify = verify
module.exports.needsRehash = needsRehash
var ARGON2_VERSION = 0x13
var BYTES_MIN = module.exports.BYTES_MIN = 16
var BYTES_MAX = module.exports.BYTES_MAX = 64
var BYTES = module.exports.BYTES = 32
var KEYBYTES_MIN = module.exports.KEYBYTES_MIN = 16
var KEYBYTES_MAX = module.exports.KEYBYTES_MAX = 64
var KEYBYTES = module.exports.KEYBYTES = 32
var SALTBYTES = module.exports.SALTBYTES = 16
var PERSONALBYTES = module.exports.PERSONALBYTES = 16
function argon2 (input, nonce, key, ad, enc, opts = {}) {
if (typeof enc === 'object') return argon2(input, nonce, key, ad, 'argon_string', enc)
if (!opts.passes) opts.passes = 1024
if (!opts.outlen) opts.outlen = 1024
if (!opts.memory) opts.memory = 8192
if (key == null) key = b4a.alloc(0)
if (ad == null) ad = b4a.alloc(0)
let head = 8192
writeUInt32LE(input.byteLength, wasm.memory, head)
wasm.memory.set(input, head + 4)
head += input.byteLength + 4
writeUInt32LE(nonce.byteLength, wasm.memory, head)
wasm.memory.set(nonce, head + 4)
head += nonce.byteLength + 4
writeUInt32LE(key.byteLength, wasm.memory, head)
wasm.memory.set(key, head + 4)
head += key.byteLength + 4
writeUInt32LE(ad.byteLength, wasm.memory, head)
wasm.memory.set(ad, head + 4)
head += ad.byteLength + 4
wasm.exports.argon2_init(0, opts.memory, opts.outlen, opts.passes, opts.type)
wasm.exports.argon2_hash(0, 8192, head)
const buf = wasm.memory.slice(8192, 8192 + opts.outlen)
if (enc === 'binary') return buf
if (enc === 'argon_string') {
const hash = {
type: opts.type,
version: ARGON2_VERSION,
memory: opts.memory,
passes: opts.passes,
lanes: 1,
salt: nonce,
digest: buf
}
return argonStringEncode(hash)
}
try {
const res = b4a.toString(buf, enc)
return res
} catch (e) {
throw new Error(`Unsupported encoding: ${enc}`)
}
}
function verify (input, password, key, opts = {}) {
if (key !== null && typeof key === 'object') return verify(input, password, null, key)
if (input.slice(0, 7) === '$argon2') input = argonStringDecode(input)
const res = argon2(password, input.salt, key, input.assocData, 'binary', input)
return b4a.compare(res, input.digest) === 0
}
function needsRehash (str, ops, mem, alg) {
const { memory, passes, type } = argonStringDecode(str)
return ops !== passes || mem !== memory || !!alg && alg !== type
}
function argonStringEncode (hash) {
const y = TYPES[hash.type]
const v = hash.version
const m = hash.memory
const t = hash.passes
const p = hash.lanes
const data = hash.data ? ',data=' + b4a.toString(hash.data, 'base64') : ''
const salt = b4a.toString(hash.salt, 'base64')
const digest = b4a.toString(hash.digest, 'base64')
return `$argon2${y}$v=${v}$m=${m},t=${t},p=${p}${data}$${salt}$${digest}`
}
function argonStringDecode (string) {
if (b4a.isBuffer(string)) return argonStringDecode(b4a.toString(string))
const form = /\$argon2([id]+)\$v=(\d+)\$m=(\d+),t=(\d+),p=(\d+)(?:,data=(.+))?\$(.+)\$(.+)/
const match = string.match(form)
if (match === null) {
throw new Error('Malformed argon2 string.')
}
const [ _, y, v, m, t, p, ad, s, d ] = match
const assocData = ad ? b4a.from(ad, 'base64') : null
const salt = b4a.from(s, 'base64')
const digest = b4a.from(d, 'base64')
return {
type: TYPES.indexOf(y),
version: Number(v),
memory: Number(m),
passes: Number(t),
lanes: Number(p),
outlen: digest.byteLength,
assocData,
salt,
digest
}
}
function noop () {}
function hexSlice (buf, start, len) {
var str = ''
for (var i = 0; i < len; i++) str += toHex(buf[start + i])
return str
}
function toHex (n) {
if (n < 16) return '0' + n.toString(16)
return n.toString(16)
}
function writeUInt32LE (int, buf, offset) {
if (!offset) offset = 0
buf[offset] = int & 0xff
buf[offset + 1] = (int >>> 8) & 0xff
buf[offset + 2] = (int >>> 16) & 0xff
buf[offset + 3] = (int >>> 24) & 0xff
return buf
}