🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

@smithy/core

Package Overview
Dependencies
Maintainers
2
Versions
147
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@smithy/core - npm Package Compare versions

Comparing version
3.26.0
to
3.27.0
+27
dist-es/submodules/checksum/crc32/Crc32Js.js
const CRC32_TABLE = new Uint32Array(256);
for (let i = 0; i < 256; ++i) {
let c = i;
for (let j = 0; j < 8; ++j) {
c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
}
CRC32_TABLE[i] = c >>> 0;
}
const ONES = 0xffff_ffff;
export class Crc32Js {
digestLength = 4;
checksum = ONES;
update(data) {
for (let i = 0; i < data.length; ++i) {
this.checksum = (this.checksum >>> 8) ^ CRC32_TABLE[(this.checksum ^ data[i]) & 0xff];
}
}
async digest() {
const value = (this.checksum ^ ONES) >>> 0;
const out = new Uint8Array(4);
new DataView(out.buffer).setUint32(0, value, false);
return out;
}
reset() {
this.checksum = ONES;
}
}
import * as zlib from "node:zlib";
import { Crc32Js } from "./Crc32Js";
const zlibCrc32 = typeof zlib.crc32 === "function" ? zlib.crc32 : undefined;
export const Crc32Node = zlibCrc32 ? buildNativeClass(zlibCrc32) : Crc32Js;
function buildNativeClass(nativeCrc32) {
return class Crc32Node {
digestLength = 4;
value = 0;
update(data) {
this.value = nativeCrc32(data, this.value);
}
async digest() {
const out = new Uint8Array(4);
new DataView(out.buffer).setUint32(0, this.value >>> 0, false);
return out;
}
reset() {
this.value = 0;
}
};
}
import { toUint8Array } from "@smithy/core/serde";
export class Md5Js {
digestLength = 16;
state = Uint32Array.from(INIT);
writeBuffer = new DataView(new ArrayBuffer(64));
bufferLength = 0;
bytesHashed = 0;
update(sourceData) {
const data = toUint8Array(sourceData);
let pos = 0;
let len = data.byteLength;
this.bytesHashed += len;
while (len > 0) {
this.writeBuffer.setUint8(this.bufferLength++, data[pos++]);
--len;
if (this.bufferLength === 64) {
compress(this.state, this.writeBuffer);
this.bufferLength = 0;
}
}
}
async digest() {
const state = Uint32Array.from(this.state);
const buf = new DataView(this.writeBuffer.buffer.slice(0));
let bufLen = this.bufferLength;
const bits = this.bytesHashed * 8;
buf.setUint8(bufLen++, 0x80);
if (this.bufferLength % 64 >= 56) {
for (let i = bufLen; i < 64; ++i) {
buf.setUint8(i, 0);
}
compress(state, buf);
bufLen = 0;
}
for (let i = bufLen; i < 56; ++i) {
buf.setUint8(i, 0);
}
buf.setUint32(56, bits >>> 0, true);
buf.setUint32(60, Math.floor(bits / 2 ** 32), true);
compress(state, buf);
const out = new Uint8Array(16);
const view = new DataView(out.buffer);
for (let i = 0; i < 4; ++i) {
view.setUint32(i * 4, state[i], true);
}
return out;
}
reset() {
this.state.set(INIT);
this.writeBuffer = new DataView(new ArrayBuffer(64));
this.bufferLength = 0;
this.bytesHashed = 0;
}
}
const INIT = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476];
const M = 0xffffffff;
const S = Uint8Array.of(7, 12, 17, 22, 5, 9, 14, 20, 4, 11, 16, 23, 6, 10, 15, 21);
const T = Array.from({ length: 64 }, (_, i) => (Math.abs(Math.sin(i + 1)) * 2 ** 32) >>> 0);
function compress(state, block) {
let a = state[0], b = state[1], c = state[2], d = state[3];
for (let i = 0; i < 64; ++i) {
let f, g;
if (i < 16) {
f = (b & c) | (~b & d);
g = i;
}
else if (i < 32) {
f = (d & b) | (c & ~d);
g = (5 * i + 1) % 16;
}
else if (i < 48) {
f = b ^ c ^ d;
g = (3 * i + 5) % 16;
}
else {
f = c ^ (b | ~d);
g = (7 * i) % 16;
}
const x = block.getUint32(g * 4, true);
const tmp = d;
d = c;
c = b;
const s = S[(i >> 4) * 4 + (i & 3)];
const sum = (((a + f) & M) + ((x + T[i]) & M)) & M;
b = (b + (((sum << s) | (sum >>> (32 - s))) >>> 0)) & M;
a = tmp;
}
state[0] = (state[0] + a) & M;
state[1] = (state[1] + b) & M;
state[2] = (state[2] + c) & M;
state[3] = (state[3] + d) & M;
}
import { createHash } from "node:crypto";
import { toUint8Array } from "@smithy/core/serde";
import { Md5Js } from "./Md5Js";
const hasNativeCrypto = (() => {
try {
createHash("md5");
return true;
}
catch {
return false;
}
})();
export const Md5Node = hasNativeCrypto ? buildNativeClass() : Md5Js;
function buildNativeClass() {
return class Md5Node {
digestLength = 16;
hash = createHash("md5");
update(data) {
this.hash.update(toUint8Array(data));
}
async digest() {
const buf = this.hash.copy().digest();
return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
}
reset() {
this.hash = createHash("md5");
}
};
}
import { toUint8Array } from "@smithy/core/serde";
const BLOCK = 64;
const DIGEST_LENGTH = 32;
const MAX_HASHABLE_LENGTH = 2 ** 53 - 1;
export class Sha256Js {
digestLength = DIGEST_LENGTH;
state = Int32Array.from(INIT);
w;
buffer = new Uint8Array(64);
bufferLength = 0;
bytesHashed = 0;
finished = false;
inner;
outer;
constructor(secret) {
if (secret) {
const key = Sha256Js.normalizeKey(secret);
this.inner = new Sha256Js();
this.outer = new Sha256Js();
const { inner, outer } = this;
const pad = new Uint8Array(BLOCK * 2);
for (let i = 0; i < BLOCK; ++i) {
pad[i] = 0x36 ^ key[i];
pad[i + BLOCK] = 0x5c ^ key[i];
}
inner.update(pad.subarray(0, BLOCK));
outer.update(pad.subarray(BLOCK));
}
}
update(data) {
if (this.finished) {
throw new Error("Attempted to update an already finished HMAC.");
}
if (this.inner) {
this.inner.update(data);
return;
}
const chunk = toUint8Array(data);
let position = 0;
let { byteLength } = chunk;
this.bytesHashed += byteLength;
if (this.bytesHashed * 8 > MAX_HASHABLE_LENGTH) {
throw new Error("Cannot hash more than 2^53 - 1 bits");
}
while (byteLength > 0) {
this.buffer[this.bufferLength++] = chunk[position++];
byteLength--;
if (this.bufferLength === BLOCK) {
this.hashBuffer();
this.bufferLength = 0;
}
}
}
async digest() {
const { inner, outer } = this;
if (inner && outer) {
if (this.finished) {
throw new Error("Attempted to digest an already finished HMAC.");
}
this.finished = true;
const innerDigest = inner.digestSync();
outer.update(innerDigest);
return outer.digestSync();
}
return this.digestSync();
}
reset() {
this.state = Int32Array.from(INIT);
this.buffer = new Uint8Array(64);
this.bufferLength = 0;
this.bytesHashed = 0;
}
digestSync() {
const state = this.state.slice();
const buffer = this.buffer.slice();
let bufferLength = this.bufferLength;
const bitsHashed = this.bytesHashed * 8;
const bufferView = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
bufferView.setUint8(bufferLength++, 0x80);
if ((bufferLength - 1) % BLOCK >= BLOCK - 8) {
for (let i = bufferLength; i < BLOCK; ++i) {
bufferView.setUint8(i, 0);
}
this.hashBufferWith(state, buffer);
bufferLength = 0;
}
for (let i = bufferLength; i < BLOCK - 8; ++i) {
bufferView.setUint8(i, 0);
}
bufferView.setUint32(BLOCK - 8, Math.floor(bitsHashed / 0x100000000), false);
bufferView.setUint32(BLOCK - 4, bitsHashed, false);
this.hashBufferWith(state, buffer);
const out = new Uint8Array(DIGEST_LENGTH);
for (let i = 0; i < 8; ++i) {
out[i * 4] = (state[i] >>> 24) & 0xff;
out[i * 4 + 1] = (state[i] >>> 16) & 0xff;
out[i * 4 + 2] = (state[i] >>> 8) & 0xff;
out[i * 4 + 3] = (state[i] >>> 0) & 0xff;
}
return out;
}
static normalizeKey(secret) {
const key = toUint8Array(secret);
if (key.byteLength > BLOCK) {
const h = new Sha256Js();
h.update(key);
const out = h.digestSync();
const padded = new Uint8Array(BLOCK);
padded.set(out);
return padded;
}
if (key.byteLength < BLOCK) {
const padded = new Uint8Array(BLOCK);
padded.set(key);
return padded;
}
return key;
}
hashBuffer() {
this.hashBufferWith(this.state, this.buffer);
}
hashBufferWith(state, buffer) {
const w = (this.w ??= new Int32Array(64));
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];
for (let i = 0; i < BLOCK; ++i) {
if (i < 16) {
w[i] =
((buffer[i * 4] & 0xff) << 24) |
((buffer[i * 4 + 1] & 0xff) << 16) |
((buffer[i * 4 + 2] & 0xff) << 8) |
(buffer[i * 4 + 3] & 0xff);
}
else {
let u = w[i - 2];
const t1 = ((u >>> 17) | (u << 15)) ^ ((u >>> 19) | (u << 13)) ^ (u >>> 10);
u = w[i - 15];
const t2 = ((u >>> 7) | (u << 25)) ^ ((u >>> 18) | (u << 14)) ^ (u >>> 3);
w[i] = ((t1 + w[i - 7]) | 0) + ((t2 + w[i - 16]) | 0);
}
const t1 = ((((((s4 >>> 6) | (s4 << 26)) ^ ((s4 >>> 11) | (s4 << 21)) ^ ((s4 >>> 25) | (s4 << 7))) +
((s4 & s5) ^ (~s4 & s6))) |
0) +
((s7 + ((K[i] + w[i]) | 0)) | 0)) |
0;
const t2 = ((((s0 >>> 2) | (s0 << 30)) ^ ((s0 >>> 13) | (s0 << 19)) ^ ((s0 >>> 22) | (s0 << 10))) +
((s0 & s1) ^ (s0 & s2) ^ (s1 & s2))) |
0;
s7 = s6;
s6 = s5;
s5 = s4;
s4 = (s3 + t1) | 0;
s3 = s2;
s2 = s1;
s1 = s0;
s0 = (t1 + t2) | 0;
}
state[0] += s0;
state[1] += s1;
state[2] += s2;
state[3] += s3;
state[4] += s4;
state[5] += s5;
state[6] += s6;
state[7] += s7;
}
}
const INIT = new Int32Array([
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
]);
const K = new Int32Array([
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
]);
import { createHash, createHmac } from "node:crypto";
import { Sha256Js } from "./Sha256Js";
const hasNativeCrypto = (() => {
try {
createHash("sha256");
return true;
}
catch {
return false;
}
})();
export const Sha256Node = hasNativeCrypto ? buildNativeClass() : Sha256Js;
function buildNativeClass() {
return class Sha256Node {
digestLength = 32;
secret;
hash;
isHmac;
finished = false;
constructor(secret) {
this.secret = secret;
this.isHmac = !!secret;
this.hash = this.createHash();
}
update(data) {
if (this.finished) {
throw new Error("Attempted to update an already finished hash.");
}
this.hash.update(data);
}
async digest() {
let buf;
if (this.isHmac) {
this.finished = true;
buf = this.hash.digest();
}
else {
buf = this.hash.copy().digest();
}
return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
}
reset() {
this.hash = this.createHash();
this.finished = false;
}
createHash() {
return this.secret ? createHmac("sha256", toBuffer(this.secret)) : createHash("sha256");
}
};
}
function toBuffer(data) {
if (typeof data === "string") {
return data;
}
if (ArrayBuffer.isView(data)) {
return Buffer.from(data.buffer, data.byteOffset, data.byteLength);
}
return Buffer.from(data);
}
import { concatBytes, toUint8Array } from "@smithy/core/serde";
import { Sha256Js } from "./Sha256Js";
const { digest, sign, importKey } = globalThis?.crypto?.subtle ?? {};
const subtle = typeof digest === "function" && typeof sign === "function" && typeof importKey === "function"
? globalThis.crypto.subtle
: undefined;
const MAX_PENDING_BYTES = 8 * 1024 * 1024;
export class Sha256WebCrypto {
digestLength = 32;
secret;
pending = [];
pendingBytes = 0;
fallback;
finished = false;
constructor(secret) {
if (secret) {
this.secret = toUint8Array(secret);
}
}
update(data) {
if (this.finished) {
throw new Error("Attempted to update an already finished HMAC.");
}
if (this.fallback) {
this.fallback.update(data);
return;
}
this.pending.push(data.slice());
this.pendingBytes += data.byteLength;
if (this.pendingBytes >= MAX_PENDING_BYTES) {
this.switchToFallback();
}
}
async digest() {
if (this.fallback) {
return this.fallback.digest();
}
if (this.secret && this.finished) {
throw new Error("Attempted to digest an already finished HMAC.");
}
const data = concatBytes(this.pending);
if (subtle) {
if (this.secret) {
this.finished = true;
const key = await subtle.importKey("raw", this.secret, { name: "HMAC", hash: "SHA-256" }, false, ["sign"]);
const sig = await subtle.sign("HMAC", key, data);
return new Uint8Array(sig);
}
const hash = await subtle.digest("SHA-256", data);
return new Uint8Array(hash);
}
const sha256 = new Sha256Js(this.secret);
sha256.update(data);
return sha256.digest();
}
reset() {
this.pending = [];
this.pendingBytes = 0;
this.fallback = undefined;
this.finished = false;
}
switchToFallback() {
const sha256Js = new Sha256Js(this.secret);
for (const chunk of this.pending) {
sha256Js.update(chunk);
}
this.fallback = sha256Js;
this.pending = [];
this.pendingBytes = 0;
}
}
export function concatBytes(arrays, length) {
if (length === undefined) {
length = 0;
for (const bytes of arrays) {
length += bytes.byteLength;
}
}
const result = new Uint8Array(length);
let offset = 0;
for (const buf of arrays) {
result.set(buf, offset);
offset += buf.byteLength;
}
return result;
}
import type { Checksum } from "@smithy/types";
/**
* Pure JS CRC-32 implementation using the IEEE 802.3 polynomial.
* @see https://www.w3.org/TR/png/#D-CRCAppendix
* @public
*/
export declare class Crc32Js implements Checksum {
readonly digestLength = 4;
private checksum;
update(data: Uint8Array): void;
digest(): Promise<Uint8Array>;
reset(): void;
}
import type { Checksum } from "@smithy/types";
/**
* CRC-32 using Node.js zlib native implementation when available,
* falling back to the pure JS implementation.
* @public
*/
export interface Crc32Node extends Checksum {
readonly digestLength: 4;
}
/**
* @public
*/
export declare const Crc32Node: new () => Crc32Node;
import type { Checksum, SourceData } from "@smithy/types";
/**
* Pure-JS MD5 implementation. Used as fallback where node:crypto is unavailable.
*
* @public
*/
export declare class Md5Js implements Checksum {
readonly digestLength = 16;
private state;
private writeBuffer;
private bufferLength;
private bytesHashed;
update(sourceData: SourceData): void;
/**
* Non-destructive: works on copies so update() may continue after digest().
*/
digest(): Promise<Uint8Array>;
reset(): void;
}
import type { Checksum, SourceData } from "@smithy/types";
/**
* MD5 using Node.js crypto native implementation when available,
* falling back to the pure JS implementation.
* @public
*/
export interface Md5Node extends Checksum {
readonly digestLength: 16;
/**
* @override
*/
update(data: SourceData): void;
}
/**
* @public
*/
export declare const Md5Node: new () => Md5Node;
import type { Checksum, SourceData } from "@smithy/types";
/**
* Pure JS SHA-256 implementation with HMAC support.
* @see https://csrc.nist.gov/pubs/fips/180-4/upd1/final
* @public
*/
export declare class Sha256Js implements Checksum {
readonly digestLength = 32;
/** Eight 32-bit words representing the current hash state. */
private state;
/** Reused message schedule array (W), allocated on first use of hashBuffer. */
private w?;
/** Accumulates input bytes until a full 64-byte block is ready. */
private buffer;
private bufferLength;
private bytesHashed;
private finished;
private readonly inner?;
private readonly outer?;
constructor(secret?: SourceData);
update(data: SourceData): void;
digest(): Promise<Uint8Array>;
reset(): void;
private digestSync;
private static normalizeKey;
private hashBuffer;
private hashBufferWith;
}
import type { Checksum, SourceData } from "@smithy/types";
/**
* SHA-256 using Node.js crypto native implementation when available,
* falling back to the pure JS implementation.
* @public
*/
export interface Sha256Node extends Checksum {
readonly digestLength: 32;
}
/**
* @public
*/
export declare const Sha256Node: new (secret?: SourceData) => Sha256Node;
import type { Checksum, SourceData } from "@smithy/types";
/**
* SHA-256 using the Web Crypto API (crypto.subtle) when available,
* falling back to the pure JS implementation.
*
* Caution: this implementation is forced to buffer the data entirely.
* Use the pure-JS or Sha256Node implementations for large streaming data.
* @public
*/
export declare class Sha256WebCrypto implements Checksum {
readonly digestLength: 32;
private readonly secret?;
private pending;
private pendingBytes;
private fallback?;
private finished;
constructor(secret?: SourceData);
update(data: Uint8Array): void;
digest(): Promise<Uint8Array>;
reset(): void;
private switchToFallback;
}
/**
* This deliberately avoids differentiating to Buffer.concat in Node.js in favor of being isomorphic.
* This implementation pattern is highly recognizable/optimizable by JS engines.
* @internal
*/
export declare function concatBytes(arrays: Uint8Array[], length?: number): Uint8Array;
+355
-145

