@smithy/core 3.26.0 → 3.28.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.
Files changed (61) hide show
  1. package/dist-cjs/submodules/checksum/index.browser.js +358 -145
  2. package/dist-cjs/submodules/checksum/index.js +462 -142
  3. package/dist-cjs/submodules/checksum/index.native.js +358 -145
  4. package/dist-cjs/submodules/config/index.js +108 -63
  5. package/dist-cjs/submodules/event-streams/index.browser.js +11 -8
  6. package/dist-cjs/submodules/event-streams/index.js +11 -8
  7. package/dist-cjs/submodules/protocols/index.js +5 -2
  8. package/dist-cjs/submodules/serde/index.browser.js +30 -36
  9. package/dist-cjs/submodules/serde/index.js +54 -110
  10. package/dist-cjs/submodules/serde/index.native.js +30 -36
  11. package/dist-es/submodules/checksum/crc32/Crc32Js.js +30 -0
  12. package/dist-es/submodules/checksum/crc32/Crc32Node.js +24 -0
  13. package/dist-es/submodules/checksum/index.browser.js +7 -1
  14. package/dist-es/submodules/checksum/index.js +7 -1
  15. package/dist-es/submodules/checksum/index.native.js +7 -1
  16. package/dist-es/submodules/checksum/md5/Md5Js.js +92 -0
  17. package/dist-es/submodules/checksum/md5/Md5Node.js +29 -0
  18. package/dist-es/submodules/checksum/sha256/Sha256Js.js +179 -0
  19. package/dist-es/submodules/checksum/sha256/Sha256Node.js +59 -0
  20. package/dist-es/submodules/checksum/sha256/Sha256WebCrypto.js +71 -0
  21. package/dist-es/submodules/config/config-resolver/regionConfig/config.js +6 -1
  22. package/dist-es/submodules/config/config-resolver/regionConfig/getInstanceMetadataRegion.js +92 -0
  23. package/dist-es/submodules/config/defaults-mode/constants.js +3 -0
  24. package/dist-es/submodules/config/defaults-mode/resolveDefaultsModeConfig.js +3 -56
  25. package/dist-es/submodules/event-streams/eventstream-codec/EventStreamCodec.js +5 -3
  26. package/dist-es/submodules/event-streams/eventstream-codec/splitMessage.js +7 -6
  27. package/dist-es/submodules/protocols/HttpProtocol.js +4 -1
  28. package/dist-es/submodules/serde/concatBytes.js +15 -0
  29. package/dist-es/submodules/serde/index.browser.js +2 -0
  30. package/dist-es/submodules/serde/index.js +2 -0
  31. package/dist-es/submodules/serde/index.native.js +2 -0
  32. package/dist-es/submodules/serde/util-stream/checksum/ChecksumStream.js +4 -0
  33. package/dist-es/submodules/serde/util-stream/headStream.js +2 -1
  34. package/dist-es/submodules/serde/util-stream/stream-collector.browser.js +12 -38
  35. package/dist-es/submodules/serde/util-stream/stream-collector.js +18 -33
  36. package/dist-es/submodules/serde/util-utf8/toUint8Array.browser.js +3 -0
  37. package/dist-es/submodules/serde/util-utf8/toUint8Array.js +3 -0
  38. package/dist-types/submodules/checksum/crc32/Crc32Js.d.ts +18 -0
  39. package/dist-types/submodules/checksum/crc32/Crc32Node.d.ts +18 -0
  40. package/dist-types/submodules/checksum/index.browser.d.ts +7 -1
  41. package/dist-types/submodules/checksum/index.d.ts +7 -1
  42. package/dist-types/submodules/checksum/index.native.d.ts +7 -1
  43. package/dist-types/submodules/checksum/md5/Md5Js.d.ts +19 -0
  44. package/dist-types/submodules/checksum/md5/Md5Node.d.ts +17 -0
  45. package/dist-types/submodules/checksum/sha256/Sha256Js.d.ts +28 -0
  46. package/dist-types/submodules/checksum/sha256/Sha256Node.d.ts +13 -0
  47. package/dist-types/submodules/checksum/sha256/Sha256WebCrypto.d.ts +22 -0
  48. package/dist-types/submodules/config/config-resolver/regionConfig/getInstanceMetadataRegion.d.ts +7 -0
  49. package/dist-types/submodules/config/defaults-mode/constants.d.ts +12 -0
  50. package/dist-types/submodules/serde/concatBytes.d.ts +6 -0
  51. package/dist-types/submodules/serde/index.browser.d.ts +2 -0
  52. package/dist-types/submodules/serde/index.d.ts +2 -0
  53. package/dist-types/submodules/serde/index.native.d.ts +2 -0
  54. package/dist-types/submodules/serde/util-stream/checksum/ChecksumStream.d.ts +9 -0
  55. package/dist-types/submodules/serde/util-stream/stream-collector.browser.d.ts +8 -2
  56. package/dist-types/submodules/serde/util-stream/stream-collector.d.ts +4 -1
  57. package/package.json +4 -4
  58. package/dist-es/submodules/checksum/md5-js/constants.js +0 -3
  59. package/dist-es/submodules/checksum/md5-js/md5.js +0 -168
  60. package/dist-types/submodules/checksum/md5-js/constants.d.ts +0 -12
  61. package/dist-types/submodules/checksum/md5-js/md5.d.ts +0 -16
