boho 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,39 @@
1
+ export declare const digestLength: number;
2
+ export declare const blockSize: number;
3
+ export declare class Hash {
4
+ digestLength: number;
5
+ blockSize: number;
6
+ private state;
7
+ private temp;
8
+ private buffer;
9
+ private bufferLength;
10
+ private bytesHashed;
11
+ finished: boolean;
12
+ constructor();
13
+ reset(): this;
14
+ clean(): void;
15
+ update(data: Uint8Array, dataLength?: number): this;
16
+ finish(out: Uint8Array): this;
17
+ digest(): Uint8Array;
18
+ _saveState(out: Uint32Array): void;
19
+ _restoreState(from: Uint32Array, bytesHashed: number): void;
20
+ }
21
+ export declare class HMAC {
22
+ private inner;
23
+ private outer;
24
+ blockSize: number;
25
+ digestLength: number;
26
+ private istate;
27
+ private ostate;
28
+ constructor(key: Uint8Array);
29
+ reset(): this;
30
+ clean(): void;
31
+ update(data: Uint8Array): this;
32
+ finish(out: Uint8Array): this;
33
+ digest(): Uint8Array;
34
+ }
35
+ export declare function hash(data: Uint8Array): Uint8Array;
36
+ export default hash;
37
+ export declare function hmac(key: Uint8Array, data: Uint8Array): Uint8Array;
38
+ export declare function hkdf(key: Uint8Array, salt?: Uint8Array, info?: Uint8Array, length?: number): Uint8Array;
39
+ export declare function pbkdf2(password: Uint8Array, salt: Uint8Array, iterations: number, dkLen: number): Uint8Array;
@@ -0,0 +1,401 @@
1
+ // SHA-256 (+ HMAC and PBKDF2) for JavaScript.
2
+ //
3
+ // Written in 2014-2016 by Dmitry Chestnykh.
4
+ // Public domain, no warranty.
5
+ //
6
+ // Functions (accept and return Uint8Arrays):
7
+ //
8
+ // sha256(message) -> hash
9
+ // sha256.hmac(key, message) -> mac
10
+ // sha256.pbkdf2(password, salt, rounds, dkLen) -> dk
11
+ //
12
+ // Classes:
13
+ //
14
+ // new sha256.Hash()
15
+ // new sha256.HMAC(key)
16
+ //
17
+ export var digestLength = 32
18
+ export var blockSize = 64
19
+ // SHA-256 constants
20
+ const K = new Uint32Array([
21
+ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b,
22
+ 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01,
23
+ 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7,
24
+ 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
25
+ 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152,
26
+ 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
27
+ 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc,
28
+ 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
29
+ 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819,
30
+ 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08,
31
+ 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f,
32
+ 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
33
+ 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
34
+ ])
35
+ function hashBlocks (w, v, p, pos, len) {
36
+ let a, b, c, d, e, f, g, h, u, i, j, t1, t2
37
+ while (len >= 64) {
38
+ a = v[0]
39
+ b = v[1]
40
+ c = v[2]
41
+ d = v[3]
42
+ e = v[4]
43
+ f = v[5]
44
+ g = v[6]
45
+ h = v[7]
46
+ for (i = 0; i < 16; i++) {
47
+ j = pos + i * 4
48
+ w[i] = (((p[j] & 0xff) << 24) | ((p[j + 1] & 0xff) << 16) |
49
+ ((p[j + 2] & 0xff) << 8) | (p[j + 3] & 0xff))
50
+ }
51
+ for (i = 16; i < 64; i++) {
52
+ u = w[i - 2]
53
+ t1 = (u >>> 17 | u << (32 - 17)) ^ (u >>> 19 | u << (32 - 19)) ^ (u >>> 10)
54
+ u = w[i - 15]
55
+ t2 = (u >>> 7 | u << (32 - 7)) ^ (u >>> 18 | u << (32 - 18)) ^ (u >>> 3)
56
+ w[i] = (t1 + w[i - 7] | 0) + (t2 + w[i - 16] | 0)
57
+ }
58
+ for (i = 0; i < 64; i++) {
59
+ t1 = (((((e >>> 6 | e << (32 - 6)) ^ (e >>> 11 | e << (32 - 11)) ^
60
+ (e >>> 25 | e << (32 - 25))) + ((e & f) ^ (~e & g))) | 0) +
61
+ ((h + ((K[i] + w[i]) | 0)) | 0)) | 0
62
+ t2 = (((a >>> 2 | a << (32 - 2)) ^ (a >>> 13 | a << (32 - 13)) ^
63
+ (a >>> 22 | a << (32 - 22))) + ((a & b) ^ (a & c) ^ (b & c))) | 0
64
+ h = g
65
+ g = f
66
+ f = e
67
+ e = (d + t1) | 0
68
+ d = c
69
+ c = b
70
+ b = a
71
+ a = (t1 + t2) | 0
72
+ }
73
+ v[0] += a
74
+ v[1] += b
75
+ v[2] += c
76
+ v[3] += d
77
+ v[4] += e
78
+ v[5] += f
79
+ v[6] += g
80
+ v[7] += h
81
+ pos += 64
82
+ len -= 64
83
+ }
84
+ return pos
85
+ }
86
+ // Hash implements SHA256 hash algorithm.
87
+ const Hash = /** @class */ (function () {
88
+ function Hash () {
89
+ this.digestLength = digestLength
90
+ this.blockSize = blockSize
91
+ // Note: Int32Array is used instead of Uint32Array for performance reasons.
92
+ this.state = new Int32Array(8) // hash state
93
+ this.temp = new Int32Array(64) // temporary state
94
+ this.buffer = new Uint8Array(128) // buffer for data to hash
95
+ this.bufferLength = 0 // number of bytes in buffer
96
+ this.bytesHashed = 0 // number of total bytes hashed
97
+ this.finished = false // indicates whether the hash was finalized
98
+ this.reset()
99
+ }
100
+ // Resets hash state making it possible
101
+ // to re-use this instance to hash other data.
102
+ Hash.prototype.reset = function () {
103
+ this.state[0] = 0x6a09e667
104
+ this.state[1] = 0xbb67ae85
105
+ this.state[2] = 0x3c6ef372
106
+ this.state[3] = 0xa54ff53a
107
+ this.state[4] = 0x510e527f
108
+ this.state[5] = 0x9b05688c
109
+ this.state[6] = 0x1f83d9ab
110
+ this.state[7] = 0x5be0cd19
111
+ this.bufferLength = 0
112
+ this.bytesHashed = 0
113
+ this.finished = false
114
+ return this
115
+ }
116
+ // Cleans internal buffers and re-initializes hash state.
117
+ Hash.prototype.clean = function () {
118
+ for (var i = 0; i < this.buffer.length; i++) {
119
+ this.buffer[i] = 0
120
+ }
121
+ for (var i = 0; i < this.temp.length; i++) {
122
+ this.temp[i] = 0
123
+ }
124
+ this.reset()
125
+ }
126
+ // Updates hash state with the given data.
127
+ //
128
+ // Optionally, length of the data can be specified to hash
129
+ // fewer bytes than data.length.
130
+ //
131
+ // Throws error when trying to update already finalized hash:
132
+ // instance must be reset to use it again.
133
+ Hash.prototype.update = function (data, dataLength) {
134
+ if (dataLength === void 0) { dataLength = data.length }
135
+ if (this.finished) {
136
+ throw new Error("SHA256: can't update because hash was finished.")
137
+ }
138
+ let dataPos = 0
139
+ this.bytesHashed += dataLength
140
+ if (this.bufferLength > 0) {
141
+ while (this.bufferLength < 64 && dataLength > 0) {
142
+ this.buffer[this.bufferLength++] = data[dataPos++]
143
+ dataLength--
144
+ }
145
+ if (this.bufferLength === 64) {
146
+ hashBlocks(this.temp, this.state, this.buffer, 0, 64)
147
+ this.bufferLength = 0
148
+ }
149
+ }
150
+ if (dataLength >= 64) {
151
+ dataPos = hashBlocks(this.temp, this.state, data, dataPos, dataLength)
152
+ dataLength %= 64
153
+ }
154
+ while (dataLength > 0) {
155
+ this.buffer[this.bufferLength++] = data[dataPos++]
156
+ dataLength--
157
+ }
158
+ return this
159
+ }
160
+ // Finalizes hash state and puts hash into out.
161
+ //
162
+ // If hash was already finalized, puts the same value.
163
+ Hash.prototype.finish = function (out) {
164
+ if (!this.finished) {
165
+ const bytesHashed = this.bytesHashed
166
+ const left = this.bufferLength
167
+ const bitLenHi = (bytesHashed / 0x20000000) | 0
168
+ const bitLenLo = bytesHashed << 3
169
+ const padLength = (bytesHashed % 64 < 56) ? 64 : 128
170
+ this.buffer[left] = 0x80
171
+ for (var i = left + 1; i < padLength - 8; i++) {
172
+ this.buffer[i] = 0
173
+ }
174
+ this.buffer[padLength - 8] = (bitLenHi >>> 24) & 0xff
175
+ this.buffer[padLength - 7] = (bitLenHi >>> 16) & 0xff
176
+ this.buffer[padLength - 6] = (bitLenHi >>> 8) & 0xff
177
+ this.buffer[padLength - 5] = (bitLenHi >>> 0) & 0xff
178
+ this.buffer[padLength - 4] = (bitLenLo >>> 24) & 0xff
179
+ this.buffer[padLength - 3] = (bitLenLo >>> 16) & 0xff
180
+ this.buffer[padLength - 2] = (bitLenLo >>> 8) & 0xff
181
+ this.buffer[padLength - 1] = (bitLenLo >>> 0) & 0xff
182
+ hashBlocks(this.temp, this.state, this.buffer, 0, padLength)
183
+ this.finished = true
184
+ }
185
+ for (var i = 0; i < 8; i++) {
186
+ out[i * 4 + 0] = (this.state[i] >>> 24) & 0xff
187
+ out[i * 4 + 1] = (this.state[i] >>> 16) & 0xff
188
+ out[i * 4 + 2] = (this.state[i] >>> 8) & 0xff
189
+ out[i * 4 + 3] = (this.state[i] >>> 0) & 0xff
190
+ }
191
+ return this
192
+ }
193
+ // Returns the final hash digest.
194
+ Hash.prototype.digest = function () {
195
+ const out = new Uint8Array(this.digestLength)
196
+ this.finish(out)
197
+ return out
198
+ }
199
+ // Internal function for use in HMAC for optimization.
200
+ Hash.prototype._saveState = function (out) {
201
+ for (let i = 0; i < this.state.length; i++) {
202
+ out[i] = this.state[i]
203
+ }
204
+ }
205
+ // Internal function for use in HMAC for optimization.
206
+ Hash.prototype._restoreState = function (from, bytesHashed) {
207
+ for (let i = 0; i < this.state.length; i++) {
208
+ this.state[i] = from[i]
209
+ }
210
+ this.bytesHashed = bytesHashed
211
+ this.finished = false
212
+ this.bufferLength = 0
213
+ }
214
+ return Hash
215
+ }())
216
+ export { Hash }
217
+ // HMAC implements HMAC-SHA256 message authentication algorithm.
218
+ const HMAC = /** @class */ (function () {
219
+ function HMAC (key) {
220
+ this.inner = new Hash()
221
+ this.outer = new Hash()
222
+ this.blockSize = this.inner.blockSize
223
+ this.digestLength = this.inner.digestLength
224
+ const pad = new Uint8Array(this.blockSize)
225
+ if (key.length > this.blockSize) {
226
+ (new Hash()).update(key).finish(pad).clean()
227
+ } else {
228
+ for (var i = 0; i < key.length; i++) {
229
+ pad[i] = key[i]
230
+ }
231
+ }
232
+ for (var i = 0; i < pad.length; i++) {
233
+ pad[i] ^= 0x36
234
+ }
235
+ this.inner.update(pad)
236
+ for (var i = 0; i < pad.length; i++) {
237
+ pad[i] ^= 0x36 ^ 0x5c
238
+ }
239
+ this.outer.update(pad)
240
+ this.istate = new Uint32Array(8)
241
+ this.ostate = new Uint32Array(8)
242
+ this.inner._saveState(this.istate)
243
+ this.outer._saveState(this.ostate)
244
+ for (var i = 0; i < pad.length; i++) {
245
+ pad[i] = 0
246
+ }
247
+ }
248
+ // Returns HMAC state to the state initialized with key
249
+ // to make it possible to run HMAC over the other data with the same
250
+ // key without creating a new instance.
251
+ HMAC.prototype.reset = function () {
252
+ this.inner._restoreState(this.istate, this.inner.blockSize)
253
+ this.outer._restoreState(this.ostate, this.outer.blockSize)
254
+ return this
255
+ }
256
+ // Cleans HMAC state.
257
+ HMAC.prototype.clean = function () {
258
+ for (let i = 0; i < this.istate.length; i++) {
259
+ this.ostate[i] = this.istate[i] = 0
260
+ }
261
+ this.inner.clean()
262
+ this.outer.clean()
263
+ }
264
+ // Updates state with provided data.
265
+ HMAC.prototype.update = function (data) {
266
+ this.inner.update(data)
267
+ return this
268
+ }
269
+ // Finalizes HMAC and puts the result in out.
270
+ HMAC.prototype.finish = function (out) {
271
+ if (this.outer.finished) {
272
+ this.outer.finish(out)
273
+ } else {
274
+ this.inner.finish(out)
275
+ this.outer.update(out, this.digestLength).finish(out)
276
+ }
277
+ return this
278
+ }
279
+ // Returns message authentication code.
280
+ HMAC.prototype.digest = function () {
281
+ const out = new Uint8Array(this.digestLength)
282
+ this.finish(out)
283
+ return out
284
+ }
285
+ return HMAC
286
+ }())
287
+ export { HMAC }
288
+ // Returns SHA256 hash of data.
289
+ export function hash (data) {
290
+ const h = (new Hash()).update(data)
291
+ const digest = h.digest()
292
+ h.clean()
293
+ return digest
294
+ }
295
+ // Function hash is both available as module.hash and as default export.
296
+ export default hash
297
+ // Returns HMAC-SHA256 of data under the key.
298
+ export function hmac (key, data) {
299
+ const h = (new HMAC(key)).update(data)
300
+ const digest = h.digest()
301
+ h.clean()
302
+ return digest
303
+ }
304
+ // Fills hkdf buffer like this:
305
+ // T(1) = HMAC-Hash(PRK, T(0) | info | 0x01)
306
+ function fillBuffer (buffer, hmac, info, counter) {
307
+ // Counter is a byte value: check if it overflowed.
308
+ const num = counter[0]
309
+ if (num === 0) {
310
+ throw new Error('hkdf: cannot expand more')
311
+ }
312
+ // Prepare HMAC instance for new data with old key.
313
+ hmac.reset()
314
+ // Hash in previous output if it was generated
315
+ // (i.e. counter is greater than 1).
316
+ if (num > 1) {
317
+ hmac.update(buffer)
318
+ }
319
+ // Hash in info if it exists.
320
+ if (info) {
321
+ hmac.update(info)
322
+ }
323
+ // Hash in the counter.
324
+ hmac.update(counter)
325
+ // Output result to buffer and clean HMAC instance.
326
+ hmac.finish(buffer)
327
+ // Increment counter inside typed array, this works properly.
328
+ counter[0]++
329
+ }
330
+ const hkdfSalt = new Uint8Array(digestLength) // Filled with zeroes.
331
+ export function hkdf (key, salt, info, length) {
332
+ if (salt === void 0) { salt = hkdfSalt }
333
+ if (length === void 0) { length = 32 }
334
+ const counter = new Uint8Array([1])
335
+ // HKDF-Extract uses salt as HMAC key, and key as data.
336
+ const okm = hmac(salt, key)
337
+ // Initialize HMAC for expanding with extracted key.
338
+ // Ensure no collisions with `hmac` function.
339
+ const hmac_ = new HMAC(okm)
340
+ // Allocate buffer.
341
+ const buffer = new Uint8Array(hmac_.digestLength)
342
+ let bufpos = buffer.length
343
+ const out = new Uint8Array(length)
344
+ for (let i = 0; i < length; i++) {
345
+ if (bufpos === buffer.length) {
346
+ fillBuffer(buffer, hmac_, info, counter)
347
+ bufpos = 0
348
+ }
349
+ out[i] = buffer[bufpos++]
350
+ }
351
+ hmac_.clean()
352
+ buffer.fill(0)
353
+ counter.fill(0)
354
+ return out
355
+ }
356
+ // Derives a key from password and salt using PBKDF2-HMAC-SHA256
357
+ // with the given number of iterations.
358
+ //
359
+ // The number of bytes returned is equal to dkLen.
360
+ //
361
+ // (For better security, avoid dkLen greater than hash length - 32 bytes).
362
+ export function pbkdf2 (password, salt, iterations, dkLen) {
363
+ const prf = new HMAC(password)
364
+ const len = prf.digestLength
365
+ const ctr = new Uint8Array(4)
366
+ const t = new Uint8Array(len)
367
+ const u = new Uint8Array(len)
368
+ const dk = new Uint8Array(dkLen)
369
+ for (var i = 0; i * len < dkLen; i++) {
370
+ const c = i + 1
371
+ ctr[0] = (c >>> 24) & 0xff
372
+ ctr[1] = (c >>> 16) & 0xff
373
+ ctr[2] = (c >>> 8) & 0xff
374
+ ctr[3] = (c >>> 0) & 0xff
375
+ prf.reset()
376
+ prf.update(salt)
377
+ prf.update(ctr)
378
+ prf.finish(u)
379
+ for (var j = 0; j < len; j++) {
380
+ t[j] = u[j]
381
+ }
382
+ for (var j = 2; j <= iterations; j++) {
383
+ prf.reset()
384
+ prf.update(u).finish(u)
385
+ for (let k = 0; k < len; k++) {
386
+ t[k] ^= u[k]
387
+ }
388
+ }
389
+ for (var j = 0; j < len && i * len + j < dkLen; j++) {
390
+ dk[i * len + j] = t[j]
391
+ }
392
+ }
393
+ for (var i = 0; i < len; i++) {
394
+ t[i] = u[i] = 0
395
+ }
396
+ for (var i = 0; i < 4; i++) {
397
+ ctr[i] = 0
398
+ }
399
+ prf.clean()
400
+ return dk
401
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "boho",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "Ultra Light SSL without PKI.",
5
5
  "cjs": "dist/boho.cjs",
6
6
  "esm": "dist/boho.mjs",
@@ -8,7 +8,7 @@
8
8
  "browser_esm": "dist/boho.esm.js",
9
9
  "type": "module",
10
10
  "exports": {
11
- "import": "./dist/boho.mjs",
11
+ "import": "./src/boho.js",
12
12
  "require": "./dist/boho.cjs"
13
13
  },
14
14
  "scripts": {