forked from chm-diederichs/sha512-wasm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
174 lines (132 loc) · 4.4 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
const assert = require('nanoassert')
const b4a = require('b4a')
const wasm = require('./sha512.js')({
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
}
}
}
})
let head = 0
// assetrt head % 8 === 0 to guarantee alignment
const freeList = []
module.exports = Sha512
const SHA512_BYTES = module.exports.SHA512_BYTES = 64
const INPUT_OFFSET = 80
const STATEBYTES = 216
const BLOCKSIZE = 128
function Sha512 () {
if (!(this instanceof Sha512)) return new Sha512()
if (!(wasm)) throw new Error('WASM not loaded. Wait for Sha512.ready(cb)')
if (!freeList.length) {
freeList.push(head)
head += STATEBYTES
}
this.finalized = false
this.digestLength = SHA512_BYTES
this.pointer = freeList.pop()
this.pos = 0
this.wasm = wasm
// IOS start with wasm.memory.buffer in 0 so we need to check that and realloc
if (wasm.memory.buffer.byteLength > 0) {
this._memory = new Uint8Array(wasm.memory.buffer)
this._memory.fill(0, this.pointer, this.pointer + STATEBYTES)
}
if (this.pointer + this.digestLength > wasm.memory.buffer.byteLength) this._realloc(this.pointer + STATEBYTES)
}
Sha512.prototype._realloc = function (size) {
wasm.memory.grow(Math.max(0, Math.ceil(Math.abs(size - wasm.memory.buffer.byteLength) / 65536)))
this._memory = new Uint8Array(wasm.memory.buffer)
}
Sha512.prototype.update = function (input, enc) {
assert(this.finalized === false, 'Hash instance finalized')
if (head % 8 !== 0) head += 8 - head % 8
assert(head % 8 === 0, 'input should be aligned for int64')
const [inputBuf, length] = formatInput(input, enc)
assert(inputBuf instanceof Uint8Array, 'input must be Uint8Array or Buffer')
if (head + input.length > this._memory.length) this._realloc(head + input.length)
this._memory.fill(0, head, head + roundUp(length, BLOCKSIZE) - BLOCKSIZE)
this._memory.set(inputBuf.subarray(0, BLOCKSIZE - this.pos), this.pointer + INPUT_OFFSET + this.pos)
this._memory.set(inputBuf.subarray(BLOCKSIZE - this.pos), head)
this.pos = (this.pos + length) & 0x7f
wasm.sha512(this.pointer, head, length, 0)
return this
}
Sha512.prototype.digest = function (enc, offset = 0) {
assert(this.finalized === false, 'Hash instance finalized')
this.finalized = true
freeList.push(this.pointer)
const paddingStart = this.pointer + INPUT_OFFSET + this.pos
this._memory.fill(0, paddingStart, this.pointer + INPUT_OFFSET + BLOCKSIZE)
wasm.sha512(this.pointer, head, 0, 1)
const resultBuf = this._memory.subarray(this.pointer, this.pointer + this.digestLength)
if (!enc) {
return resultBuf
}
if (typeof enc === 'string') {
return b4a.toString(resultBuf, enc)
}
assert(enc instanceof Uint8Array, 'output must be Uint8Array or Buffer')
assert(enc.byteLength >= this.digestLength + offset,
"output must have at least 'SHA512_BYTES' bytes remaining")
for (let i = 0; i < this.digestLength; i++) {
enc[i + offset] = resultBuf[i]
}
return enc
}
Sha512.WASM = wasm
Sha512.WASM_SUPPORTED = typeof WebAssembly !== 'undefined'
Sha512.ready = function (cb) {
if (!cb) cb = noop
if (!wasm) return cb(new Error('WebAssembly not supported'))
cb()
return Promise.resolve()
}
Sha512.prototype.ready = Sha512.ready
function HMAC (key) {
if (!(this instanceof HMAC)) return new HMAC(key)
this.pad = b4a.alloc(128)
this.inner = Sha512()
this.outer = Sha512()
const keyhash = b4a.alloc(64)
if (key.byteLength > 128) {
Sha512().update(key).digest(keyhash)
key = keyhash
}
this.pad.fill(0x36)
for (let i = 0; i < key.byteLength; i++) {
this.pad[i] ^= key[i]
}
this.inner.update(this.pad)
this.pad.fill(0x5c)
for (let i = 0; i < key.byteLength; i++) {
this.pad[i] ^= key[i]
}
this.outer.update(this.pad)
this.pad.fill(0)
keyhash.fill(0)
}
HMAC.prototype.update = function (input, enc) {
this.inner.update(input, enc)
return this
}
HMAC.prototype.digest = function (enc, offset = 0) {
this.outer.update(this.inner.digest())
return this.outer.digest(enc, offset)
}
Sha512.HMAC = HMAC
function noop () {}
function formatInput (input, enc) {
var result = b4a.from(input, enc)
return [result, result.byteLength]
}
// only works for base that is power of 2
function roundUp (n, base) {
return (n + base - 1) & -base
}