@@ -1,6 +1,8 @@
1
1
  const { createReadStream } = require("node:fs");
2
2
  const { Writable } = require("node:stream");
3
- const { toUint8Array, fromUtf8 } = require("@smithy/core/serde");
3
+ const { toUint8Array, concatBytes } = require("@smithy/core/serde");
4
+ const { createHash, createHmac } = require("node:crypto");
5
+ const zlib = require("node:zlib");
4
6
 
5
7
  async function blobReader(blob, onChunk, chunkSize = 1024 * 1024) {
6
8
  const size = blob.size;
@@ -79,178 +81,496 @@ const readableStreamHasher = (hashCtor, readableStream) => {
79
81
  });
80
82
  };
81
83
 
82
- const BLOCK_SIZE = 64;
83
- const DIGEST_LENGTH = 16;
84
- const INIT = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476];
84
+ class Md5Js {
85
+ digestLength = 16;
86
+ state = Uint32Array.from(INIT$1);
87
+ writeBuffer = new DataView(new ArrayBuffer(64));
88
+ bufferLength = 0;
89
+ bytesHashed = 0;
90
+ update(sourceData) {
91
+ const data = toUint8Array(sourceData);
92
+ let pos = 0;
93
+ let len = data.byteLength;
94
+ this.bytesHashed += len;
95
+ while (len > 0) {
96
+ this.writeBuffer.setUint8(this.bufferLength++, data[pos++]);
97
+ --len;
98
+ if (this.bufferLength === 64) {
99
+ compress(this.state, this.writeBuffer);
100
+ this.bufferLength = 0;
101
+ }
102
+ }
103
+ }
104
+ async digest() {
105
+ const state = Uint32Array.from(this.state);
106
+ const buf = new DataView(this.writeBuffer.buffer.slice(0));
107
+ let bufLen = this.bufferLength;
108
+ const bits = this.bytesHashed * 8;
109
+ buf.setUint8(bufLen++, 0x80);
110
+ if (this.bufferLength % 64 >= 56) {
111
+ for (let i = bufLen; i < 64; ++i) {
112
+ buf.setUint8(i, 0);
113
+ }
114
+ compress(state, buf);
115
+ bufLen = 0;
116
+ }
117
+ for (let i = bufLen; i < 56; ++i) {
118
+ buf.setUint8(i, 0);
119
+ }
120
+ buf.setUint32(56, bits >>> 0, true);
121
+ buf.setUint32(60, Math.floor(bits / 2 ** 32), true);
122
+ compress(state, buf);
123
+ const out = new Uint8Array(16);
124
+ const view = new DataView(out.buffer);
125
+ for (let i = 0; i < 4; ++i) {
126
+ view.setUint32(i * 4, state[i], true);
127
+ }
128
+ return out;
129
+ }
130
+ reset() {
131
+ this.state.set(INIT$1);
132
+ this.writeBuffer = new DataView(new ArrayBuffer(64));
133
+ this.bufferLength = 0;
134
+ this.bytesHashed = 0;
135
+ }
136
+ }
137
+ const INIT$1 = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476];
138
+ const M = 0xffffffff;
139
+ const S = Uint8Array.of(7, 12, 17, 22, 5, 9, 14, 20, 4, 11, 16, 23, 6, 10, 15, 21);
140
+ const T = Array.from({ length: 64 }, (_, i) => (Math.abs(Math.sin(i + 1)) * 2 ** 32) >>> 0);
141
+ function compress(state, block) {
142
+ let a = state[0], b = state[1], c = state[2], d = state[3];
143
+ for (let i = 0; i < 64; ++i) {
144
+ let f, g;
145
+ if (i < 16) {
146
+ f = (b & c) | (~b & d);
147
+ g = i;
148
+ }
149
+ else if (i < 32) {
150
+ f = (d & b) | (c & ~d);
151
+ g = (5 * i + 1) % 16;
152
+ }
153
+ else if (i < 48) {
154
+ f = b ^ c ^ d;
155
+ g = (3 * i + 5) % 16;
156
+ }
157
+ else {
158
+ f = c ^ (b | ~d);
159
+ g = (7 * i) % 16;
160
+ }
161
+ const x = block.getUint32(g * 4, true);
162
+ const tmp = d;
163
+ d = c;
164
+ c = b;
165
+ const s = S[(i >> 4) * 4 + (i & 3)];
166
+ const sum = (((a + f) & M) + ((x + T[i]) & M)) & M;
167
+ b = (b + (((sum << s) | (sum >>> (32 - s))) >>> 0)) & M;
168
+ a = tmp;
169
+ }
170
+ state[0] = (state[0] + a) & M;
171
+ state[1] = (state[1] + b) & M;
172
+ state[2] = (state[2] + c) & M;
173
+ state[3] = (state[3] + d) & M;
174
+ }
85
175
 
