-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
169 lines (127 loc) · 4.25 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
const assert = require('nanoassert')
const b4a = require('b4a')
const wasm = typeof WebAssembly !== 'undefined' && require('./sha256.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
const freeList = []
module.exports = Sha256
const SHA256_BYTES = module.exports.SHA256_BYTES = 32
const INPUT_OFFSET = 40
const STATEBYTES = 108
const BLOCKSIZE = 64
function Sha256 () {
if (!(this instanceof Sha256)) return new Sha256()
if (!(wasm)) throw new Error('WASM not loaded. Wait for Sha256.ready(cb)')
if (!freeList.length) {
freeList.push(head)
head += STATEBYTES // need 100 bytes for internal state
}
this.finalized = false
this.digestLength = SHA256_BYTES
this.pointer = freeList.pop()
this.pos = 0
this._memory = new Uint8Array(wasm.memory.buffer)
this._memory.fill(0, this.pointer, this.pointer + STATEBYTES)
if (this.pointer + this.digestLength > this._memory.length) this._realloc(this.pointer + STATEBYTES)
}
Sha256.prototype._realloc = function (size) {
wasm.memory.grow(Math.max(0, Math.ceil(Math.abs(size - this._memory.length) / 65536)))
this._memory = new Uint8Array(wasm.memory.buffer)
}
Sha256.prototype.update = function (input, enc) {
assert(this.finalized === false, 'Hash instance finalized')
if (head % 4 !== 0) head += 4 - head % 4
assert(head % 4 === 0, 'input shoud be aligned for int32')
const [inputBuf, length] = formatInput(input, enc)
assert(inputBuf instanceof Uint8Array, 'input must be Uint8Array or Buffer')
if (head + 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) & 0x3f
wasm.sha256(this.pointer, head, length, 0)
return this
}
Sha256.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.sha256(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 'SHA256_BYTES' bytes remaining")
for (let i = 0; i < this.digestLength; i++) {
enc[i + offset] = resultBuf[i]
}
return enc
}
Sha256.WASM = wasm
Sha256.WASM_SUPPORTED = typeof WebAssembly !== 'undefined'
Sha256.ready = function (cb) {
if (!cb) cb = noop
if (!wasm) return cb(new Error('WebAssembly not supported'))
cb()
return Promise.resolve()
}
Sha256.prototype.ready = Sha256.ready
function HMAC (key) {
if (!(this instanceof HMAC)) return new HMAC(key)
this.pad = b4a.alloc(64)
this.inner = Sha256()
this.outer = Sha256()
const keyhash = b4a.alloc(32)
if (key.byteLength > 64) {
Sha256().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)
}
Sha256.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
}