@@ -1,2 +0,2 @@

const { fromUtf8 } = require("@smithy/core/serde");
const { toUint8Array, concatBytes } = require("@smithy/core/serde");

@@ -21,30 +21,169 @@ async function blobReader(blob, onChunk, chunkSize = 1024 * 1024) {

const BLOCK_SIZE = 64;
const DIGEST_LENGTH = 16;
const INIT = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476];
class Md5Js {
digestLength = 16;
state = Uint32Array.from(INIT$1);
writeBuffer = new DataView(new ArrayBuffer(64));
bufferLength = 0;
bytesHashed = 0;
update(sourceData) {
const data = toUint8Array(sourceData);
let pos = 0;
let len = data.byteLength;
this.bytesHashed += len;
while (len > 0) {
this.writeBuffer.setUint8(this.bufferLength++, data[pos++]);
--len;
if (this.bufferLength === 64) {
compress(this.state, this.writeBuffer);
this.bufferLength = 0;
}
}
}
async digest() {
const state = Uint32Array.from(this.state);
const buf = new DataView(this.writeBuffer.buffer.slice(0));
let bufLen = this.bufferLength;
const bits = this.bytesHashed * 8;
buf.setUint8(bufLen++, 0x80);
if (this.bufferLength % 64 >= 56) {
for (let i = bufLen; i < 64; ++i) {
buf.setUint8(i, 0);
}
compress(state, buf);
bufLen = 0;
}
for (let i = bufLen; i < 56; ++i) {
buf.setUint8(i, 0);
}
buf.setUint32(56, bits >>> 0, true);
buf.setUint32(60, Math.floor(bits / 2 ** 32), true);
compress(state, buf);
const out = new Uint8Array(16);
const view = new DataView(out.buffer);
for (let i = 0; i < 4; ++i) {
view.setUint32(i * 4, state[i], true);
}
return out;
}
reset() {
this.state.set(INIT$1);
this.writeBuffer = new DataView(new ArrayBuffer(64));
this.bufferLength = 0;
this.bytesHashed = 0;
}
}
const INIT$1 = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476];
const M = 0xffffffff;
const S = Uint8Array.of(7, 12, 17, 22, 5, 9, 14, 20, 4, 11, 16, 23, 6, 10, 15, 21);
const T = Array.from({ length: 64 }, (_, i) => (Math.abs(Math.sin(i + 1)) * 2 ** 32) >>> 0);
function compress(state, block) {
let a = state[0], b = state[1], c = state[2], d = state[3];
for (let i = 0; i < 64; ++i) {
let f, g;
if (i < 16) {
f = (b & c) | (~b & d);
g = i;
}
else if (i < 32) {
f = (d & b) | (c & ~d);
g = (5 * i + 1) % 16;
}
else if (i < 48) {
f = b ^ c ^ d;
g = (3 * i + 5) % 16;
}
else {
f = c ^ (b | ~d);
g = (7 * i) % 16;
}
const x = block.getUint32(g * 4, true);
const tmp = d;
d = c;
c = b;
const s = S[(i >> 4) * 4 + (i & 3)];
const sum = (((a + f) & M) + ((x + T[i]) & M)) & M;
b = (b + (((sum << s) | (sum >>> (32 - s))) >>> 0)) & M;
a = tmp;
}
state[0] = (state[0] + a) & M;
state[1] = (state[1] + b) & M;
state[2] = (state[2] + c) & M;
state[3] = (state[3] + d) & M;
}
class Md5 {
state;
buffer;
bufferLength;
bytesHashed;
finished;
constructor() {
this.reset();
const CRC32_TABLE = new Uint32Array(256);
for (let i = 0; i < 256; ++i) {
let c = i;
for (let j = 0; j < 8; ++j) {
c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
}
update(sourceData) {
if (isEmptyData(sourceData)) {
CRC32_TABLE[i] = c >>> 0;
}
const ONES = 0xffff_ffff;
class Crc32Js {
digestLength = 4;
checksum = ONES;
update(data) {
for (let i = 0; i < data.length; ++i) {
this.checksum = (this.checksum >>> 8) ^ CRC32_TABLE[(this.checksum ^ data[i]) & 0xff];
}
}
async digest() {
const value = (this.checksum ^ ONES) >>> 0;
const out = new Uint8Array(4);
new DataView(out.buffer).setUint32(0, value, false);
return out;
}
reset() {
this.checksum = ONES;
}
}
const BLOCK = 64;
const DIGEST_LENGTH = 32;
const MAX_HASHABLE_LENGTH = 2 ** 53 - 1;
class Sha256Js {
digestLength = DIGEST_LENGTH;
state = Int32Array.from(INIT);
w;
buffer = new Uint8Array(64);
bufferLength = 0;
bytesHashed = 0;
finished = false;
inner;
outer;
constructor(secret) {
if (secret) {
const key = Sha256Js.normalizeKey(secret);
this.inner = new Sha256Js();
this.outer = new Sha256Js();
const { inner, outer } = this;
const pad = new Uint8Array(BLOCK * 2);
for (let i = 0; i < BLOCK; ++i) {
pad[i] = 0x36 ^ key[i];
pad[i + BLOCK] = 0x5c ^ key[i];
}
inner.update(pad.subarray(0, BLOCK));
outer.update(pad.subarray(BLOCK));
}
}
update(data) {
if (this.finished) {
throw new Error("Attempted to update an already finished HMAC.");
}
if (this.inner) {
this.inner.update(data);
return;
}
else if (this.finished) {
throw new Error("Attempted to update an already finished hash.");
}
const data = convertToBuffer(sourceData);
const chunk = toUint8Array(data);
let position = 0;
let { byteLength } = data;
let { byteLength } = chunk;
this.bytesHashed += byteLength;
if (this.bytesHashed * 8 > MAX_HASHABLE_LENGTH) {
throw new Error("Cannot hash more than 2^53 - 1 bits");
}
while (byteLength > 0) {
this.buffer.setUint8(this.bufferLength++, data[position++]);
this.buffer[this.bufferLength++] = chunk[position++];
byteLength--;
if (this.bufferLength === BLOCK_SIZE) {
if (this.bufferLength === BLOCK) {
this.hashBuffer();

@@ -56,137 +195,196 @@ this.bufferLength = 0;

async digest() {
if (!this.finished) {
const { buffer, bufferLength: undecoratedLength, bytesHashed } = this;
const bitsHashed = bytesHashed * 8;
buffer.setUint8(this.bufferLength++, 0b10000000);
if (undecoratedLength % BLOCK_SIZE >= BLOCK_SIZE - 8) {
for (let i = this.bufferLength; i < BLOCK_SIZE; i++) {
buffer.setUint8(i, 0);
}
this.hashBuffer();
this.bufferLength = 0;
const { inner, outer } = this;
if (inner && outer) {
if (this.finished) {
throw new Error("Attempted to digest an already finished HMAC.");
}
for (let i = this.bufferLength; i < BLOCK_SIZE - 8; i++) {
buffer.setUint8(i, 0);
}
buffer.setUint32(BLOCK_SIZE - 8, bitsHashed >>> 0, true);
buffer.setUint32(BLOCK_SIZE - 4, Math.floor(bitsHashed / 0x100000000), true);
this.hashBuffer();
this.finished = true;
const innerDigest = inner.digestSync();
outer.update(innerDigest);
return outer.digestSync();
}
const out = new DataView(new ArrayBuffer(DIGEST_LENGTH));
for (let i = 0; i < 4; i++) {
out.setUint32(i * 4, this.state[i], true);
}
return new Uint8Array(out.buffer, out.byteOffset, out.byteLength);
return this.digestSync();
}
hashBuffer() {
const { buffer, state } = this;
let a = state[0], b = state[1], c = state[2], d = state[3];
a = ff(a, b, c, d, buffer.getUint32(0, true), 7, 0xd76aa478);
d = ff(d, a, b, c, buffer.getUint32(4, true), 12, 0xe8c7b756);
c = ff(c, d, a, b, buffer.getUint32(8, true), 17, 0x242070db);
b = ff(b, c, d, a, buffer.getUint32(12, true), 22, 0xc1bdceee);
a = ff(a, b, c, d, buffer.getUint32(16, true), 7, 0xf57c0faf);
d = ff(d, a, b, c, buffer.getUint32(20, true), 12, 0x4787c62a);
c = ff(c, d, a, b, buffer.getUint32(24, true), 17, 0xa8304613);
b = ff(b, c, d, a, buffer.getUint32(28, true), 22, 0xfd469501);
a = ff(a, b, c, d, buffer.getUint32(32, true), 7, 0x698098d8);
d = ff(d, a, b, c, buffer.getUint32(36, true), 12, 0x8b44f7af);
c = ff(c, d, a, b, buffer.getUint32(40, true), 17, 0xffff5bb1);
b = ff(b, c, d, a, buffer.getUint32(44, true), 22, 0x895cd7be);
a = ff(a, b, c, d, buffer.getUint32(48, true), 7, 0x6b901122);
d = ff(d, a, b, c, buffer.getUint32(52, true), 12, 0xfd987193);
c = ff(c, d, a, b, buffer.getUint32(56, true), 17, 0xa679438e);
b = ff(b, c, d, a, buffer.getUint32(60, true), 22, 0x49b40821);
a = gg(a, b, c, d, buffer.getUint32(4, true), 5, 0xf61e2562);
d = gg(d, a, b, c, buffer.getUint32(24, true), 9, 0xc040b340);
c = gg(c, d, a, b, buffer.getUint32(44, true), 14, 0x265e5a51);
b = gg(b, c, d, a, buffer.getUint32(0, true), 20, 0xe9b6c7aa);
a = gg(a, b, c, d, buffer.getUint32(20, true), 5, 0xd62f105d);
d = gg(d, a, b, c, buffer.getUint32(40, true), 9, 0x02441453);
c = gg(c, d, a, b, buffer.getUint32(60, true), 14, 0xd8a1e681);
b = gg(b, c, d, a, buffer.getUint32(16, true), 20, 0xe7d3fbc8);
a = gg(a, b, c, d, buffer.getUint32(36, true), 5, 0x21e1cde6);
d = gg(d, a, b, c, buffer.getUint32(56, true), 9, 0xc33707d6);
c = gg(c, d, a, b, buffer.getUint32(12, true), 14, 0xf4d50d87);
b = gg(b, c, d, a, buffer.getUint32(32, true), 20, 0x455a14ed);
a = gg(a, b, c, d, buffer.getUint32(52, true), 5, 0xa9e3e905);
d = gg(d, a, b, c, buffer.getUint32(8, true), 9, 0xfcefa3f8);
c = gg(c, d, a, b, buffer.getUint32(28, true), 14, 0x676f02d9);
b = gg(b, c, d, a, buffer.getUint32(48, true), 20, 0x8d2a4c8a);
a = hh(a, b, c, d, buffer.getUint32(20, true), 4, 0xfffa3942);
d = hh(d, a, b, c, buffer.getUint32(32, true), 11, 0x8771f681);
c = hh(c, d, a, b, buffer.getUint32(44, true), 16, 0x6d9d6122);
b = hh(b, c, d, a, buffer.getUint32(56, true), 23, 0xfde5380c);
a = hh(a, b, c, d, buffer.getUint32(4, true), 4, 0xa4beea44);
d = hh(d, a, b, c, buffer.getUint32(16, true), 11, 0x4bdecfa9);
c = hh(c, d, a, b, buffer.getUint32(28, true), 16, 0xf6bb4b60);
b = hh(b, c, d, a, buffer.getUint32(40, true), 23, 0xbebfbc70);
a = hh(a, b, c, d, buffer.getUint32(52, true), 4, 0x289b7ec6);
d = hh(d, a, b, c, buffer.getUint32(0, true), 11, 0xeaa127fa);
c = hh(c, d, a, b, buffer.getUint32(12, true), 16, 0xd4ef3085);
b = hh(b, c, d, a, buffer.getUint32(24, true), 23, 0x04881d05);
a = hh(a, b, c, d, buffer.getUint32(36, true), 4, 0xd9d4d039);
d = hh(d, a, b, c, buffer.getUint32(48, true), 11, 0xe6db99e5);
c = hh(c, d, a, b, buffer.getUint32(60, true), 16, 0x1fa27cf8);
b = hh(b, c, d, a, buffer.getUint32(8, true), 23, 0xc4ac5665);
a = ii(a, b, c, d, buffer.getUint32(0, true), 6, 0xf4292244);
d = ii(d, a, b, c, buffer.getUint32(28, true), 10, 0x432aff97);
c = ii(c, d, a, b, buffer.getUint32(56, true), 15, 0xab9423a7);
b = ii(b, c, d, a, buffer.getUint32(20, true), 21, 0xfc93a039);
a = ii(a, b, c, d, buffer.getUint32(48, true), 6, 0x655b59c3);
d = ii(d, a, b, c, buffer.getUint32(12, true), 10, 0x8f0ccc92);
c = ii(c, d, a, b, buffer.getUint32(40, true), 15, 0xffeff47d);
b = ii(b, c, d, a, buffer.getUint32(4, true), 21, 0x85845dd1);
a = ii(a, b, c, d, buffer.getUint32(32, true), 6, 0x6fa87e4f);
d = ii(d, a, b, c, buffer.getUint32(60, true), 10, 0xfe2ce6e0);
c = ii(c, d, a, b, buffer.getUint32(24, true), 15, 0xa3014314);
b = ii(b, c, d, a, buffer.getUint32(52, true), 21, 0x4e0811a1);
a = ii(a, b, c, d, buffer.getUint32(16, true), 6, 0xf7537e82);
d = ii(d, a, b, c, buffer.getUint32(44, true), 10, 0xbd3af235);
c = ii(c, d, a, b, buffer.getUint32(8, true), 15, 0x2ad7d2bb);
b = ii(b, c, d, a, buffer.getUint32(36, true), 21, 0xeb86d391);
state[0] = (a + state[0]) & 0xffffffff;
state[1] = (b + state[1]) & 0xffffffff;
state[2] = (c + state[2]) & 0xffffffff;
state[3] = (d + state[3]) & 0xffffffff;
}
reset() {
this.state = Uint32Array.from(INIT);
this.buffer = new DataView(new ArrayBuffer(BLOCK_SIZE));
this.state = Int32Array.from(INIT);
this.buffer = new Uint8Array(64);
this.bufferLength = 0;
this.bytesHashed = 0;
this.finished = false;
}
}
function cmn(q, a, b, x, s, t) {
a = (((a + q) & 0xffffffff) + ((x + t) & 0xffffffff)) & 0xffffffff;
return (((a << s) | (a >>> (32 - s))) + b) & 0xffffffff;
}
function ff(a, b, c, d, x, s, t) {
return cmn((b & c) | (~b & d), a, b, x, s, t);
}
function gg(a, b, c, d, x, s, t) {
return cmn((b & d) | (c & ~d), a, b, x, s, t);
}
function hh(a, b, c, d, x, s, t) {
return cmn(b ^ c ^ d, a, b, x, s, t);
}
function ii(a, b, c, d, x, s, t) {
return cmn(c ^ (b | ~d), a, b, x, s, t);
}
function isEmptyData(data) {
if (typeof data === "string") {
return data.length === 0;
digestSync() {
const state = this.state.slice();
const buffer = this.buffer.slice();
let bufferLength = this.bufferLength;
const bitsHashed = this.bytesHashed * 8;
const bufferView = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
bufferView.setUint8(bufferLength++, 0x80);
if ((bufferLength - 1) % BLOCK >= BLOCK - 8) {
for (let i = bufferLength; i < BLOCK; ++i) {
bufferView.setUint8(i, 0);
}
this.hashBufferWith(state, buffer);
bufferLength = 0;
}
for (let i = bufferLength; i < BLOCK - 8; ++i) {
bufferView.setUint8(i, 0);
}
bufferView.setUint32(BLOCK - 8, Math.floor(bitsHashed / 0x100000000), false);
bufferView.setUint32(BLOCK - 4, bitsHashed, false);
this.hashBufferWith(state, buffer);
const out = new Uint8Array(DIGEST_LENGTH);
for (let i = 0; i < 8; ++i) {
out[i * 4] = (state[i] >>> 24) & 0xff;
out[i * 4 + 1] = (state[i] >>> 16) & 0xff;
out[i * 4 + 2] = (state[i] >>> 8) & 0xff;
out[i * 4 + 3] = (state[i] >>> 0) & 0xff;
}
return out;
}
return data.byteLength === 0;
static normalizeKey(secret) {
const key = toUint8Array(secret);
if (key.byteLength > BLOCK) {
const h = new Sha256Js();
h.update(key);
const out = h.digestSync();
const padded = new Uint8Array(BLOCK);
padded.set(out);
return padded;
}
if (key.byteLength < BLOCK) {
const padded = new Uint8Array(BLOCK);
padded.set(key);
return padded;
}
return key;
}
hashBuffer() {
this.hashBufferWith(this.state, this.buffer);
}
hashBufferWith(state, buffer) {
const w = (this.w ??= new Int32Array(64));
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];
for (let i = 0; i < BLOCK; ++i) {
if (i < 16) {
w[i] =
((buffer[i * 4] & 0xff) << 24) |
((buffer[i * 4 + 1] & 0xff) << 16) |
((buffer[i * 4 + 2] & 0xff) << 8) |
(buffer[i * 4 + 3] & 0xff);
}
else {
let u = w[i - 2];
const t1 = ((u >>> 17) | (u << 15)) ^ ((u >>> 19) | (u << 13)) ^ (u >>> 10);
u = w[i - 15];
const t2 = ((u >>> 7) | (u << 25)) ^ ((u >>> 18) | (u << 14)) ^ (u >>> 3);
w[i] = ((t1 + w[i - 7]) | 0) + ((t2 + w[i - 16]) | 0);
}
const t1 = ((((((s4 >>> 6) | (s4 << 26)) ^ ((s4 >>> 11) | (s4 << 21)) ^ ((s4 >>> 25) | (s4 << 7))) +
((s4 & s5) ^ (~s4 & s6))) |
0) +
((s7 + ((K[i] + w[i]) | 0)) | 0)) |
0;
const t2 = ((((s0 >>> 2) | (s0 << 30)) ^ ((s0 >>> 13) | (s0 << 19)) ^ ((s0 >>> 22) | (s0 << 10))) +
((s0 & s1) ^ (s0 & s2) ^ (s1 & s2))) |
0;
s7 = s6;
s6 = s5;
s5 = s4;
s4 = (s3 + t1) | 0;
s3 = s2;
s2 = s1;
s1 = s0;
s0 = (t1 + t2) | 0;
}
state[0] += s0;
state[1] += s1;
state[2] += s2;
state[3] += s3;
state[4] += s4;
state[5] += s5;
state[6] += s6;
state[7] += s7;
}
}
function convertToBuffer(data) {
if (typeof data === "string") {
return fromUtf8(data);
const INIT = new Int32Array([
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
]);
const K = new Int32Array([
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
]);
const { digest, sign, importKey } = globalThis?.crypto?.subtle ?? {};
const subtle = typeof digest === "function" && typeof sign === "function" && typeof importKey === "function"
? globalThis.crypto.subtle
: undefined;
const MAX_PENDING_BYTES = 8 * 1024 * 1024;
class Sha256WebCrypto {
digestLength = 32;
secret;
pending = [];
pendingBytes = 0;
fallback;
finished = false;
constructor(secret) {
if (secret) {
this.secret = toUint8Array(secret);
}
}
if (ArrayBuffer.isView(data)) {
return new Uint8Array(data.buffer, data.byteOffset, data.byteLength / Uint8Array.BYTES_PER_ELEMENT);
update(data) {
if (this.finished) {
throw new Error("Attempted to update an already finished HMAC.");
}
if (this.fallback) {
this.fallback.update(data);
return;
}
this.pending.push(data.slice());
this.pendingBytes += data.byteLength;
if (this.pendingBytes >= MAX_PENDING_BYTES) {
this.switchToFallback();
}
}
return new Uint8Array(data);
async digest() {
if (this.fallback) {
return this.fallback.digest();
}
if (this.secret && this.finished) {
throw new Error("Attempted to digest an already finished HMAC.");
}
const data = concatBytes(this.pending);
if (subtle) {
if (this.secret) {
this.finished = true;
const key = await subtle.importKey("raw", this.secret, { name: "HMAC", hash: "SHA-256" }, false, ["sign"]);
const sig = await subtle.sign("HMAC", key, data);
return new Uint8Array(sig);
}
const hash = await subtle.digest("SHA-256", data);
return new Uint8Array(hash);
}
const sha256 = new Sha256Js(this.secret);
sha256.update(data);
return sha256.digest();
}
reset() {
this.pending = [];
this.pendingBytes = 0;
this.fallback = undefined;
this.finished = false;
}
switchToFallback() {
const sha256Js = new Sha256Js(this.secret);
for (const chunk of this.pending) {
sha256Js.update(chunk);
}
this.fallback = sha256Js;
this.pending = [];
this.pendingBytes = 0;
}
}

@@ -197,4 +395,16 @@

const readableStreamHasher = no;
const Md5Node = no;
const Crc32Node = no;
const Sha256Node = no;
exports.Md5 = Md5;
exports.Crc32 = Crc32Js;
exports.Crc32Js = Crc32Js;
exports.Crc32Node = Crc32Node;
exports.Md5 = Md5Js;
exports.Md5Js = Md5Js;
exports.Md5Node = Md5Node;
exports.Sha256 = Sha256WebCrypto;
exports.Sha256Js = Sha256Js;
exports.Sha256Node = Sha256Node;
exports.Sha256WebCrypto = Sha256WebCrypto;
exports.blobHasher = blobHasher;

@@ -201,0 +411,0 @@ exports.blobReader = blobReader;

const { createReadStream } = require("node:fs");
const { Writable } = require("node:stream");
const { toUint8Array, fromUtf8 } = require("@smithy/core/serde");
const { toUint8Array, concatBytes } = require("@smithy/core/serde");
const { createHash, createHmac } = require("node:crypto");
const zlib = require("node:zlib");

@@ -82,30 +84,216 @@ async function blobReader(blob, onChunk, chunkSize = 1024 * 1024) {

const BLOCK_SIZE = 64;
const DIGEST_LENGTH = 16;
const INIT = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476];
class Md5Js {
digestLength = 16;
state = Uint32Array.from(INIT$1);
writeBuffer = new DataView(new ArrayBuffer(64));
bufferLength = 0;
bytesHashed = 0;
update(sourceData) {
const data = toUint8Array(sourceData);
let pos = 0;
let len = data.byteLength;
this.bytesHashed += len;
while (len > 0) {
this.writeBuffer.setUint8(this.bufferLength++, data[pos++]);
--len;
if (this.bufferLength === 64) {
compress(this.state, this.writeBuffer);
this.bufferLength = 0;
}
}
}
async digest() {
const state = Uint32Array.from(this.state);
const buf = new DataView(this.writeBuffer.buffer.slice(0));
let bufLen = this.bufferLength;
const bits = this.bytesHashed * 8;
buf.setUint8(bufLen++, 0x80);
if (this.bufferLength % 64 >= 56) {
for (let i = bufLen; i < 64; ++i) {
buf.setUint8(i, 0);
}
compress(state, buf);
bufLen = 0;
}
for (let i = bufLen; i < 56; ++i) {
buf.setUint8(i, 0);
}
buf.setUint32(56, bits >>> 0, true);
buf.setUint32(60, Math.floor(bits / 2 ** 32), true);
compress(state, buf);
const out = new Uint8Array(16);
const view = new DataView(out.buffer);
for (let i = 0; i < 4; ++i) {
view.setUint32(i * 4, state[i], true);
}
return out;
}
reset() {
this.state.set(INIT$1);
this.writeBuffer = new DataView(new ArrayBuffer(64));
this.bufferLength = 0;
this.bytesHashed = 0;
}
}
const INIT$1 = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476];
const M = 0xffffffff;
const S = Uint8Array.of(7, 12, 17, 22, 5, 9, 14, 20, 4, 11, 16, 23, 6, 10, 15, 21);
const T = Array.from({ length: 64 }, (_, i) => (Math.abs(Math.sin(i + 1)) * 2 ** 32) >>> 0);
function compress(state, block) {
let a = state[0], b = state[1], c = state[2], d = state[3];
for (let i = 0; i < 64; ++i) {
let f, g;
if (i < 16) {
f = (b & c) | (~b & d);
g = i;
}
else if (i < 32) {
f = (d & b) | (c & ~d);
g = (5 * i + 1) % 16;
}
else if (i < 48) {
f = b ^ c ^ d;
g = (3 * i + 5) % 16;
}
else {
f = c ^ (b | ~d);
g = (7 * i) % 16;
}
const x = block.getUint32(g * 4, true);
const tmp = d;
d = c;
c = b;
const s = S[(i >> 4) * 4 + (i & 3)];
const sum = (((a + f) & M) + ((x + T[i]) & M)) & M;
b = (b + (((sum << s) | (sum >>> (32 - s))) >>> 0)) & M;
a = tmp;
}
state[0] = (state[0] + a) & M;
state[1] = (state[1] + b) & M;
state[2] = (state[2] + c) & M;
state[3] = (state[3] + d) & M;
}
class Md5 {
state;
buffer;
bufferLength;
bytesHashed;
finished;
constructor() {
this.reset();
const hasNativeCrypto$1 = (() => {
try {
createHash("md5");
return true;
}
update(sourceData) {
if (isEmptyData(sourceData)) {
catch {
return false;
}
})();
const Md5Node = hasNativeCrypto$1 ? buildNativeClass$2() : Md5Js;
function buildNativeClass$2() {
return class Md5Node {
digestLength = 16;
hash = createHash("md5");
update(data) {
this.hash.update(toUint8Array(data));
}
async digest() {
const buf = this.hash.copy().digest();
return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
}
reset() {
this.hash = createHash("md5");
}
};
}
const CRC32_TABLE = new Uint32Array(256);
for (let i = 0; i < 256; ++i) {
let c = i;
for (let j = 0; j < 8; ++j) {
c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
}
CRC32_TABLE[i] = c >>> 0;
}
const ONES = 0xffff_ffff;
class Crc32Js {
digestLength = 4;
checksum = ONES;
update(data) {
for (let i = 0; i < data.length; ++i) {
this.checksum = (this.checksum >>> 8) ^ CRC32_TABLE[(this.checksum ^ data[i]) & 0xff];
}
}
async digest() {
const value = (this.checksum ^ ONES) >>> 0;
const out = new Uint8Array(4);
new DataView(out.buffer).setUint32(0, value, false);
return out;
}
reset() {
this.checksum = ONES;
}
}
const zlibCrc32 = typeof zlib.crc32 === "function" ? zlib.crc32 : undefined;
const Crc32Node = zlibCrc32 ? buildNativeClass$1(zlibCrc32) : Crc32Js;
function buildNativeClass$1(nativeCrc32) {
return class Crc32Node {
digestLength = 4;
value = 0;
update(data) {
this.value = nativeCrc32(data, this.value);
}
async digest() {
const out = new Uint8Array(4);
new DataView(out.buffer).setUint32(0, this.value >>> 0, false);
return out;
}
reset() {
this.value = 0;
}
};
}
const BLOCK = 64;
const DIGEST_LENGTH = 32;
const MAX_HASHABLE_LENGTH = 2 ** 53 - 1;
class Sha256Js {
digestLength = DIGEST_LENGTH;
state = Int32Array.from(INIT);
w;
buffer = new Uint8Array(64);
bufferLength = 0;
bytesHashed = 0;
finished = false;
inner;
outer;
constructor(secret) {
if (secret) {
const key = Sha256Js.normalizeKey(secret);
this.inner = new Sha256Js();
this.outer = new Sha256Js();
const { inner, outer } = this;
const pad = new Uint8Array(BLOCK * 2);
for (let i = 0; i < BLOCK; ++i) {
pad[i] = 0x36 ^ key[i];
pad[i + BLOCK] = 0x5c ^ key[i];
}
inner.update(pad.subarray(0, BLOCK));
outer.update(pad.subarray(BLOCK));
}
}
update(data) {
if (this.finished) {
throw new Error("Attempted to update an already finished HMAC.");
}
if (this.inner) {
this.inner.update(data);
return;
}
else if (this.finished) {
throw new Error("Attempted to update an already finished hash.");
}
const data = convertToBuffer(sourceData);
const chunk = toUint8Array(data);
let position = 0;
let { byteLength } = data;
let { byteLength } = chunk;
this.bytesHashed += byteLength;
if (this.bytesHashed * 8 > MAX_HASHABLE_LENGTH) {
throw new Error("Cannot hash more than 2^53 - 1 bits");
}
while (byteLength > 0) {
this.buffer.setUint8(this.bufferLength++, data[position++]);
this.buffer[this.bufferLength++] = chunk[position++];
byteLength--;
if (this.bufferLength === BLOCK_SIZE) {
if (this.bufferLength === BLOCK) {
this.hashBuffer();

@@ -117,140 +305,266 @@ this.bufferLength = 0;

async digest() {
if (!this.finished) {
const { buffer, bufferLength: undecoratedLength, bytesHashed } = this;
const bitsHashed = bytesHashed * 8;
buffer.setUint8(this.bufferLength++, 0b10000000);
if (undecoratedLength % BLOCK_SIZE >= BLOCK_SIZE - 8) {
for (let i = this.bufferLength; i < BLOCK_SIZE; i++) {
buffer.setUint8(i, 0);
}
this.hashBuffer();
this.bufferLength = 0;
const { inner, outer } = this;
if (inner && outer) {
if (this.finished) {
throw new Error("Attempted to digest an already finished HMAC.");
}
for (let i = this.bufferLength; i < BLOCK_SIZE - 8; i++) {
buffer.setUint8(i, 0);
}
buffer.setUint32(BLOCK_SIZE - 8, bitsHashed >>> 0, true);
buffer.setUint32(BLOCK_SIZE - 4, Math.floor(bitsHashed / 0x100000000), true);
this.hashBuffer();
this.finished = true;
const innerDigest = inner.digestSync();
outer.update(innerDigest);
return outer.digestSync();
}
const out = new DataView(new ArrayBuffer(DIGEST_LENGTH));
for (let i = 0; i < 4; i++) {
out.setUint32(i * 4, this.state[i], true);
}
return new Uint8Array(out.buffer, out.byteOffset, out.byteLength);
return this.digestSync();
}
hashBuffer() {
const { buffer, state } = this;
let a = state[0], b = state[1], c = state[2], d = state[3];
a = ff(a, b, c, d, buffer.getUint32(0, true), 7, 0xd76aa478);
d = ff(d, a, b, c, buffer.getUint32(4, true), 12, 0xe8c7b756);
c = ff(c, d, a, b, buffer.getUint32(8, true), 17, 0x242070db);
b = ff(b, c, d, a, buffer.getUint32(12, true), 22, 0xc1bdceee);
a = ff(a, b, c, d, buffer.getUint32(16, true), 7, 0xf57c0faf);
d = ff(d, a, b, c, buffer.getUint32(20, true), 12, 0x4787c62a);
c = ff(c, d, a, b, buffer.getUint32(24, true), 17, 0xa8304613);
b = ff(b, c, d, a, buffer.getUint32(28, true), 22, 0xfd469501);
a = ff(a, b, c, d, buffer.getUint32(32, true), 7, 0x698098d8);
d = ff(d, a, b, c, buffer.getUint32(36, true), 12, 0x8b44f7af);
c = ff(c, d, a, b, buffer.getUint32(40, true), 17, 0xffff5bb1);
b = ff(b, c, d, a, buffer.getUint32(44, true), 22, 0x895cd7be);
a = ff(a, b, c, d, buffer.getUint32(48, true), 7, 0x6b901122);
d = ff(d, a, b, c, buffer.getUint32(52, true), 12, 0xfd987193);
c = ff(c, d, a, b, buffer.getUint32(56, true), 17, 0xa679438e);
b = ff(b, c, d, a, buffer.getUint32(60, true), 22, 0x49b40821);
a = gg(a, b, c, d, buffer.getUint32(4, true), 5, 0xf61e2562);
d = gg(d, a, b, c, buffer.getUint32(24, true), 9, 0xc040b340);
c = gg(c, d, a, b, buffer.getUint32(44, true), 14, 0x265e5a51);
b = gg(b, c, d, a, buffer.getUint32(0, true), 20, 0xe9b6c7aa);
a = gg(a, b, c, d, buffer.getUint32(20, true), 5, 0xd62f105d);
d = gg(d, a, b, c, buffer.getUint32(40, true), 9, 0x02441453);
c = gg(c, d, a, b, buffer.getUint32(60, true), 14, 0xd8a1e681);
b = gg(b, c, d, a, buffer.getUint32(16, true), 20, 0xe7d3fbc8);
a = gg(a, b, c, d, buffer.getUint32(36, true), 5, 0x21e1cde6);
d = gg(d, a, b, c, buffer.getUint32(56, true), 9, 0xc33707d6);
c = gg(c, d, a, b, buffer.getUint32(12, true), 14, 0xf4d50d87);
b = gg(b, c, d, a, buffer.getUint32(32, true), 20, 0x455a14ed);
a = gg(a, b, c, d, buffer.getUint32(52, true), 5, 0xa9e3e905);
d = gg(d, a, b, c, buffer.getUint32(8, true), 9, 0xfcefa3f8);
c = gg(c, d, a, b, buffer.getUint32(28, true), 14, 0x676f02d9);
b = gg(b, c, d, a, buffer.getUint32(48, true), 20, 0x8d2a4c8a);
a = hh(a, b, c, d, buffer.getUint32(20, true), 4, 0xfffa3942);
d = hh(d, a, b, c, buffer.getUint32(32, true), 11, 0x8771f681);
c = hh(c, d, a, b, buffer.getUint32(44, true), 16, 0x6d9d6122);
b = hh(b, c, d, a, buffer.getUint32(56, true), 23, 0xfde5380c);
a = hh(a, b, c, d, buffer.getUint32(4, true), 4, 0xa4beea44);
d = hh(d, a, b, c, buffer.getUint32(16, true), 11, 0x4bdecfa9);
c = hh(c, d, a, b, buffer.getUint32(28, true), 16, 0xf6bb4b60);
b = hh(b, c, d, a, buffer.getUint32(40, true), 23, 0xbebfbc70);
a = hh(a, b, c, d, buffer.getUint32(52, true), 4, 0x289b7ec6);
d = hh(d, a, b, c, buffer.getUint32(0, true), 11, 0xeaa127fa);
c = hh(c, d, a, b, buffer.getUint32(12, true), 16, 0xd4ef3085);
b = hh(b, c, d, a, buffer.getUint32(24, true), 23, 0x04881d05);
a = hh(a, b, c, d, buffer.getUint32(36, true), 4, 0xd9d4d039);
d = hh(d, a, b, c, buffer.getUint32(48, true), 11, 0xe6db99e5);
c = hh(c, d, a, b, buffer.getUint32(60, true), 16, 0x1fa27cf8);
b = hh(b, c, d, a, buffer.getUint32(8, true), 23, 0xc4ac5665);
a = ii(a, b, c, d, buffer.getUint32(0, true), 6, 0xf4292244);
d = ii(d, a, b, c, buffer.getUint32(28, true), 10, 0x432aff97);
c = ii(c, d, a, b, buffer.getUint32(56, true), 15, 0xab9423a7);
b = ii(b, c, d, a, buffer.getUint32(20, true), 21, 0xfc93a039);
a = ii(a, b, c, d, buffer.getUint32(48, true), 6, 0x655b59c3);
d = ii(d, a, b, c, buffer.getUint32(12, true), 10, 0x8f0ccc92);
c = ii(c, d, a, b, buffer.getUint32(40, true), 15, 0xffeff47d);
b = ii(b, c, d, a, buffer.getUint32(4, true), 21, 0x85845dd1);
a = ii(a, b, c, d, buffer.getUint32(32, true), 6, 0x6fa87e4f);
d = ii(d, a, b, c, buffer.getUint32(60, true), 10, 0xfe2ce6e0);
c = ii(c, d, a, b, buffer.getUint32(24, true), 15, 0xa3014314);
b = ii(b, c, d, a, buffer.getUint32(52, true), 21, 0x4e0811a1);
a = ii(a, b, c, d, buffer.getUint32(16, true), 6, 0xf7537e82);
d = ii(d, a, b, c, buffer.getUint32(44, true), 10, 0xbd3af235);
c = ii(c, d, a, b, buffer.getUint32(8, true), 15, 0x2ad7d2bb);
b = ii(b, c, d, a, buffer.getUint32(36, true), 21, 0xeb86d391);
state[0] = (a + state[0]) & 0xffffffff;
state[1] = (b + state[1]) & 0xffffffff;
state[2] = (c + state[2]) & 0xffffffff;
state[3] = (d + state[3]) & 0xffffffff;
}
reset() {
this.state = Uint32Array.from(INIT);
this.buffer = new DataView(new ArrayBuffer(BLOCK_SIZE));
this.state = Int32Array.from(INIT);
this.buffer = new Uint8Array(64);
this.bufferLength = 0;
this.bytesHashed = 0;
this.finished = false;
}
digestSync() {
const state = this.state.slice();
const buffer = this.buffer.slice();
let bufferLength = this.bufferLength;
const bitsHashed = this.bytesHashed * 8;
const bufferView = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
bufferView.setUint8(bufferLength++, 0x80);
if ((bufferLength - 1) % BLOCK >= BLOCK - 8) {
for (let i = bufferLength; i < BLOCK; ++i) {
bufferView.setUint8(i, 0);
}
this.hashBufferWith(state, buffer);
bufferLength = 0;
}
for (let i = bufferLength; i < BLOCK - 8; ++i) {
bufferView.setUint8(i, 0);
}
bufferView.setUint32(BLOCK - 8, Math.floor(bitsHashed / 0x100000000), false);
bufferView.setUint32(BLOCK - 4, bitsHashed, false);
this.hashBufferWith(state, buffer);
const out = new Uint8Array(DIGEST_LENGTH);
for (let i = 0; i < 8; ++i) {
out[i * 4] = (state[i] >>> 24) & 0xff;
out[i * 4 + 1] = (state[i] >>> 16) & 0xff;
out[i * 4 + 2] = (state[i] >>> 8) & 0xff;
out[i * 4 + 3] = (state[i] >>> 0) & 0xff;
}
return out;
}
static normalizeKey(secret) {
const key = toUint8Array(secret);
if (key.byteLength > BLOCK) {
const h = new Sha256Js();
h.update(key);
const out = h.digestSync();
const padded = new Uint8Array(BLOCK);
padded.set(out);
return padded;
}
if (key.byteLength < BLOCK) {
const padded = new Uint8Array(BLOCK);
padded.set(key);
return padded;
}
return key;
}
hashBuffer() {
this.hashBufferWith(this.state, this.buffer);
}
hashBufferWith(state, buffer) {
const w = (this.w ??= new Int32Array(64));
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];
for (let i = 0; i < BLOCK; ++i) {
if (i < 16) {
w[i] =
((buffer[i * 4] & 0xff) << 24) |
((buffer[i * 4 + 1] & 0xff) << 16) |
((buffer[i * 4 + 2] & 0xff) << 8) |
(buffer[i * 4 + 3] & 0xff);
}
else {
let u = w[i - 2];
const t1 = ((u >>> 17) | (u << 15)) ^ ((u >>> 19) | (u << 13)) ^ (u >>> 10);
u = w[i - 15];
const t2 = ((u >>> 7) | (u << 25)) ^ ((u >>> 18) | (u << 14)) ^ (u >>> 3);
w[i] = ((t1 + w[i - 7]) | 0) + ((t2 + w[i - 16]) | 0);
}
const t1 = ((((((s4 >>> 6) | (s4 << 26)) ^ ((s4 >>> 11) | (s4 << 21)) ^ ((s4 >>> 25) | (s4 << 7))) +
((s4 & s5) ^ (~s4 & s6))) |
0) +
((s7 + ((K[i] + w[i]) | 0)) | 0)) |
0;
const t2 = ((((s0 >>> 2) | (s0 << 30)) ^ ((s0 >>> 13) | (s0 << 19)) ^ ((s0 >>> 22) | (s0 << 10))) +
((s0 & s1) ^ (s0 & s2) ^ (s1 & s2))) |
0;
s7 = s6;
s6 = s5;
s5 = s4;
s4 = (s3 + t1) | 0;
s3 = s2;
s2 = s1;
s1 = s0;
s0 = (t1 + t2) | 0;
}
state[0] += s0;
state[1] += s1;
state[2] += s2;
state[3] += s3;
state[4] += s4;
state[5] += s5;
state[6] += s6;
state[7] += s7;
}
}
function cmn(q, a, b, x, s, t) {
a = (((a + q) & 0xffffffff) + ((x + t) & 0xffffffff)) & 0xffffffff;
return (((a << s) | (a >>> (32 - s))) + b) & 0xffffffff;
}
function ff(a, b, c, d, x, s, t) {
return cmn((b & c) | (~b & d), a, b, x, s, t);
}
function gg(a, b, c, d, x, s, t) {
return cmn((b & d) | (c & ~d), a, b, x, s, t);
}
function hh(a, b, c, d, x, s, t) {
return cmn(b ^ c ^ d, a, b, x, s, t);
}
function ii(a, b, c, d, x, s, t) {
return cmn(c ^ (b | ~d), a, b, x, s, t);
}
function isEmptyData(data) {
if (typeof data === "string") {
return data.length === 0;
const INIT = new Int32Array([
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
]);
const K = new Int32Array([
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
]);
const hasNativeCrypto = (() => {
try {
createHash("sha256");
return true;
}
return data.byteLength === 0;
catch {
return false;
}
})();
const Sha256Node = hasNativeCrypto ? buildNativeClass() : Sha256Js;
function buildNativeClass() {
return class Sha256Node {
digestLength = 32;
secret;
hash;
isHmac;
finished = false;
constructor(secret) {
this.secret = secret;
this.isHmac = !!secret;
this.hash = this.createHash();
}
update(data) {
if (this.finished) {
throw new Error("Attempted to update an already finished hash.");
}
this.hash.update(data);
}
async digest() {
let buf;
if (this.isHmac) {
this.finished = true;
buf = this.hash.digest();
}
else {
buf = this.hash.copy().digest();
}
return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
}
reset() {
this.hash = this.createHash();
this.finished = false;
}
createHash() {
return this.secret ? createHmac("sha256", toBuffer(this.secret)) : createHash("sha256");
}
};
}
function convertToBuffer(data) {
function toBuffer(data) {
if (typeof data === "string") {
return fromUtf8(data);
return data;
}
if (ArrayBuffer.isView(data)) {
return new Uint8Array(data.buffer, data.byteOffset, data.byteLength / Uint8Array.BYTES_PER_ELEMENT);
return Buffer.from(data.buffer, data.byteOffset, data.byteLength);
}
return new Uint8Array(data);
return Buffer.from(data);
}
exports.Md5 = Md5;
const { digest, sign, importKey } = globalThis?.crypto?.subtle ?? {};
const subtle = typeof digest === "function" && typeof sign === "function" && typeof importKey === "function"
? globalThis.crypto.subtle
: undefined;
const MAX_PENDING_BYTES = 8 * 1024 * 1024;
class Sha256WebCrypto {
digestLength = 32;
secret;
pending = [];
pendingBytes = 0;
fallback;
finished = false;
constructor(secret) {
if (secret) {
this.secret = toUint8Array(secret);
}
}
update(data) {
if (this.finished) {
throw new Error("Attempted to update an already finished HMAC.");
}
if (this.fallback) {
this.fallback.update(data);
return;
}
this.pending.push(data.slice());
this.pendingBytes += data.byteLength;
if (this.pendingBytes >= MAX_PENDING_BYTES) {
this.switchToFallback();
}
}
async digest() {
if (this.fallback) {
return this.fallback.digest();
}
if (this.secret && this.finished) {
throw new Error("Attempted to digest an already finished HMAC.");
}
const data = concatBytes(this.pending);
if (subtle) {
if (this.secret) {
this.finished = true;
const key = await subtle.importKey("raw", this.secret, { name: "HMAC", hash: "SHA-256" }, false, ["sign"]);
const sig = await subtle.sign("HMAC", key, data);
return new Uint8Array(sig);
}
const hash = await subtle.digest("SHA-256", data);
return new Uint8Array(hash);
}
const sha256 = new Sha256Js(this.secret);
sha256.update(data);
return sha256.digest();
}
reset() {
this.pending = [];
this.pendingBytes = 0;
this.fallback = undefined;
this.finished = false;
}
switchToFallback() {
const sha256Js = new Sha256Js(this.secret);
for (const chunk of this.pending) {
sha256Js.update(chunk);
}
this.fallback = sha256Js;
this.pending = [];
this.pendingBytes = 0;
}
}
exports.Crc32 = Crc32Node;
exports.Crc32Js = Crc32Js;
exports.Crc32Node = Crc32Node;
exports.Md5 = Md5Node;
exports.Md5Js = Md5Js;
exports.Md5Node = Md5Node;
exports.Sha256 = Sha256Node;
exports.Sha256Js = Sha256Js;
exports.Sha256Node = Sha256Node;
exports.Sha256WebCrypto = Sha256WebCrypto;
exports.blobHasher = blobHasher;

@@ -257,0 +571,0 @@ exports.blobReader = blobReader;

@@ -1,2 +0,2 @@

const { fromUtf8, fromBase64 } = require("@smithy/core/serde");
const { toUint8Array, concatBytes, fromBase64 } = require("@smithy/core/serde");

@@ -21,30 +21,169 @@ async function blobReader$1(blob, onChunk, chunkSize = 1024 * 1024) {

const BLOCK_SIZE = 64;
const DIGEST_LENGTH = 16;
const INIT = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476];
class Md5Js {
digestLength = 16;
state = Uint32Array.from(INIT$1);
writeBuffer = new DataView(new ArrayBuffer(64));
bufferLength = 0;
bytesHashed = 0;
update(sourceData) {
const data = toUint8Array(sourceData);
let pos = 0;
let len = data.byteLength;
this.bytesHashed += len;
while (len > 0) {
this.writeBuffer.setUint8(this.bufferLength++, data[pos++]);
--len;
if (this.bufferLength === 64) {
compress(this.state, this.writeBuffer);
this.bufferLength = 0;
}
}
}
async digest() {
const state = Uint32Array.from(this.state);
const buf = new DataView(this.writeBuffer.buffer.slice(0));
let bufLen = this.bufferLength;
const bits = this.bytesHashed * 8;
buf.setUint8(bufLen++, 0x80);
if (this.bufferLength % 64 >= 56) {
for (let i = bufLen; i < 64; ++i) {
buf.setUint8(i, 0);
}
compress(state, buf);
bufLen = 0;
}
for (let i = bufLen; i < 56; ++i) {
buf.setUint8(i, 0);
}
buf.setUint32(56, bits >>> 0, true);
buf.setUint32(60, Math.floor(bits / 2 ** 32), true);
compress(state, buf);
const out = new Uint8Array(16);
const view = new DataView(out.buffer);
for (let i = 0; i < 4; ++i) {
view.setUint32(i * 4, state[i], true);
}
return out;
}
reset() {
this.state.set(INIT$1);
this.writeBuffer = new DataView(new ArrayBuffer(64));
this.bufferLength = 0;
this.bytesHashed = 0;
}
}
const INIT$1 = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476];
const M = 0xffffffff;
const S = Uint8Array.of(7, 12, 17, 22, 5, 9, 14, 20, 4, 11, 16, 23, 6, 10, 15, 21);
const T = Array.from({ length: 64 }, (_, i) => (Math.abs(Math.sin(i + 1)) * 2 ** 32) >>> 0);
function compress(state, block) {
let a = state[0], b = state[1], c = state[2], d = state[3];
for (let i = 0; i < 64; ++i) {
let f, g;
if (i < 16) {
f = (b & c) | (~b & d);
g = i;
}
else if (i < 32) {
f = (d & b) | (c & ~d);
g = (5 * i + 1) % 16;
}
else if (i < 48) {
f = b ^ c ^ d;
g = (3 * i + 5) % 16;
}
else {
f = c ^ (b | ~d);
g = (7 * i) % 16;
}
const x = block.getUint32(g * 4, true);
const tmp = d;
d = c;
c = b;
const s = S[(i >> 4) * 4 + (i & 3)];
const sum = (((a + f) & M) + ((x + T[i]) & M)) & M;
b = (b + (((sum << s) | (sum >>> (32 - s))) >>> 0)) & M;
a = tmp;
}
state[0] = (state[0] + a) & M;
state[1] = (state[1] + b) & M;
state[2] = (state[2] + c) & M;
state[3] = (state[3] + d) & M;
}
class Md5 {
state;
buffer;
bufferLength;
bytesHashed;
finished;
constructor() {
this.reset();
const CRC32_TABLE = new Uint32Array(256);
for (let i = 0; i < 256; ++i) {
let c = i;
for (let j = 0; j < 8; ++j) {
c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
}
update(sourceData) {
if (isEmptyData(sourceData)) {
CRC32_TABLE[i] = c >>> 0;
}
const ONES = 0xffff_ffff;
class Crc32Js {
digestLength = 4;
checksum = ONES;
update(data) {
for (let i = 0; i < data.length; ++i) {
this.checksum = (this.checksum >>> 8) ^ CRC32_TABLE[(this.checksum ^ data[i]) & 0xff];
}
}
async digest() {
const value = (this.checksum ^ ONES) >>> 0;
const out = new Uint8Array(4);
new DataView(out.buffer).setUint32(0, value, false);
return out;
}
reset() {
this.checksum = ONES;
}
}
const BLOCK = 64;
const DIGEST_LENGTH = 32;
const MAX_HASHABLE_LENGTH = 2 ** 53 - 1;
class Sha256Js {
digestLength = DIGEST_LENGTH;
state = Int32Array.from(INIT);
w;
buffer = new Uint8Array(64);
bufferLength = 0;
bytesHashed = 0;
finished = false;
inner;
outer;
constructor(secret) {
if (secret) {
const key = Sha256Js.normalizeKey(secret);
this.inner = new Sha256Js();
this.outer = new Sha256Js();
const { inner, outer } = this;
const pad = new Uint8Array(BLOCK * 2);
for (let i = 0; i < BLOCK; ++i) {
pad[i] = 0x36 ^ key[i];
pad[i + BLOCK] = 0x5c ^ key[i];
}
inner.update(pad.subarray(0, BLOCK));
outer.update(pad.subarray(BLOCK));
}
}
update(data) {
if (this.finished) {
throw new Error("Attempted to update an already finished HMAC.");
}
if (this.inner) {
this.inner.update(data);
return;
}
else if (this.finished) {
throw new Error("Attempted to update an already finished hash.");
}
const data = convertToBuffer(sourceData);
const chunk = toUint8Array(data);
let position = 0;
let { byteLength } = data;
let { byteLength } = chunk;
this.bytesHashed += byteLength;
if (this.bytesHashed * 8 > MAX_HASHABLE_LENGTH) {
throw new Error("Cannot hash more than 2^53 - 1 bits");
}
while (byteLength > 0) {
this.buffer.setUint8(this.bufferLength++, data[position++]);
this.buffer[this.bufferLength++] = chunk[position++];
byteLength--;
if (this.bufferLength === BLOCK_SIZE) {
if (this.bufferLength === BLOCK) {
this.hashBuffer();

@@ -56,137 +195,196 @@ this.bufferLength = 0;

async digest() {
if (!this.finished) {
const { buffer, bufferLength: undecoratedLength, bytesHashed } = this;
const bitsHashed = bytesHashed * 8;
buffer.setUint8(this.bufferLength++, 0b10000000);
if (undecoratedLength % BLOCK_SIZE >= BLOCK_SIZE - 8) {
for (let i = this.bufferLength; i < BLOCK_SIZE; i++) {
buffer.setUint8(i, 0);
}
this.hashBuffer();
this.bufferLength = 0;
const { inner, outer } = this;
if (inner && outer) {
if (this.finished) {
throw new Error("Attempted to digest an already finished HMAC.");
}
for (let i = this.bufferLength; i < BLOCK_SIZE - 8; i++) {
buffer.setUint8(i, 0);
}
buffer.setUint32(BLOCK_SIZE - 8, bitsHashed >>> 0, true);
buffer.setUint32(BLOCK_SIZE - 4, Math.floor(bitsHashed / 0x100000000), true);
this.hashBuffer();
this.finished = true;
const innerDigest = inner.digestSync();
outer.update(innerDigest);
return outer.digestSync();
}
const out = new DataView(new ArrayBuffer(DIGEST_LENGTH));
for (let i = 0; i < 4; i++) {
out.setUint32(i * 4, this.state[i], true);
}
return new Uint8Array(out.buffer, out.byteOffset, out.byteLength);
return this.digestSync();
}
hashBuffer() {
const { buffer, state } = this;
let a = state[0], b = state[1], c = state[2], d = state[3];
a = ff(a, b, c, d, buffer.getUint32(0, true), 7, 0xd76aa478);
d = ff(d, a, b, c, buffer.getUint32(4, true), 12, 0xe8c7b756);
c = ff(c, d, a, b, buffer.getUint32(8, true), 17, 0x242070db);
b = ff(b, c, d, a, buffer.getUint32(12, true), 22, 0xc1bdceee);
a = ff(a, b, c, d, buffer.getUint32(16, true), 7, 0xf57c0faf);
d = ff(d, a, b, c, buffer.getUint32(20, true), 12, 0x4787c62a);
c = ff(c, d, a, b, buffer.getUint32(24, true), 17, 0xa8304613);
b = ff(b, c, d, a, buffer.getUint32(28, true), 22, 0xfd469501);
a = ff(a, b, c, d, buffer.getUint32(32, true), 7, 0x698098d8);
d = ff(d, a, b, c, buffer.getUint32(36, true), 12, 0x8b44f7af);
c = ff(c, d, a, b, buffer.getUint32(40, true), 17, 0xffff5bb1);
b = ff(b, c, d, a, buffer.getUint32(44, true), 22, 0x895cd7be);
a = ff(a, b, c, d, buffer.getUint32(48, true), 7, 0x6b901122);
d = ff(d, a, b, c, buffer.getUint32(52, true), 12, 0xfd987193);
c = ff(c, d, a, b, buffer.getUint32(56, true), 17, 0xa679438e);
b = ff(b, c, d, a, buffer.getUint32(60, true), 22, 0x49b40821);
a = gg(a, b, c, d, buffer.getUint32(4, true), 5, 0xf61e2562);
d = gg(d, a, b, c, buffer.getUint32(24, true), 9, 0xc040b340);
c = gg(c, d, a, b, buffer.getUint32(44, true), 14, 0x265e5a51);
b = gg(b, c, d, a, buffer.getUint32(0, true), 20, 0xe9b6c7aa);
a = gg(a, b, c, d, buffer.getUint32(20, true), 5, 0xd62f105d);
d = gg(d, a, b, c, buffer.getUint32(40, true), 9, 0x02441453);
c = gg(c, d, a, b, buffer.getUint32(60, true), 14, 0xd8a1e681);
b = gg(b, c, d, a, buffer.getUint32(16, true), 20, 0xe7d3fbc8);
a = gg(a, b, c, d, buffer.getUint32(36, true), 5, 0x21e1cde6);
d = gg(d, a, b, c, buffer.getUint32(56, true), 9, 0xc33707d6);
c = gg(c, d, a, b, buffer.getUint32(12, true), 14, 0xf4d50d87);
b = gg(b, c, d, a, buffer.getUint32(32, true), 20, 0x455a14ed);
a = gg(a, b, c, d, buffer.getUint32(52, true), 5, 0xa9e3e905);
d = gg(d, a, b, c, buffer.getUint32(8, true), 9, 0xfcefa3f8);
c = gg(c, d, a, b, buffer.getUint32(28, true), 14, 0x676f02d9);
b = gg(b, c, d, a, buffer.getUint32(48, true), 20, 0x8d2a4c8a);
a = hh(a, b, c, d, buffer.getUint32(20, true), 4, 0xfffa3942);
d = hh(d, a, b, c, buffer.getUint32(32, true), 11, 0x8771f681);
c = hh(c, d, a, b, buffer.getUint32(44, true), 16, 0x6d9d6122);
b = hh(b, c, d, a, buffer.getUint32(56, true), 23, 0xfde5380c);
a = hh(a, b, c, d, buffer.getUint32(4, true), 4, 0xa4beea44);
d = hh(d, a, b, c, buffer.getUint32(16, true), 11, 0x4bdecfa9);
c = hh(c, d, a, b, buffer.getUint32(28, true), 16, 0xf6bb4b60);
b = hh(b, c, d, a, buffer.getUint32(40, true), 23, 0xbebfbc70);
a = hh(a, b, c, d, buffer.getUint32(52, true), 4, 0x289b7ec6);
d = hh(d, a, b, c, buffer.getUint32(0, true), 11, 0xeaa127fa);
c = hh(c, d, a, b, buffer.getUint32(12, true), 16, 0xd4ef3085);
b = hh(b, c, d, a, buffer.getUint32(24, true), 23, 0x04881d05);
a = hh(a, b, c, d, buffer.getUint32(36, true), 4, 0xd9d4d039);
d = hh(d, a, b, c, buffer.getUint32(48, true), 11, 0xe6db99e5);
c = hh(c, d, a, b, buffer.getUint32(60, true), 16, 0x1fa27cf8);
b = hh(b, c, d, a, buffer.getUint32(8, true), 23, 0xc4ac5665);
a = ii(a, b, c, d, buffer.getUint32(0, true), 6, 0xf4292244);
d = ii(d, a, b, c, buffer.getUint32(28, true), 10, 0x432aff97);
c = ii(c, d, a, b, buffer.getUint32(56, true), 15, 0xab9423a7);
b = ii(b, c, d, a, buffer.getUint32(20, true), 21, 0xfc93a039);
a = ii(a, b, c, d, buffer.getUint32(48, true), 6, 0x655b59c3);
d = ii(d, a, b, c, buffer.getUint32(12, true), 10, 0x8f0ccc92);
c = ii(c, d, a, b, buffer.getUint32(40, true), 15, 0xffeff47d);
b = ii(b, c, d, a, buffer.getUint32(4, true), 21, 0x85845dd1);
a = ii(a, b, c, d, buffer.getUint32(32, true), 6, 0x6fa87e4f);
d = ii(d, a, b, c, buffer.getUint32(60, true), 10, 0xfe2ce6e0);
c = ii(c, d, a, b, buffer.getUint32(24, true), 15, 0xa3014314);
b = ii(b, c, d, a, buffer.getUint32(52, true), 21, 0x4e0811a1);
a = ii(a, b, c, d, buffer.getUint32(16, true), 6, 0xf7537e82);
d = ii(d, a, b, c, buffer.getUint32(44, true), 10, 0xbd3af235);
c = ii(c, d, a, b, buffer.getUint32(8, true), 15, 0x2ad7d2bb);
b = ii(b, c, d, a, buffer.getUint32(36, true), 21, 0xeb86d391);
state[0] = (a + state[0]) & 0xffffffff;
state[1] = (b + state[1]) & 0xffffffff;
state[2] = (c + state[2]) & 0xffffffff;
state[3] = (d + state[3]) & 0xffffffff;
}
reset() {
this.state = Uint32Array.from(INIT);
this.buffer = new DataView(new ArrayBuffer(BLOCK_SIZE));
this.state = Int32Array.from(INIT);
this.buffer = new Uint8Array(64);
this.bufferLength = 0;
this.bytesHashed = 0;
this.finished = false;
}
}
function cmn(q, a, b, x, s, t) {
a = (((a + q) & 0xffffffff) + ((x + t) & 0xffffffff)) & 0xffffffff;
return (((a << s) | (a >>> (32 - s))) + b) & 0xffffffff;
}
function ff(a, b, c, d, x, s, t) {
return cmn((b & c) | (~b & d), a, b, x, s, t);
}
function gg(a, b, c, d, x, s, t) {
return cmn((b & d) | (c & ~d), a, b, x, s, t);
}
function hh(a, b, c, d, x, s, t) {
return cmn(b ^ c ^ d, a, b, x, s, t);
}
function ii(a, b, c, d, x, s, t) {
return cmn(c ^ (b | ~d), a, b, x, s, t);
}
function isEmptyData(data) {
if (typeof data === "string") {
return data.length === 0;
digestSync() {
const state = this.state.slice();
const buffer = this.buffer.slice();
let bufferLength = this.bufferLength;
const bitsHashed = this.bytesHashed * 8;
const bufferView = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
bufferView.setUint8(bufferLength++, 0x80);
if ((bufferLength - 1) % BLOCK >= BLOCK - 8) {
for (let i = bufferLength; i < BLOCK; ++i) {
bufferView.setUint8(i, 0);
}
this.hashBufferWith(state, buffer);
bufferLength = 0;
}
for (let i = bufferLength; i < BLOCK - 8; ++i) {
bufferView.setUint8(i, 0);
}
bufferView.setUint32(BLOCK - 8, Math.floor(bitsHashed / 0x100000000), false);
bufferView.setUint32(BLOCK - 4, bitsHashed, false);
this.hashBufferWith(state, buffer);
const out = new Uint8Array(DIGEST_LENGTH);
for (let i = 0; i < 8; ++i) {
out[i * 4] = (state[i] >>> 24) & 0xff;
out[i * 4 + 1] = (state[i] >>> 16) & 0xff;
out[i * 4 + 2] = (state[i] >>> 8) & 0xff;
out[i * 4 + 3] = (state[i] >>> 0) & 0xff;
}
return out;
}
return data.byteLength === 0;
static normalizeKey(secret) {
const key = toUint8Array(secret);
if (key.byteLength > BLOCK) {
const h = new Sha256Js();
h.update(key);
const out = h.digestSync();
const padded = new Uint8Array(BLOCK);
padded.set(out);
return padded;
}
if (key.byteLength < BLOCK) {
const padded = new Uint8Array(BLOCK);
padded.set(key);
return padded;
}
return key;
}
hashBuffer() {
this.hashBufferWith(this.state, this.buffer);
}
hashBufferWith(state, buffer) {
const w = (this.w ??= new Int32Array(64));
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];
for (let i = 0; i < BLOCK; ++i) {
if (i < 16) {
w[i] =
((buffer[i * 4] & 0xff) << 24) |
((buffer[i * 4 + 1] & 0xff) << 16) |
((buffer[i * 4 + 2] & 0xff) << 8) |
(buffer[i * 4 + 3] & 0xff);
}
else {
let u = w[i - 2];
const t1 = ((u >>> 17) | (u << 15)) ^ ((u >>> 19) | (u << 13)) ^ (u >>> 10);
u = w[i - 15];
const t2 = ((u >>> 7) | (u << 25)) ^ ((u >>> 18) | (u << 14)) ^ (u >>> 3);
w[i] = ((t1 + w[i - 7]) | 0) + ((t2 + w[i - 16]) | 0);
}
const t1 = ((((((s4 >>> 6) | (s4 << 26)) ^ ((s4 >>> 11) | (s4 << 21)) ^ ((s4 >>> 25) | (s4 << 7))) +
((s4 & s5) ^ (~s4 & s6))) |
0) +
((s7 + ((K[i] + w[i]) | 0)) | 0)) |
0;
const t2 = ((((s0 >>> 2) | (s0 << 30)) ^ ((s0 >>> 13) | (s0 << 19)) ^ ((s0 >>> 22) | (s0 << 10))) +
((s0 & s1) ^ (s0 & s2) ^ (s1 & s2))) |
0;
s7 = s6;
s6 = s5;
s5 = s4;
s4 = (s3 + t1) | 0;
s3 = s2;
s2 = s1;
s1 = s0;
s0 = (t1 + t2) | 0;
}
state[0] += s0;
state[1] += s1;
state[2] += s2;
state[3] += s3;
state[4] += s4;
state[5] += s5;
state[6] += s6;
state[7] += s7;
}
}
function convertToBuffer(data) {
if (typeof data === "string") {
return fromUtf8(data);
const INIT = new Int32Array([
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
]);
const K = new Int32Array([
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
]);
const { digest, sign, importKey } = globalThis?.crypto?.subtle ?? {};
const subtle = typeof digest === "function" && typeof sign === "function" && typeof importKey === "function"
? globalThis.crypto.subtle
: undefined;
const MAX_PENDING_BYTES = 8 * 1024 * 1024;
class Sha256WebCrypto {
digestLength = 32;
secret;
pending = [];
pendingBytes = 0;
fallback;
finished = false;
constructor(secret) {
if (secret) {
this.secret = toUint8Array(secret);
}
}
if (ArrayBuffer.isView(data)) {
return new Uint8Array(data.buffer, data.byteOffset, data.byteLength / Uint8Array.BYTES_PER_ELEMENT);
update(data) {
if (this.finished) {
throw new Error("Attempted to update an already finished HMAC.");
}
if (this.fallback) {
this.fallback.update(data);
return;
}
this.pending.push(data.slice());
this.pendingBytes += data.byteLength;
if (this.pendingBytes >= MAX_PENDING_BYTES) {
this.switchToFallback();
}
}
return new Uint8Array(data);
async digest() {
if (this.fallback) {
return this.fallback.digest();
}
if (this.secret && this.finished) {
throw new Error("Attempted to digest an already finished HMAC.");
}
const data = concatBytes(this.pending);
if (subtle) {
if (this.secret) {
this.finished = true;
const key = await subtle.importKey("raw", this.secret, { name: "HMAC", hash: "SHA-256" }, false, ["sign"]);
const sig = await subtle.sign("HMAC", key, data);
return new Uint8Array(sig);
}
const hash = await subtle.digest("SHA-256", data);
return new Uint8Array(hash);
}
const sha256 = new Sha256Js(this.secret);
sha256.update(data);
return sha256.digest();
}
reset() {
this.pending = [];
this.pendingBytes = 0;
this.fallback = undefined;
this.finished = false;
}
switchToFallback() {
const sha256Js = new Sha256Js(this.secret);
for (const chunk of this.pending) {
sha256Js.update(chunk);
}
this.fallback = sha256Js;
this.pending = [];
this.pendingBytes = 0;
}
}

@@ -224,4 +422,16 @@

const readableStreamHasher = no;
const Md5Node = no;
const Crc32Node = no;
const Sha256Node = no;
exports.Md5 = Md5;
exports.Crc32 = Crc32Js;
exports.Crc32Js = Crc32Js;
exports.Crc32Node = Crc32Node;
exports.Md5 = Md5Js;
exports.Md5Js = Md5Js;
exports.Md5Node = Md5Node;
exports.Sha256 = Sha256WebCrypto;
exports.Sha256Js = Sha256Js;
exports.Sha256Node = Sha256Node;
exports.Sha256WebCrypto = Sha256WebCrypto;
exports.blobHasher = blobHasher;

@@ -228,0 +438,0 @@ exports.blobReader = blobReader;

const { Uint8ArrayBlobAdapter, sdkStreamMixin, splitEvery, splitHeader, fromBase64, _parseEpochTimestamp, _parseRfc7231DateTime, _parseRfc3339DateTimeWithOffset, LazyJsonString, NumericValue, toUtf8, fromUtf8, generateIdempotencyToken, toBase64, dateToUtcString, quoteHeader } = require("@smithy/core/serde");
const { TypeRegistry, NormalizedSchema, translateTraits } = require("@smithy/core/schema");
const { HttpRequest, HttpResponse } = require("@smithy/core/transport");
const { isValidHostname, parseQueryString, parseUrl } = require("@smithy/core/transport");
const { HttpRequest, HttpResponse, isValidHostname } = require("@smithy/core/transport");
const { parseQueryString, parseUrl } = require("@smithy/core/transport");
exports.HttpRequest = HttpRequest;

@@ -119,2 +119,5 @@ exports.HttpResponse = HttpResponse;

request.hostname = hostPrefix + request.hostname;
if (!isValidHostname(request.hostname)) {
throw new Error(`[${request.hostname}] is not a valid hostname.`);
}
}

@@ -121,0 +124,0 @@ }

@@ -866,2 +866,5 @@ const { HttpResponse } = require("@smithy/core/transport");

const toUint8Array = (data) => {
if (data instanceof Uint8Array) {
return data;
}
if (typeof data === "string") {

@@ -876,2 +879,18 @@ return fromUtf8(data);

function concatBytes(arrays, length) {
if (length === undefined) {
length = 0;
for (const bytes of arrays) {
length += bytes.byteLength;
}
}
const result = new Uint8Array(length);
let offset = 0;
for (const buf of arrays) {
result.set(buf, offset);
offset += buf.byteLength;
}
return result;
}
const isArrayBuffer = (arg) => (typeof ArrayBuffer === "function" && arg instanceof ArrayBuffer) ||

@@ -1206,21 +1225,15 @@ Object.prototype.toString.call(arg) === "[object ArrayBuffer]";

const streamCollector = async (stream) => {
if ((typeof Blob === "function" && stream instanceof Blob) || stream.constructor?.name === "Blob") {
if (Blob.prototype.arrayBuffer !== undefined) {
return new Uint8Array(await stream.arrayBuffer());
}
if (isBlob(stream)) {
return collectBlob(stream);
}
return collectStream(stream);
return collectReadableStream(stream);
};
async function collectBlob(blob) {
const base64 = await readToBase64(blob);
const arrayBuffer = fromBase64(base64);
return new Uint8Array(arrayBuffer);
return blob.arrayBuffer().then((ab) => new Uint8Array(ab));
}
async function collectStream(stream) {
async function collectReadableStream(stream) {
const chunks = [];
const reader = stream.getReader();
let isDone = false;
let length = 0;
while (!isDone) {
while (true) {
const { done, value } = await reader.read();

@@ -1231,29 +1244,8 @@ if (value) {

}
isDone = done;
if (done) {
break;
}
}
const collected = new Uint8Array(length);
let offset = 0;
for (const chunk of chunks) {
collected.set(chunk, offset);
offset += chunk.length;
}
return collected;
return concatBytes(chunks, length);
}
function readToBase64(blob) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onloadend = () => {
if (reader.readyState !== 2) {
return reject(new Error("Reader aborted too early"));
}
const result = (reader.result ?? "");
const commaIndex = result.indexOf(",");
const dataOffset = commaIndex > -1 ? commaIndex + 1 : result.length;
resolve(result.substring(dataOffset));
};
reader.onabort = () => reject(new Error("Read aborted"));
reader.onerror = () => reject(reader.error);
reader.readAsDataURL(blob);
});
}

@@ -1347,2 +1339,3 @@ const ERR_MSG_STREAM_HAS_BEEN_TRANSFORMED = "The stream has already been transformed.";

exports.calculateBodyLength = calculateBodyLength;
exports.concatBytes = concatBytes;
exports.copyDocumentWithTransform = copyDocumentWithTransform;

@@ -1396,2 +1389,3 @@ exports.createBufferedReadable = createBufferedReadable;

exports.splitStream = splitStream;
exports.streamCollector = streamCollector;
exports.strictParseByte = strictParseByte;

@@ -1398,0 +1392,0 @@ exports.strictParseDouble = strictParseDouble;

@@ -24,3 +24,3 @@ const { createHmac, createHash, getRandomValues } = require("node:crypto");

const BASE64_REGEX = /^[A-Za-z0-9+/]*={0,2}$/;
const fromBase64$1 = (input) => {
const fromBase64 = (input) => {
if ((input.length * 3) % 4 !== 0) {

@@ -835,2 +835,5 @@ throw new TypeError(`Incorrect padding on base64 string.`);

const toUint8Array = (data) => {
if (data instanceof Uint8Array) {
return data;
}
if (typeof data === "string") {

@@ -845,2 +848,18 @@ return fromUtf8$1(data);

function concatBytes(arrays, length) {
if (length === undefined) {
length = 0;
for (const bytes of arrays) {
length += bytes.byteLength;
}
}
const result = new Uint8Array(length);
let offset = 0;
for (const buf of arrays) {
result.set(buf, offset);
offset += buf.byteLength;
}
return result;
}
const deserializerMiddleware = (options, deserializer) => (next, context) => async (args) => {

@@ -1031,2 +1050,6 @@ const { response } = await next(args);

}
_destroy(error, callback) {
this.source?.destroy();
callback(error);
}
};

@@ -1043,3 +1066,3 @@

const chars = `ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/`;
const alphabetByEncoding = Object.entries(chars).reduce((acc, [i, c]) => {
Object.entries(chars).reduce((acc, [i, c]) => {
acc[c] = Number(i);

@@ -1422,3 +1445,3 @@ return acc;

collector.on("finish", function () {
const bytes = new Uint8Array(Buffer.concat(this.buffers));
const bytes = concatBytes(this.buffers);
resolve(bytes);

@@ -1455,58 +1478,16 @@ });

const fromBase64 = (input) => {
let totalByteLength = (input.length / 4) * 3;
if (input.slice(-2) === "==") {
totalByteLength -= 2;
}
else if (input.slice(-1) === "=") {
totalByteLength--;
}
const out = new ArrayBuffer(totalByteLength);
const dataView = new DataView(out);
for (let i = 0; i < input.length; i += 4) {
let bits = 0;
let bitLength = 0;
for (let j = i, limit = i + 3; j <= limit; j++) {
if (input[j] !== "=") {
if (!(input[j] in alphabetByEncoding)) {
throw new TypeError(`Invalid character ${input[j]} in base64 string.`);
}
bits |= alphabetByEncoding[input[j]] << ((limit - j) * bitsPerLetter);
bitLength += bitsPerLetter;
}
else {
bits >>= bitsPerLetter;
}
}
const chunkOffset = (i / 4) * 3;
bits >>= bitLength % bitsPerByte;
const byteLength = Math.floor(bitLength / bitsPerByte);
for (let k = 0; k < byteLength; k++) {
const offset = (byteLength - k - 1) * bitsPerByte;
dataView.setUint8(chunkOffset + k, (bits & (255 << offset)) >> offset);
}
}
return new Uint8Array(out);
};
const streamCollector$1 = async (stream) => {
if ((typeof Blob === "function" && stream instanceof Blob) || stream.constructor?.name === "Blob") {
if (Blob.prototype.arrayBuffer !== undefined) {
return new Uint8Array(await stream.arrayBuffer());
}
if (isBlob(stream)) {
return collectBlob(stream);
}
return collectStream(stream);
return collectReadableStream(stream);
};
async function collectBlob(blob) {
const base64 = await readToBase64(blob);
const arrayBuffer = fromBase64(base64);
return new Uint8Array(arrayBuffer);
return blob.arrayBuffer().then((ab) => new Uint8Array(ab));
}
async function collectStream(stream) {
async function collectReadableStream(stream) {
const chunks = [];
const reader = stream.getReader();
let isDone = false;
let length = 0;
while (!isDone) {
while (true) {
const { done, value } = await reader.read();

@@ -1517,29 +1498,8 @@ if (value) {

}
isDone = done;
if (done) {
break;
}
}
const collected = new Uint8Array(length);
let offset = 0;
for (const chunk of chunks) {
collected.set(chunk, offset);
offset += chunk.length;
}
return collected;
return concatBytes(chunks, length);
}
function readToBase64(blob) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onloadend = () => {
if (reader.readyState !== 2) {
return reject(new Error("Reader aborted too early"));
}
const result = (reader.result ?? "");
const commaIndex = result.indexOf(",");
const dataOffset = commaIndex > -1 ? commaIndex + 1 : result.length;
resolve(result.substring(dataOffset));
};
reader.onabort = () => reject(new Error("Read aborted"));
reader.onerror = () => reject(reader.error);
reader.readAsDataURL(blob);
});
}

@@ -1606,33 +1566,7 @@ const ERR_MSG_STREAM_HAS_BEEN_TRANSFORMED$1 = "The stream has already been transformed.";

class Collector extends Writable {
bufferedBytes = [];
_write(chunk, encoding, callback) {
this.bufferedBytes.push(chunk);
callback();
const streamCollector = (stream) => {
if (isBlob(stream)) {
return collectBlob(stream);
}
}
const isReadableStreamInstance = (stream) => typeof ReadableStream === "function" && stream instanceof ReadableStream;
async function collectReadableStream(stream) {
const chunks = [];
const reader = stream.getReader();
let isDone = false;
let length = 0;
while (!isDone) {
const { done, value } = await reader.read();
if (value) {
chunks.push(value);
length += value.length;
}
isDone = done;
}
const collected = new Uint8Array(length);
let offset = 0;
for (const chunk of chunks) {
collected.set(chunk, offset);
offset += chunk.length;
}
return collected;
}
const streamCollector = (stream) => {
if (isReadableStreamInstance(stream)) {
if (isReadableStream(stream)) {
return collectReadableStream(stream);

@@ -1642,4 +1576,5 @@ }

const collector = new Collector();
stream.pipe(collector);
stream.on("error", (err) => {
const nodeStream = stream;
nodeStream.pipe(collector);
nodeStream.on("error", (err) => {
collector.end();

@@ -1650,3 +1585,3 @@ reject(err);

collector.on("finish", function () {
const bytes = new Uint8Array(Buffer.concat(this.bufferedBytes));
const bytes = concatBytes(this.bufferedBytes);
resolve(bytes);

@@ -1656,2 +1591,9 @@ });

};
class Collector extends Writable {
bufferedBytes = [];
_write(chunk, encoding, callback) {
this.bufferedBytes.push(chunk);
callback();
}
}

@@ -1724,3 +1666,3 @@ const ERR_MSG_STREAM_HAS_BEEN_TRANSFORMED = "The stream has already been transformed.";

class Uint8ArrayBlobAdapter extends bindUint8ArrayBlobAdapter(toUtf8$1, fromUtf8$1, toBase64$1, fromBase64$1) {
class Uint8ArrayBlobAdapter extends bindUint8ArrayBlobAdapter(toUtf8$1, fromUtf8$1, toBase64$1, fromBase64) {
}

@@ -1740,2 +1682,3 @@ const _getRandomValues = getRandomValues;

exports.calculateBodyLength = calculateBodyLength;
exports.concatBytes = concatBytes;
exports.copyDocumentWithTransform = copyDocumentWithTransform;

@@ -1760,3 +1703,3 @@ exports.createBufferedReadable = createBufferedReadable;

exports.fromArrayBuffer = fromArrayBuffer;
exports.fromBase64 = fromBase64$1;
exports.fromBase64 = fromBase64;
exports.fromHex = fromHex;

@@ -1790,2 +1733,3 @@ exports.fromString = fromString;

exports.splitStream = splitStream;
exports.streamCollector = streamCollector;
exports.strictParseByte = strictParseByte;

@@ -1792,0 +1736,0 @@ exports.strictParseDouble = strictParseDouble;

@@ -866,2 +866,5 @@ const { HttpResponse } = require("@smithy/core/transport");

const toUint8Array = (data) => {
if (data instanceof Uint8Array) {
return data;
}
if (typeof data === "string") {

@@ -876,2 +879,18 @@ return fromUtf8(data);

function concatBytes(arrays, length) {
if (length === undefined) {
length = 0;
for (const bytes of arrays) {
length += bytes.byteLength;
}
}
const result = new Uint8Array(length);
let offset = 0;
for (const buf of arrays) {
result.set(buf, offset);
offset += buf.byteLength;
}
return result;
}
const isArrayBuffer = (arg) => (typeof ArrayBuffer === "function" && arg instanceof ArrayBuffer) ||

@@ -1206,21 +1225,15 @@ Object.prototype.toString.call(arg) === "[object ArrayBuffer]";

const streamCollector = async (stream) => {
if ((typeof Blob === "function" && stream instanceof Blob) || stream.constructor?.name === "Blob") {
if (Blob.prototype.arrayBuffer !== undefined) {
return new Uint8Array(await stream.arrayBuffer());
}
if (isBlob(stream)) {
return collectBlob(stream);
}
return collectStream(stream);
return collectReadableStream(stream);
};
async function collectBlob(blob) {
const base64 = await readToBase64(blob);
const arrayBuffer = fromBase64(base64);
return new Uint8Array(arrayBuffer);
return blob.arrayBuffer().then((ab) => new Uint8Array(ab));
}
async function collectStream(stream) {
async function collectReadableStream(stream) {
const chunks = [];
const reader = stream.getReader();
let isDone = false;
let length = 0;
while (!isDone) {
while (true) {
const { done, value } = await reader.read();

@@ -1231,29 +1244,8 @@ if (value) {

}
isDone = done;
if (done) {
break;
}
}
const collected = new Uint8Array(length);
let offset = 0;
for (const chunk of chunks) {
collected.set(chunk, offset);
offset += chunk.length;
}
return collected;
return concatBytes(chunks, length);
}
function readToBase64(blob) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onloadend = () => {
if (reader.readyState !== 2) {
return reject(new Error("Reader aborted too early"));
}
const result = (reader.result ?? "");
const commaIndex = result.indexOf(",");
const dataOffset = commaIndex > -1 ? commaIndex + 1 : result.length;
resolve(result.substring(dataOffset));
};
reader.onabort = () => reject(new Error("Read aborted"));
reader.onerror = () => reject(reader.error);
reader.readAsDataURL(blob);
});
}

@@ -1347,2 +1339,3 @@ const ERR_MSG_STREAM_HAS_BEEN_TRANSFORMED = "The stream has already been transformed.";

exports.calculateBodyLength = calculateBodyLength;
exports.concatBytes = concatBytes;
exports.copyDocumentWithTransform = copyDocumentWithTransform;

@@ -1396,2 +1389,3 @@ exports.createBufferedReadable = createBufferedReadable;

exports.splitStream = splitStream;
exports.streamCollector = streamCollector;
exports.strictParseByte = strictParseByte;

@@ -1398,0 +1392,0 @@ exports.strictParseDouble = strictParseDouble;

@@ -5,3 +5,9 @@ const no = Symbol.for("node-only");

export const readableStreamHasher = no;
export { Md5 } from "./md5-js/md5";
export { Md5Js, Md5Js as Md5 } from "./md5/Md5Js";
export const Md5Node = no;
export { Crc32Js, Crc32Js as Crc32 } from "./crc32/Crc32Js";
export const Crc32Node = no;
export { Sha256Js } from "./sha256/Sha256Js";
export { Sha256WebCrypto, Sha256WebCrypto as Sha256 } from "./sha256/Sha256WebCrypto";
export const Sha256Node = no;
export { blobReader } from "./chunked-blob-reader/chunked-blob-reader";
export { blobHasher } from "./hash-blob-browser/blobHasher";
export { fileStreamHasher } from "./hash-stream-node/fileStreamHasher";
export { readableStreamHasher } from "./hash-stream-node/readableStreamHasher";
export { Md5 } from "./md5-js/md5";
export { Md5Js } from "./md5/Md5Js";
export { Md5Node, Md5Node as Md5 } from "./md5/Md5Node";
export { Crc32Js } from "./crc32/Crc32Js";
export { Crc32Node, Crc32Node as Crc32 } from "./crc32/Crc32Node";
export { Sha256Js } from "./sha256/Sha256Js";
export { Sha256Node, Sha256Node as Sha256 } from "./sha256/Sha256Node";
export { Sha256WebCrypto } from "./sha256/Sha256WebCrypto";
export { blobReader } from "./chunked-blob-reader/chunked-blob-reader";

@@ -5,3 +5,9 @@ const no = Symbol.for("node-only");

export const readableStreamHasher = no;
export { Md5 } from "./md5-js/md5";
export { Md5Js, Md5Js as Md5 } from "./md5/Md5Js";
export const Md5Node = no;
export { Crc32Js, Crc32Js as Crc32 } from "./crc32/Crc32Js";
export const Crc32Node = no;
export { Sha256Js } from "./sha256/Sha256Js";
export { Sha256WebCrypto, Sha256WebCrypto as Sha256 } from "./sha256/Sha256WebCrypto";
export const Sha256Node = no;
export { blobReader } from "./chunked-blob-reader/chunked-blob-reader.native";
import { NormalizedSchema, TypeRegistry, translateTraits } from "@smithy/core/schema";
import { HttpRequest, HttpResponse } from "@smithy/core/transport";
import { HttpRequest, HttpResponse, isValidHostname } from "@smithy/core/transport";
import { SerdeContext } from "./SerdeContext";

@@ -87,2 +87,5 @@ export class HttpProtocol extends SerdeContext {

request.hostname = hostPrefix + request.hostname;
if (!isValidHostname(request.hostname)) {
throw new Error(`[${request.hostname}] is not a valid hostname.`);
}
}

@@ -89,0 +92,0 @@ }

@@ -22,2 +22,3 @@ import { fromBase64 } from "./util-base64/fromBase64.browser";

export { toUtf8, fromUtf8 };
export { concatBytes } from "./concatBytes";
export const fromArrayBuffer = no;

@@ -40,4 +41,5 @@ export const fromString = no;

export { isReadableStream, isBlob } from "./util-stream/stream-type-check";
export { streamCollector } from "./util-stream/stream-collector.browser";
const _getRandomValues = (array) => crypto.getRandomValues(array);
export const v4 = bindV4(_getRandomValues);
export const generateIdempotencyToken = v4;

@@ -22,2 +22,3 @@ import { getRandomValues } from "node:crypto";

export { toUtf8, fromUtf8 };
export { concatBytes } from "./concatBytes";
export { fromArrayBuffer, fromString } from "./util-buffer-from/buffer-from";

@@ -39,4 +40,5 @@ export { isArrayBuffer } from "./is-array-buffer/is-array-buffer";

export { isReadableStream, isBlob } from "./util-stream/stream-type-check";
export { streamCollector } from "./util-stream/stream-collector";
const _getRandomValues = getRandomValues;
export const v4 = bindV4(_getRandomValues);
export const generateIdempotencyToken = v4;

@@ -24,2 +24,3 @@ import { fromBase64 } from "./util-base64/fromBase64.browser";

export { toUtf8 } from "./util-utf8/toUtf8.browser";
export { concatBytes } from "./concatBytes";
export const fromArrayBuffer = no;

@@ -42,4 +43,5 @@ export const fromString = no;

export { isReadableStream, isBlob } from "./util-stream/stream-type-check";
export { streamCollector } from "./util-stream/stream-collector.browser";
const _getRandomValues = (array) => crypto.getRandomValues(array);
export const v4 = bindV4(_getRandomValues);
export const generateIdempotencyToken = v4;

@@ -60,2 +60,6 @@ import { Duplex } from "node:stream";

}
_destroy(error, callback) {
this.source?.destroy();
callback(error);
}
}
import { Writable } from "node:stream";
import { concatBytes } from "../concatBytes";
import { headStream as headWebStream } from "./headStream.browser";

@@ -18,3 +19,3 @@ import { isReadableStream } from "./stream-type-check";

collector.on("finish", function () {
const bytes = new Uint8Array(Buffer.concat(this.buffers));
const bytes = concatBytes(this.buffers);
resolve(bytes);

@@ -21,0 +22,0 @@ });

@@ -1,22 +0,17 @@

import { fromBase64 } from "../util-base64/fromBase64.browser";
import { concatBytes } from "../concatBytes";
import { isBlob } from "./stream-type-check";
export const streamCollector = async (stream) => {
if ((typeof Blob === "function" && stream instanceof Blob) || stream.constructor?.name === "Blob") {
if (Blob.prototype.arrayBuffer !== undefined) {
return new Uint8Array(await stream.arrayBuffer());
}
if (isBlob(stream)) {
return collectBlob(stream);
}
return collectStream(stream);
return collectReadableStream(stream);
};
async function collectBlob(blob) {
const base64 = await readToBase64(blob);
const arrayBuffer = fromBase64(base64);
return new Uint8Array(arrayBuffer);
export async function collectBlob(blob) {
return blob.arrayBuffer().then((ab) => new Uint8Array(ab));
}
async function collectStream(stream) {
export async function collectReadableStream(stream) {
const chunks = [];
const reader = stream.getReader();
let isDone = false;
let length = 0;
while (!isDone) {
while (true) {
const { done, value } = await reader.read();

@@ -27,28 +22,7 @@ if (value) {

}
isDone = done;
if (done) {
break;
}
}
const collected = new Uint8Array(length);
let offset = 0;
for (const chunk of chunks) {
collected.set(chunk, offset);
offset += chunk.length;
}
return collected;
return concatBytes(chunks, length);
}
function readToBase64(blob) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onloadend = () => {
if (reader.readyState !== 2) {
return reject(new Error("Reader aborted too early"));
}
const result = (reader.result ?? "");
const commaIndex = result.indexOf(",");
const dataOffset = commaIndex > -1 ? commaIndex + 1 : result.length;
resolve(result.substring(dataOffset));
};
reader.onabort = () => reject(new Error("Read aborted"));
reader.onerror = () => reject(reader.error);
reader.readAsDataURL(blob);
});
}
import { Writable } from "node:stream";
class Collector extends Writable {
bufferedBytes = [];
_write(chunk, encoding, callback) {
this.bufferedBytes.push(chunk);
callback();
import { concatBytes } from "../concatBytes";
import { collectBlob, collectReadableStream } from "./stream-collector.browser";
import { isBlob, isReadableStream } from "./stream-type-check";
export const streamCollector = (stream) => {
if (isBlob(stream)) {
return collectBlob(stream);
}
}
const isReadableStreamInstance = (stream) => typeof ReadableStream === "function" && stream instanceof ReadableStream;
async function collectReadableStream(stream) {
const chunks = [];
const reader = stream.getReader();
let isDone = false;
let length = 0;
while (!isDone) {
const { done, value } = await reader.read();
if (value) {
chunks.push(value);
length += value.length;
}
isDone = done;
}
const collected = new Uint8Array(length);
let offset = 0;
for (const chunk of chunks) {
collected.set(chunk, offset);
offset += chunk.length;
}
return collected;
}
export const streamCollector = (stream) => {
if (isReadableStreamInstance(stream)) {
if (isReadableStream(stream)) {
return collectReadableStream(stream);

@@ -37,4 +14,5 @@ }

const collector = new Collector();
stream.pipe(collector);
stream.on("error", (err) => {
const nodeStream = stream;
nodeStream.pipe(collector);
nodeStream.on("error", (err) => {
collector.end();

@@ -45,3 +23,3 @@ reject(err);

collector.on("finish", function () {
const bytes = new Uint8Array(Buffer.concat(this.bufferedBytes));
const bytes = concatBytes(this.bufferedBytes);
resolve(bytes);

@@ -51,1 +29,8 @@ });

};
class Collector extends Writable {
bufferedBytes = [];
_write(chunk, encoding, callback) {
this.bufferedBytes.push(chunk);
callback();
}
}
import { fromUtf8 } from "./fromUtf8.browser";
export const toUint8Array = (data) => {
if (data instanceof Uint8Array) {
return data;
}
if (typeof data === "string") {

@@ -4,0 +7,0 @@ return fromUtf8(data);

import { fromUtf8 } from "./fromUtf8";
export const toUint8Array = (data) => {
if (data instanceof Uint8Array) {
return data;
}
if (typeof data === "string") {

@@ -4,0 +7,0 @@ return fromUtf8(data);

export { blobHasher } from "./hash-blob-browser/blobHasher";
export declare const fileStreamHasher: symbol;
export declare const readableStreamHasher: symbol;
export { Md5 } from "./md5-js/md5";
export { Md5Js, Md5Js as Md5 } from "./md5/Md5Js";
export declare const Md5Node: symbol;
export { Crc32Js, Crc32Js as Crc32 } from "./crc32/Crc32Js";
export declare const Crc32Node: symbol;
export { Sha256Js } from "./sha256/Sha256Js";
export { Sha256WebCrypto, Sha256WebCrypto as Sha256 } from "./sha256/Sha256WebCrypto";
export declare const Sha256Node: symbol;
export { blobReader } from "./chunked-blob-reader/chunked-blob-reader";
export { blobHasher } from "./hash-blob-browser/blobHasher";
export { fileStreamHasher } from "./hash-stream-node/fileStreamHasher";
export { readableStreamHasher } from "./hash-stream-node/readableStreamHasher";
export { Md5 } from "./md5-js/md5";
export { Md5Js } from "./md5/Md5Js";
export { Md5Node, Md5Node as Md5 } from "./md5/Md5Node";
export { Crc32Js } from "./crc32/Crc32Js";
export { Crc32Node, Crc32Node as Crc32 } from "./crc32/Crc32Node";
export { Sha256Js } from "./sha256/Sha256Js";
export { Sha256Node, Sha256Node as Sha256 } from "./sha256/Sha256Node";
export { Sha256WebCrypto } from "./sha256/Sha256WebCrypto";
export { blobReader } from "./chunked-blob-reader/chunked-blob-reader";
export { blobHasher } from "./hash-blob-browser/blobHasher";
export declare const fileStreamHasher: symbol;
export declare const readableStreamHasher: symbol;
export { Md5 } from "./md5-js/md5";
export { Md5Js, Md5Js as Md5 } from "./md5/Md5Js";
export declare const Md5Node: symbol;
export { Crc32Js, Crc32Js as Crc32 } from "./crc32/Crc32Js";
export declare const Crc32Node: symbol;
export { Sha256Js } from "./sha256/Sha256Js";
export { Sha256WebCrypto, Sha256WebCrypto as Sha256 } from "./sha256/Sha256WebCrypto";
export declare const Sha256Node: symbol;
export { blobReader } from "./chunked-blob-reader/chunked-blob-reader.native";

@@ -19,2 +19,3 @@ import { fromBase64 } from "./util-base64/fromBase64.browser";

export { toUtf8, fromUtf8 };
export { concatBytes } from "./concatBytes";
export { type StringEncoding } from "./util-buffer-from/buffer-from";

@@ -39,3 +40,4 @@ export declare const fromArrayBuffer: symbol;

export { isReadableStream, isBlob } from "./util-stream/stream-type-check";
export { streamCollector } from "./util-stream/stream-collector.browser";
export declare const v4: () => string;
export declare const generateIdempotencyToken: () => string;

@@ -19,2 +19,3 @@ import { fromBase64 } from "./util-base64/fromBase64";

export { toUtf8, fromUtf8 };
export { concatBytes } from "./concatBytes";
export { fromArrayBuffer, fromString, type StringEncoding } from "./util-buffer-from/buffer-from";

@@ -37,3 +38,4 @@ export { isArrayBuffer } from "./is-array-buffer/is-array-buffer";

export { isReadableStream, isBlob } from "./util-stream/stream-type-check";
export { streamCollector } from "./util-stream/stream-collector";
export declare const v4: () => string;
export declare const generateIdempotencyToken: () => string;

@@ -17,2 +17,3 @@ export { copyDocumentWithTransform } from "./copyDocumentWithTransform";

export { toUtf8 } from "./util-utf8/toUtf8.browser";
export { concatBytes } from "./concatBytes";
export { type StringEncoding } from "./util-buffer-from/buffer-from";

@@ -37,3 +38,4 @@ export declare const fromArrayBuffer: symbol;

export { isReadableStream, isBlob } from "./util-stream/stream-type-check";
export { streamCollector } from "./util-stream/stream-collector.browser";
export declare const v4: () => string;
export declare const generateIdempotencyToken: () => string;

@@ -60,2 +60,11 @@ import { Duplex, type Readable } from "node:stream";

_final(callback: (err?: Error) => void): Promise<void>;
/**
* Destroy the upstream source for cleanup so it is not left dangling, then
* complete this stream's destruction. The error is intentionally not forwarded
* to the source as the source is typically internal and without an error listener
* The error still surfaces on this stream via the callback.
* Do not call this directly.
* @internal
*/
_destroy(error: Error | null, callback: (error?: Error | null | undefined) => void): void;
}
/**
* Inlined from @smithy/fetch-http-handler streamCollector.
*
* @internal
*/
export declare const streamCollector: (stream: Blob | ReadableStream) => Promise<Uint8Array>;
/**
* @internal
*/
export declare function collectBlob(blob: Blob): Promise<Uint8Array>;
/**
* @internal
*/
export declare function collectReadableStream(stream: ReadableStream): Promise<Uint8Array>;
import { type Readable } from "node:stream";
import type { ReadableStream as IReadableStream } from "node:stream/web";
export declare const streamCollector: (stream: Readable | IReadableStream) => Promise<Uint8Array>;
/**
* @internal
*/
export declare const streamCollector: (stream: Readable | IReadableStream | ReadableStream | Blob) => Promise<Uint8Array>;
{
"name": "@smithy/core",
"version": "3.26.0",
"version": "3.27.0",
"scripts": {
"benchmark:cbor": "node ./scripts/cbor-perf.mjs",
"benchmark:checksum": "node ./scripts/checksum-perf.mjs",
"build": "concurrently 'yarn:build:types' 'yarn:build:es:cjs'",

@@ -18,4 +20,3 @@ "build:es:cjs": "premove dist-es && yarn g:tsc -p tsconfig.es.json && node ../../scripts/inline",

"test:integration": "yarn g:vitest run -c vitest.config.integ.mts",
"test:integration:watch": "yarn g:vitest watch -c vitest.config.integ.mts",
"test:cbor:perf": "node ./scripts/cbor-perf.mjs"
"test:integration:watch": "yarn g:vitest watch -c vitest.config.integ.mts"
},

@@ -22,0 +23,0 @@ "main": "./dist-cjs/index.js",

export const BLOCK_SIZE = 64;
export const DIGEST_LENGTH = 16;
export const INIT = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476];
import { fromUtf8 } from "@smithy/core/serde";
import { BLOCK_SIZE, DIGEST_LENGTH, INIT } from "./constants";
export class Md5 {
state;
buffer;
bufferLength;
bytesHashed;
finished;
constructor() {
this.reset();
}
update(sourceData) {
if (isEmptyData(sourceData)) {
return;
}
else if (this.finished) {
throw new Error("Attempted to update an already finished hash.");
}
const data = convertToBuffer(sourceData);
let position = 0;
let { byteLength } = data;
this.bytesHashed += byteLength;
while (byteLength > 0) {
this.buffer.setUint8(this.bufferLength++, data[position++]);
byteLength--;
if (this.bufferLength === BLOCK_SIZE) {
this.hashBuffer();
this.bufferLength = 0;
}
}
}
async digest() {
if (!this.finished) {
const { buffer, bufferLength: undecoratedLength, bytesHashed } = this;
const bitsHashed = bytesHashed * 8;
buffer.setUint8(this.bufferLength++, 0b10000000);
if (undecoratedLength % BLOCK_SIZE >= BLOCK_SIZE - 8) {
for (let i = this.bufferLength; i < BLOCK_SIZE; i++) {
buffer.setUint8(i, 0);
}
this.hashBuffer();
this.bufferLength = 0;
}
for (let i = this.bufferLength; i < BLOCK_SIZE - 8; i++) {
buffer.setUint8(i, 0);
}
buffer.setUint32(BLOCK_SIZE - 8, bitsHashed >>> 0, true);
buffer.setUint32(BLOCK_SIZE - 4, Math.floor(bitsHashed / 0x100000000), true);
this.hashBuffer();
this.finished = true;
}
const out = new DataView(new ArrayBuffer(DIGEST_LENGTH));
for (let i = 0; i < 4; i++) {
out.setUint32(i * 4, this.state[i], true);
}
return new Uint8Array(out.buffer, out.byteOffset, out.byteLength);
}
hashBuffer() {
const { buffer, state } = this;
let a = state[0], b = state[1], c = state[2], d = state[3];
a = ff(a, b, c, d, buffer.getUint32(0, true), 7, 0xd76aa478);
d = ff(d, a, b, c, buffer.getUint32(4, true), 12, 0xe8c7b756);
c = ff(c, d, a, b, buffer.getUint32(8, true), 17, 0x242070db);
b = ff(b, c, d, a, buffer.getUint32(12, true), 22, 0xc1bdceee);
a = ff(a, b, c, d, buffer.getUint32(16, true), 7, 0xf57c0faf);
d = ff(d, a, b, c, buffer.getUint32(20, true), 12, 0x4787c62a);
c = ff(c, d, a, b, buffer.getUint32(24, true), 17, 0xa8304613);
b = ff(b, c, d, a, buffer.getUint32(28, true), 22, 0xfd469501);
a = ff(a, b, c, d, buffer.getUint32(32, true), 7, 0x698098d8);
d = ff(d, a, b, c, buffer.getUint32(36, true), 12, 0x8b44f7af);
c = ff(c, d, a, b, buffer.getUint32(40, true), 17, 0xffff5bb1);
b = ff(b, c, d, a, buffer.getUint32(44, true), 22, 0x895cd7be);
a = ff(a, b, c, d, buffer.getUint32(48, true), 7, 0x6b901122);
d = ff(d, a, b, c, buffer.getUint32(52, true), 12, 0xfd987193);
c = ff(c, d, a, b, buffer.getUint32(56, true), 17, 0xa679438e);
b = ff(b, c, d, a, buffer.getUint32(60, true), 22, 0x49b40821);
a = gg(a, b, c, d, buffer.getUint32(4, true), 5, 0xf61e2562);
d = gg(d, a, b, c, buffer.getUint32(24, true), 9, 0xc040b340);
c = gg(c, d, a, b, buffer.getUint32(44, true), 14, 0x265e5a51);
b = gg(b, c, d, a, buffer.getUint32(0, true), 20, 0xe9b6c7aa);
a = gg(a, b, c, d, buffer.getUint32(20, true), 5, 0xd62f105d);
d = gg(d, a, b, c, buffer.getUint32(40, true), 9, 0x02441453);
c = gg(c, d, a, b, buffer.getUint32(60, true), 14, 0xd8a1e681);
b = gg(b, c, d, a, buffer.getUint32(16, true), 20, 0xe7d3fbc8);
a = gg(a, b, c, d, buffer.getUint32(36, true), 5, 0x21e1cde6);
d = gg(d, a, b, c, buffer.getUint32(56, true), 9, 0xc33707d6);
c = gg(c, d, a, b, buffer.getUint32(12, true), 14, 0xf4d50d87);
b = gg(b, c, d, a, buffer.getUint32(32, true), 20, 0x455a14ed);
a = gg(a, b, c, d, buffer.getUint32(52, true), 5, 0xa9e3e905);
d = gg(d, a, b, c, buffer.getUint32(8, true), 9, 0xfcefa3f8);
c = gg(c, d, a, b, buffer.getUint32(28, true), 14, 0x676f02d9);
b = gg(b, c, d, a, buffer.getUint32(48, true), 20, 0x8d2a4c8a);
a = hh(a, b, c, d, buffer.getUint32(20, true), 4, 0xfffa3942);
d = hh(d, a, b, c, buffer.getUint32(32, true), 11, 0x8771f681);
c = hh(c, d, a, b, buffer.getUint32(44, true), 16, 0x6d9d6122);
b = hh(b, c, d, a, buffer.getUint32(56, true), 23, 0xfde5380c);
a = hh(a, b, c, d, buffer.getUint32(4, true), 4, 0xa4beea44);
d = hh(d, a, b, c, buffer.getUint32(16, true), 11, 0x4bdecfa9);
c = hh(c, d, a, b, buffer.getUint32(28, true), 16, 0xf6bb4b60);
b = hh(b, c, d, a, buffer.getUint32(40, true), 23, 0xbebfbc70);
a = hh(a, b, c, d, buffer.getUint32(52, true), 4, 0x289b7ec6);
d = hh(d, a, b, c, buffer.getUint32(0, true), 11, 0xeaa127fa);
c = hh(c, d, a, b, buffer.getUint32(12, true), 16, 0xd4ef3085);
b = hh(b, c, d, a, buffer.getUint32(24, true), 23, 0x04881d05);
a = hh(a, b, c, d, buffer.getUint32(36, true), 4, 0xd9d4d039);
d = hh(d, a, b, c, buffer.getUint32(48, true), 11, 0xe6db99e5);
c = hh(c, d, a, b, buffer.getUint32(60, true), 16, 0x1fa27cf8);
b = hh(b, c, d, a, buffer.getUint32(8, true), 23, 0xc4ac5665);
a = ii(a, b, c, d, buffer.getUint32(0, true), 6, 0xf4292244);
d = ii(d, a, b, c, buffer.getUint32(28, true), 10, 0x432aff97);
c = ii(c, d, a, b, buffer.getUint32(56, true), 15, 0xab9423a7);
b = ii(b, c, d, a, buffer.getUint32(20, true), 21, 0xfc93a039);
a = ii(a, b, c, d, buffer.getUint32(48, true), 6, 0x655b59c3);
d = ii(d, a, b, c, buffer.getUint32(12, true), 10, 0x8f0ccc92);
c = ii(c, d, a, b, buffer.getUint32(40, true), 15, 0xffeff47d);
b = ii(b, c, d, a, buffer.getUint32(4, true), 21, 0x85845dd1);
a = ii(a, b, c, d, buffer.getUint32(32, true), 6, 0x6fa87e4f);
d = ii(d, a, b, c, buffer.getUint32(60, true), 10, 0xfe2ce6e0);
c = ii(c, d, a, b, buffer.getUint32(24, true), 15, 0xa3014314);
b = ii(b, c, d, a, buffer.getUint32(52, true), 21, 0x4e0811a1);
a = ii(a, b, c, d, buffer.getUint32(16, true), 6, 0xf7537e82);
d = ii(d, a, b, c, buffer.getUint32(44, true), 10, 0xbd3af235);
c = ii(c, d, a, b, buffer.getUint32(8, true), 15, 0x2ad7d2bb);
b = ii(b, c, d, a, buffer.getUint32(36, true), 21, 0xeb86d391);
state[0] = (a + state[0]) & 0xffffffff;
state[1] = (b + state[1]) & 0xffffffff;
state[2] = (c + state[2]) & 0xffffffff;
state[3] = (d + state[3]) & 0xffffffff;
}
reset() {
this.state = Uint32Array.from(INIT);
this.buffer = new DataView(new ArrayBuffer(BLOCK_SIZE));
this.bufferLength = 0;
this.bytesHashed = 0;
this.finished = false;
}
}
function cmn(q, a, b, x, s, t) {
a = (((a + q) & 0xffffffff) + ((x + t) & 0xffffffff)) & 0xffffffff;
return (((a << s) | (a >>> (32 - s))) + b) & 0xffffffff;
}
function ff(a, b, c, d, x, s, t) {
return cmn((b & c) | (~b & d), a, b, x, s, t);
}
function gg(a, b, c, d, x, s, t) {
return cmn((b & d) | (c & ~d), a, b, x, s, t);
}
function hh(a, b, c, d, x, s, t) {
return cmn(b ^ c ^ d, a, b, x, s, t);
}
function ii(a, b, c, d, x, s, t) {
return cmn(c ^ (b | ~d), a, b, x, s, t);
}
function isEmptyData(data) {
if (typeof data === "string") {
return data.length === 0;
}
return data.byteLength === 0;
}
function convertToBuffer(data) {
if (typeof data === "string") {
return fromUtf8(data);
}
if (ArrayBuffer.isView(data)) {
return new Uint8Array(data.buffer, data.byteOffset, data.byteLength / Uint8Array.BYTES_PER_ELEMENT);
}
return new Uint8Array(data);
}
/**
* @internal
*/
export declare const BLOCK_SIZE = 64;
/**
* @internal
*/
export declare const DIGEST_LENGTH = 16;
/**
* @internal
*/
export declare const INIT: number[];
import type { Checksum, SourceData } from "@smithy/types";
/**
* @internal
*/
export declare class Md5 implements Checksum {
private state;
private buffer;
private bufferLength;
private bytesHashed;
private finished;
constructor();
update(sourceData: SourceData): void;
digest(): Promise<Uint8Array>;
private hashBuffer;
reset(): void;
}