86
- class Md5 {
87
- state;
88
- buffer;
89
- bufferLength;
90
- bytesHashed;
91
- finished;
92
- constructor() {
93
- this.reset();
176
+ const hasNativeCrypto$1 = (() => {
177
+ try {
178
+ createHash("md5");
179
+ return true;
94
180
  }
95
- update(sourceData) {
96
- if (isEmptyData(sourceData)) {
97
- return;
181
+ catch {
182
+ return false;
183
+ }
184
+ })();
185
+ const Md5Node = hasNativeCrypto$1 ? buildNativeClass$2() : Md5Js;
186
+ function buildNativeClass$2() {
187
+ return class Md5Node {
188
+ digestLength = 16;
189
+ hash = createHash("md5");
190
+ update(data) {
191
+ this.hash.update(toUint8Array(data));
98
192
  }
99
- else if (this.finished) {
100
- throw new Error("Attempted to update an already finished hash.");
193
+ async digest() {
194
+ const buf = this.hash.copy().digest();
195
+ return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
101
196
  }
102
- const data = convertToBuffer(sourceData);
197
+ reset() {
198
+ this.hash = createHash("md5");
199
+ }
200
+ };
201
+ }
202
+
203
+ const CRC32_TABLE = new Uint32Array(256);
204
+ for (let i = 0; i < 256; ++i) {
205
+ let c = i;
206
+ for (let j = 0; j < 8; ++j) {
207
+ c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
208
+ }
209
+ CRC32_TABLE[i] = c >>> 0;
210
+ }
211
+ const ONES = 0xffff_ffff;
212
+ class Crc32Js {
213
+ digestLength = 4;
214
+ checksum = ONES;
215
+ update(data) {
216
+ for (let i = 0; i < data.length; ++i) {
217
+ this.checksum = (this.checksum >>> 8) ^ CRC32_TABLE[(this.checksum ^ data[i]) & 0xff];
218
+ }
219
+ }
220
+ digestSync() {
221
+ return (this.checksum ^ ONES) >>> 0;
222
+ }
223
+ async digest() {
224
+ const value = this.digestSync();
225
+ const out = new Uint8Array(4);
226
+ new DataView(out.buffer).setUint32(0, value, false);
227
+ return out;
228
+ }
229
+ reset() {
230
+ this.checksum = ONES;
231
+ }
232
+ }
233
+
234
+ const zlibCrc32 = typeof zlib.crc32 === "function" ? zlib.crc32 : undefined;
235
+ const Crc32Node = zlibCrc32 ? buildNativeClass$1(zlibCrc32) : Crc32Js;
236
+ function buildNativeClass$1(nativeCrc32) {
237
+ return class Crc32Node {
238
+ digestLength = 4;
239
+ value = 0;
240
+ update(data) {
241
+ this.value = nativeCrc32(data, this.value);
242
+ }
243
+ digestSync() {
244
+ return this.value >>> 0;
245
+ }
246
+ async digest() {
247
+ const out = new Uint8Array(4);
248
+ new DataView(out.buffer).setUint32(0, this.digestSync(), false);
249
+ return out;
250
+ }
251
+ reset() {
252
+ this.value = 0;
253
+ }
254
+ };
255
+ }
256
+
257
+ const BLOCK = 64;
258
+ const DIGEST_LENGTH = 32;
259
+ const MAX_HASHABLE_LENGTH = 2 ** 53 - 1;
260
+ class Sha256Js {
261
+ digestLength = DIGEST_LENGTH;
262
+ state = Int32Array.from(INIT);
263
+ w;
264
+ buffer = new Uint8Array(64);
265
+ bufferLength = 0;
266
+ bytesHashed = 0;
267
+ finished = false;
268
+ inner;
269
+ outer;
270
+ constructor(secret) {
271
+ if (secret) {
272
+ const key = Sha256Js.normalizeKey(secret);
273
+ this.inner = new Sha256Js();
274
+ this.outer = new Sha256Js();
275
+ const { inner, outer } = this;
276
+ const pad = new Uint8Array(BLOCK * 2);
277
+ for (let i = 0; i < BLOCK; ++i) {
278
+ pad[i] = 0x36 ^ key[i];
279
+ pad[i + BLOCK] = 0x5c ^ key[i];
280
+ }
281
+ inner.update(pad.subarray(0, BLOCK));
282
+ outer.update(pad.subarray(BLOCK));
283
+ }
284
+ }
285
+ update(data) {
286
+ if (this.finished) {
287
+ throw new Error("Attempted to update an already finished HMAC.");
288
+ }
289
+ if (this.inner) {
290
+ this.inner.update(data);
291
+ return;
292
+ }
293
+ const chunk = toUint8Array(data);
103
294
  let position = 0;
104
- let { byteLength } = data;
295
+ let { byteLength } = chunk;
105
296
  this.bytesHashed += byteLength;
297
+ if (this.bytesHashed * 8 > MAX_HASHABLE_LENGTH) {
298
+ throw new Error("Cannot hash more than 2^53 - 1 bits");
299
+ }
106
300
  while (byteLength > 0) {
107
- this.buffer.setUint8(this.bufferLength++, data[position++]);
301
+ this.buffer[this.bufferLength++] = chunk[position++];
108
302
  byteLength--;
109
- if (this.bufferLength === BLOCK_SIZE) {
303
+ if (this.bufferLength === BLOCK) {
110
304
  this.hashBuffer();
111
305
  this.bufferLength = 0;
112
306
  }
113
307
  }
114
308
  }
115
309
  async digest() {
116
- if (!this.finished) {
117
- const { buffer, bufferLength: undecoratedLength, bytesHashed } = this;
118
- const bitsHashed = bytesHashed * 8;
119
- buffer.setUint8(this.bufferLength++, 0b10000000);
120
- if (undecoratedLength % BLOCK_SIZE >= BLOCK_SIZE - 8) {
121
- for (let i = this.bufferLength; i < BLOCK_SIZE; i++) {
122
- buffer.setUint8(i, 0);
123
- }
124
- this.hashBuffer();
125
- this.bufferLength = 0;
126
- }
127
- for (let i = this.bufferLength; i < BLOCK_SIZE - 8; i++) {
128
- buffer.setUint8(i, 0);
310
+ const { inner, outer } = this;
311
+ if (inner && outer) {
312
+ if (this.finished) {
313
+ throw new Error("Attempted to digest an already finished HMAC.");
129
314
  }
130
- buffer.setUint32(BLOCK_SIZE - 8, bitsHashed >>> 0, true);
131
- buffer.setUint32(BLOCK_SIZE - 4, Math.floor(bitsHashed / 0x100000000), true);
132
- this.hashBuffer();
133
315
  this.finished = true;
316
+ const innerDigest = inner.digestSync();
317
+ outer.update(innerDigest);
318
+ return outer.digestSync();
134
319
  }
135
- const out = new DataView(new ArrayBuffer(DIGEST_LENGTH));
136
- for (let i = 0; i < 4; i++) {
137
- out.setUint32(i * 4, this.state[i], true);
138
- }
139
- return new Uint8Array(out.buffer, out.byteOffset, out.byteLength);
140
- }
141
- hashBuffer() {
142
- const { buffer, state } = this;
143
- let a = state[0], b = state[1], c = state[2], d = state[3];
144
- a = ff(a, b, c, d, buffer.getUint32(0, true), 7, 0xd76aa478);
145
- d = ff(d, a, b, c, buffer.getUint32(4, true), 12, 0xe8c7b756);
146
- c = ff(c, d, a, b, buffer.getUint32(8, true), 17, 0x242070db);
147
- b = ff(b, c, d, a, buffer.getUint32(12, true), 22, 0xc1bdceee);
148
- a = ff(a, b, c, d, buffer.getUint32(16, true), 7, 0xf57c0faf);
149
- d = ff(d, a, b, c, buffer.getUint32(20, true), 12, 0x4787c62a);
150
- c = ff(c, d, a, b, buffer.getUint32(24, true), 17, 0xa8304613);
151
- b = ff(b, c, d, a, buffer.getUint32(28, true), 22, 0xfd469501);
152
- a = ff(a, b, c, d, buffer.getUint32(32, true), 7, 0x698098d8);
153
- d = ff(d, a, b, c, buffer.getUint32(36, true), 12, 0x8b44f7af);
154
- c = ff(c, d, a, b, buffer.getUint32(40, true), 17, 0xffff5bb1);
155
- b = ff(b, c, d, a, buffer.getUint32(44, true), 22, 0x895cd7be);
156
- a = ff(a, b, c, d, buffer.getUint32(48, true), 7, 0x6b901122);
157
- d = ff(d, a, b, c, buffer.getUint32(52, true), 12, 0xfd987193);
158
- c = ff(c, d, a, b, buffer.getUint32(56, true), 17, 0xa679438e);
159
- b = ff(b, c, d, a, buffer.getUint32(60, true), 22, 0x49b40821);
160
- a = gg(a, b, c, d, buffer.getUint32(4, true), 5, 0xf61e2562);
161
- d = gg(d, a, b, c, buffer.getUint32(24, true), 9, 0xc040b340);
162
- c = gg(c, d, a, b, buffer.getUint32(44, true), 14, 0x265e5a51);
163
- b = gg(b, c, d, a, buffer.getUint32(0, true), 20, 0xe9b6c7aa);
164
- a = gg(a, b, c, d, buffer.getUint32(20, true), 5, 0xd62f105d);
165
- d = gg(d, a, b, c, buffer.getUint32(40, true), 9, 0x02441453);
166
- c = gg(c, d, a, b, buffer.getUint32(60, true), 14, 0xd8a1e681);
167
- b = gg(b, c, d, a, buffer.getUint32(16, true), 20, 0xe7d3fbc8);
168
- a = gg(a, b, c, d, buffer.getUint32(36, true), 5, 0x21e1cde6);
169
- d = gg(d, a, b, c, buffer.getUint32(56, true), 9, 0xc33707d6);
170
- c = gg(c, d, a, b, buffer.getUint32(12, true), 14, 0xf4d50d87);
171
- b = gg(b, c, d, a, buffer.getUint32(32, true), 20, 0x455a14ed);
172
- a = gg(a, b, c, d, buffer.getUint32(52, true), 5, 0xa9e3e905);
173
- d = gg(d, a, b, c, buffer.getUint32(8, true), 9, 0xfcefa3f8);
174
- c = gg(c, d, a, b, buffer.getUint32(28, true), 14, 0x676f02d9);
175
- b = gg(b, c, d, a, buffer.getUint32(48, true), 20, 0x8d2a4c8a);
176
- a = hh(a, b, c, d, buffer.getUint32(20, true), 4, 0xfffa3942);
177
- d = hh(d, a, b, c, buffer.getUint32(32, true), 11, 0x8771f681);
178
- c = hh(c, d, a, b, buffer.getUint32(44, true), 16, 0x6d9d6122);
179
- b = hh(b, c, d, a, buffer.getUint32(56, true), 23, 0xfde5380c);
180
- a = hh(a, b, c, d, buffer.getUint32(4, true), 4, 0xa4beea44);
181
- d = hh(d, a, b, c, buffer.getUint32(16, true), 11, 0x4bdecfa9);
182
- c = hh(c, d, a, b, buffer.getUint32(28, true), 16, 0xf6bb4b60);
183
- b = hh(b, c, d, a, buffer.getUint32(40, true), 23, 0xbebfbc70);
184
- a = hh(a, b, c, d, buffer.getUint32(52, true), 4, 0x289b7ec6);
185
- d = hh(d, a, b, c, buffer.getUint32(0, true), 11, 0xeaa127fa);
186
- c = hh(c, d, a, b, buffer.getUint32(12, true), 16, 0xd4ef3085);
187
- b = hh(b, c, d, a, buffer.getUint32(24, true), 23, 0x04881d05);
188
- a = hh(a, b, c, d, buffer.getUint32(36, true), 4, 0xd9d4d039);
189
- d = hh(d, a, b, c, buffer.getUint32(48, true), 11, 0xe6db99e5);
190
- c = hh(c, d, a, b, buffer.getUint32(60, true), 16, 0x1fa27cf8);
191
- b = hh(b, c, d, a, buffer.getUint32(8, true), 23, 0xc4ac5665);
192
- a = ii(a, b, c, d, buffer.getUint32(0, true), 6, 0xf4292244);
193
- d = ii(d, a, b, c, buffer.getUint32(28, true), 10, 0x432aff97);
194
- c = ii(c, d, a, b, buffer.getUint32(56, true), 15, 0xab9423a7);
195
- b = ii(b, c, d, a, buffer.getUint32(20, true), 21, 0xfc93a039);
196
- a = ii(a, b, c, d, buffer.getUint32(48, true), 6, 0x655b59c3);
197
- d = ii(d, a, b, c, buffer.getUint32(12, true), 10, 0x8f0ccc92);
198
- c = ii(c, d, a, b, buffer.getUint32(40, true), 15, 0xffeff47d);
199
- b = ii(b, c, d, a, buffer.getUint32(4, true), 21, 0x85845dd1);
200
- a = ii(a, b, c, d, buffer.getUint32(32, true), 6, 0x6fa87e4f);
201
- d = ii(d, a, b, c, buffer.getUint32(60, true), 10, 0xfe2ce6e0);
202
- c = ii(c, d, a, b, buffer.getUint32(24, true), 15, 0xa3014314);
203
- b = ii(b, c, d, a, buffer.getUint32(52, true), 21, 0x4e0811a1);
204
- a = ii(a, b, c, d, buffer.getUint32(16, true), 6, 0xf7537e82);
205
- d = ii(d, a, b, c, buffer.getUint32(44, true), 10, 0xbd3af235);
206
- c = ii(c, d, a, b, buffer.getUint32(8, true), 15, 0x2ad7d2bb);
207
- b = ii(b, c, d, a, buffer.getUint32(36, true), 21, 0xeb86d391);
208
- state[0] = (a + state[0]) & 0xffffffff;
209
- state[1] = (b + state[1]) & 0xffffffff;
210
- state[2] = (c + state[2]) & 0xffffffff;
211
- state[3] = (d + state[3]) & 0xffffffff;
320
+ return this.digestSync();
212
321
  }
213
322
  reset() {
214
- this.state = Uint32Array.from(INIT);
215
- this.buffer = new DataView(new ArrayBuffer(BLOCK_SIZE));
323
+ this.state = Int32Array.from(INIT);
324
+ this.buffer = new Uint8Array(64);
216
325
  this.bufferLength = 0;
217
326
  this.bytesHashed = 0;
218
- this.finished = false;
327
+ }
328
+ digestSync() {
329
+ const state = this.state.slice();
330
+ const buffer = this.buffer.slice();
331
+ let bufferLength = this.bufferLength;
332
+ const bitsHashed = this.bytesHashed * 8;
333
+ const bufferView = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
334
+ bufferView.setUint8(bufferLength++, 0x80);
335
+ if ((bufferLength - 1) % BLOCK >= BLOCK - 8) {
336
+ for (let i = bufferLength; i < BLOCK; ++i) {
337
+ bufferView.setUint8(i, 0);
338
+ }
339
+ this.hashBufferWith(state, buffer);
340
+ bufferLength = 0;
341
+ }
342
+ for (let i = bufferLength; i < BLOCK - 8; ++i) {
343
+ bufferView.setUint8(i, 0);
344
+ }
345
+ bufferView.setUint32(BLOCK - 8, Math.floor(bitsHashed / 0x100000000), false);
346
+ bufferView.setUint32(BLOCK - 4, bitsHashed, false);
347
+ this.hashBufferWith(state, buffer);
348
+ const out = new Uint8Array(DIGEST_LENGTH);
349
+ for (let i = 0; i < 8; ++i) {
350
+ out[i * 4] = (state[i] >>> 24) & 0xff;
351
+ out[i * 4 + 1] = (state[i] >>> 16) & 0xff;
352
+ out[i * 4 + 2] = (state[i] >>> 8) & 0xff;
353
+ out[i * 4 + 3] = (state[i] >>> 0) & 0xff;
354
+ }
355
+ return out;
356
+ }
357
+ static normalizeKey(secret) {
358
+ const key = toUint8Array(secret);
359
+ if (key.byteLength > BLOCK) {
360
+ const h = new Sha256Js();
361
+ h.update(key);
362
+ const out = h.digestSync();
363
+ const padded = new Uint8Array(BLOCK);
364
+ padded.set(out);
365
+ return padded;
366
+ }
367
+ if (key.byteLength < BLOCK) {
368
+ const padded = new Uint8Array(BLOCK);
369
+ padded.set(key);
370
+ return padded;
371
+ }
372
+ return key;
373
+ }
374
+ hashBuffer() {
375
+ this.hashBufferWith(this.state, this.buffer);
376
+ }
377
+ hashBufferWith(state, buffer) {
378
+ const w = (this.w ??= new Int32Array(64));
379
+ let s0 = state[0], s1 = state[1], s2 = state[2], s3 = state[3], s4 = state[4], s5 = state[5], s6 = state[6], s7 = state[7];
380
+ for (let i = 0; i < BLOCK; ++i) {
381
+ if (i < 16) {
382
+ w[i] =
383
+ ((buffer[i * 4] & 0xff) << 24) |
384
+ ((buffer[i * 4 + 1] & 0xff) << 16) |
385
+ ((buffer[i * 4 + 2] & 0xff) << 8) |
386
+ (buffer[i * 4 + 3] & 0xff);
387
+ }
388
+ else {
389
+ let u = w[i - 2];
390
+ const t1 = ((u >>> 17) | (u << 15)) ^ ((u >>> 19) | (u << 13)) ^ (u >>> 10);
391
+ u = w[i - 15];
392
+ const t2 = ((u >>> 7) | (u << 25)) ^ ((u >>> 18) | (u << 14)) ^ (u >>> 3);
393
+ w[i] = ((t1 + w[i - 7]) | 0) + ((t2 + w[i - 16]) | 0);
394
+ }
395
+ const t1 = ((((((s4 >>> 6) | (s4 << 26)) ^ ((s4 >>> 11) | (s4 << 21)) ^ ((s4 >>> 25) | (s4 << 7))) +
396
+ ((s4 & s5) ^ (~s4 & s6))) |
397
+ 0) +
398
+ ((s7 + ((K[i] + w[i]) | 0)) | 0)) |
399
+ 0;
400
+ const t2 = ((((s0 >>> 2) | (s0 << 30)) ^ ((s0 >>> 13) | (s0 << 19)) ^ ((s0 >>> 22) | (s0 << 10))) +
401
+ ((s0 & s1) ^ (s0 & s2) ^ (s1 & s2))) |
402
+ 0;
403
+ s7 = s6;
404
+ s6 = s5;
405
+ s5 = s4;
406
+ s4 = (s3 + t1) | 0;
407
+ s3 = s2;
408
+ s2 = s1;
409
+ s1 = s0;
410
+ s0 = (t1 + t2) | 0;
411
+ }
412
+ state[0] += s0;
413
+ state[1] += s1;
414
+ state[2] += s2;
415
+ state[3] += s3;
416
+ state[4] += s4;
417
+ state[5] += s5;
418
+ state[6] += s6;
419
+ state[7] += s7;
219
420
  }
220
421
  }
221
- function cmn(q, a, b, x, s, t) {
222
- a = (((a + q) & 0xffffffff) + ((x + t) & 0xffffffff)) & 0xffffffff;
223
- return (((a << s) | (a >>> (32 - s))) + b) & 0xffffffff;
224
- }
225
- function ff(a, b, c, d, x, s, t) {
226
- return cmn((b & c) | (~b & d), a, b, x, s, t);
227
- }
228
- function gg(a, b, c, d, x, s, t) {
229
- return cmn((b & d) | (c & ~d), a, b, x, s, t);
230
- }
231
- function hh(a, b, c, d, x, s, t) {
232
- return cmn(b ^ c ^ d, a, b, x, s, t);
233
- }
234
- function ii(a, b, c, d, x, s, t) {
235
- return cmn(c ^ (b | ~d), a, b, x, s, t);
236
- }
237
- function isEmptyData(data) {
238
- if (typeof data === "string") {
239
- return data.length === 0;
422
+ const INIT = new Int32Array([
423
+ 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
424
+ ]);
425
+ const K = new Int32Array([
426
+ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
427
+ 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
428
+ 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
429
+ 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
430
+ 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
431
+ 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
432
+ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
433
+ 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
434
+ ]);
435
+
436
+ const hasNativeCrypto = (() => {
437
+ try {
438
+ createHash("sha256");
439
+ return true;
440
+ }
441
+ catch {
442
+ return false;
240
443
  }
241
- return data.byteLength === 0;
444
+ })();
445
+ const Sha256Node = hasNativeCrypto ? buildNativeClass() : Sha256Js;
446
+ function buildNativeClass() {
447
+ return class Sha256Node {
448
+ digestLength = 32;
449
+ secret;
450
+ hash;
451
+ isHmac;
452
+ finished = false;
453
+ constructor(secret) {
454
+ this.secret = secret;
455
+ this.isHmac = !!secret;
456
+ this.hash = this.createHash();
457
+ }
458
+ update(data) {
459
+ if (this.finished) {
460
+ throw new Error("Attempted to update an already finished hash.");
461
+ }
462
+ this.hash.update(data);
463
+ }
464
+ async digest() {
465
+ let buf;
466
+ if (this.isHmac) {
467
+ this.finished = true;
468
+ buf = this.hash.digest();
469
+ }
470
+ else {
471
+ buf = this.hash.copy().digest();
472
+ }
473
+ return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
474
+ }
475
+ reset() {
476
+ this.hash = this.createHash();
477
+ this.finished = false;
478
+ }
479
+ createHash() {
480
+ return this.secret ? createHmac("sha256", toBuffer(this.secret)) : createHash("sha256");
481
+ }
482
+ };
242
483
  }
243
- function convertToBuffer(data) {
484
+ function toBuffer(data) {
244
485
  if (typeof data === "string") {
245
- return fromUtf8(data);
486
+ return data;
246
487
  }
247
488
  if (ArrayBuffer.isView(data)) {
248
- return new Uint8Array(data.buffer, data.byteOffset, data.byteLength / Uint8Array.BYTES_PER_ELEMENT);
489
+ return Buffer.from(data.buffer, data.byteOffset, data.byteLength);
490
+ }
491
+ return Buffer.from(data);
492
+ }
493
+
494
+ const { digest, sign, importKey } = globalThis?.crypto?.subtle ?? {};
495
+ const subtle = typeof digest === "function" && typeof sign === "function" && typeof importKey === "function"
496
+ ? globalThis.crypto.subtle
497
+ : undefined;
498
+ const MAX_PENDING_BYTES = 8 * 1024 * 1024;
499
+ class Sha256WebCrypto {
500
+ digestLength = 32;
501
+ secret;
502
+ pending = [];
503
+ pendingBytes = 0;
504
+ fallback;
505
+ finished = false;
506
+ constructor(secret) {
507
+ if (secret) {
508
+ this.secret = toUint8Array(secret);
509
+ }
510
+ }
511
+ update(data) {
512
+ if (this.finished) {
513
+ throw new Error("Attempted to update an already finished HMAC.");
514
+ }
515
+ if (this.fallback) {
516
+ this.fallback.update(data);
517
+ return;
518
+ }
519
+ this.pending.push(data.slice());
520
+ this.pendingBytes += data.byteLength;
521
+ if (this.pendingBytes >= MAX_PENDING_BYTES) {
522
+ this.switchToFallback();
523
+ }
524
+ }
525
+ async digest() {
526
+ if (this.fallback) {
527
+ return this.fallback.digest();
528
+ }
529
+ if (this.secret && this.finished) {
530
+ throw new Error("Attempted to digest an already finished HMAC.");
531
+ }
532
+ const data = concatBytes(this.pending);
533
+ if (subtle) {
534
+ if (this.secret) {
535
+ this.finished = true;
536
+ const key = await subtle.importKey("raw", this.secret, { name: "HMAC", hash: "SHA-256" }, false, ["sign"]);
537
+ const sig = await subtle.sign("HMAC", key, data);
538
+ return new Uint8Array(sig);
539
+ }
540
+ const hash = await subtle.digest("SHA-256", data);
541
+ return new Uint8Array(hash);
542
+ }
543
+ const sha256 = new Sha256Js(this.secret);
544
+ sha256.update(data);
545
+ return sha256.digest();
546
+ }
547
+ reset() {
548
+ this.pending = [];
549
+ this.pendingBytes = 0;
550
+ this.fallback = undefined;
551
+ this.finished = false;
552
+ }
553
+ switchToFallback() {
554
+ const sha256Js = new Sha256Js(this.secret);
555
+ for (const chunk of this.pending) {
556
+ sha256Js.update(chunk);
557
+ }
558
+ this.fallback = sha256Js;
559
+ this.pending = [];
560
+ this.pendingBytes = 0;
249
561
  }
250
- return new Uint8Array(data);
251
562
  }
252
563
 
253
- exports.Md5 = Md5;
564
+ exports.Crc32 = Crc32Node;
565
+ exports.Crc32Js = Crc32Js;
566
+ exports.Crc32Node = Crc32Node;
567
+ exports.Md5 = Md5Node;
568
+ exports.Md5Js = Md5Js;
569
+ exports.Md5Node = Md5Node;
570
+ exports.Sha256 = Sha256Node;
571
+ exports.Sha256Js = Sha256Js;
572
+ exports.Sha256Node = Sha256Node;
573
+ exports.Sha256WebCrypto = Sha256WebCrypto;
254
574
  exports.blobHasher = blobHasher;
255
575
  exports.blobReader = blobReader;
256
576
  exports.fileStreamHasher = fileStreamHasher;