Initial commit (by create-cloudflare CLI)

This commit is contained in:
Jonas_Jones 2023-09-12 00:20:38 +02:00
parent 8cb86120f1
commit fff961078a
1777 changed files with 1011798 additions and 0 deletions

View file

@ -0,0 +1,5 @@
export declare type BrowserEncoding = 'hex' | 'base64' | 'utf8';
/**
* @hidden
*/
export declare const mustGetEncoder: (encoding: BrowserEncoding) => (data: Uint8Array) => string;

View file

@ -0,0 +1,30 @@
"use strict";
// A small collection of encodings for convenience of use in the browser.
Object.defineProperty(exports, "__esModule", { value: true });
const decoder = new TextDecoder();
const encoders = {
// certainly not the fastest, but hashes are pretty small
base64: data => btoa(String.fromCharCode(...data)),
hex: data => {
let out = '';
for (const byte of data) {
if (byte < 0x10) {
out += '0';
}
out += byte.toString(16);
}
return out;
},
utf8: data => decoder.decode(data),
};
/**
* @hidden
*/
exports.mustGetEncoder = (encoding) => {
const encoder = encoders[encoding];
if (!encoder) {
throw new Error(`Unknown encoding ${encoding}`);
}
return encoder;
};
//# sourceMappingURL=encoding.js.map

View file

@ -0,0 +1 @@
{"version":3,"file":"encoding.js","sourceRoot":"","sources":["../../ts/browser/encoding.ts"],"names":[],"mappings":";AAAA,yEAAyE;;AAIzE,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC;AAElC,MAAM,QAAQ,GAA6D;IACzE,yDAAyD;IACzD,MAAM,EAAE,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC,GAAG,IAAI,CAAC,CAAC;IAElD,GAAG,EAAE,IAAI,CAAC,EAAE;QACV,IAAI,GAAG,GAAG,EAAE,CAAC;QACb,KAAK,MAAM,IAAI,IAAI,IAAI,EAAE;YACvB,IAAI,IAAI,GAAG,IAAI,EAAE;gBACf,GAAG,IAAI,GAAG,CAAC;aACZ;YAED,GAAG,IAAI,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;SAC1B;QAED,OAAO,GAAG,CAAC;IACb,CAAC;IAED,IAAI,EAAE,IAAI,CAAC,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC;CACnC,CAAC;AAEF;;GAEG;AACU,QAAA,cAAc,GAAG,CAAC,QAAyB,EAAE,EAAE;IAC1D,MAAM,OAAO,GAAG,QAAQ,CAAC,QAAQ,CAAC,CAAC;IACnC,IAAI,CAAC,OAAO,EAAE;QACZ,MAAM,IAAI,KAAK,CAAC,oBAAoB,QAAQ,EAAE,CAAC,CAAC;KACjD;IAED,OAAO,OAAO,CAAC;AACjB,CAAC,CAAC"}

View file

@ -0,0 +1,24 @@
import { BaseHashInput, IBaseHashOptions } from '../base/hash-fn';
import { Hash } from './hash';
/**
* Input used for browser-based hashes.
*/
export declare type HashInput = BaseHashInput | string;
/**
* @hidden
*/
export declare const normalizeInput: (input: import("..").HashInput) => Uint8Array;
/**
* Returns a blake3 hash of the input.
*/
export declare function hash(input: HashInput, { length }?: IBaseHashOptions): Hash;
/**
* Given cryptographic key material and a context string, services a subkey of
* any length. See {@link https://docs.rs/blake3/0.1.3/blake3/fn.derive_key.html}
* for more information.
*/
export declare function deriveKey(context: string, material: HashInput, { length }?: IBaseHashOptions): Hash;
/**
* The keyed hash function. See {@link https://docs.rs/blake3/0.1.3/blake3/fn.keyed_hash.html}.
*/
export declare function keyedHash(key: Uint8Array, input: HashInput, { length }?: IBaseHashOptions): Hash;

View file

@ -0,0 +1,47 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const hash_fn_1 = require("../base/hash-fn");
const hash_1 = require("./hash");
const wasm_1 = require("./wasm");
const textEncoder = new TextEncoder();
/**
* @hidden
*/
exports.normalizeInput = (input) => hash_fn_1.inputToArray(typeof input === 'string' ? textEncoder.encode(input) : input);
/**
* Returns a blake3 hash of the input.
*/
function hash(input, { length = hash_fn_1.defaultHashLength } = {}) {
const result = new hash_1.Hash(length);
wasm_1.getWasm().hash(exports.normalizeInput(input), result);
return result;
}
exports.hash = hash;
/**
* Given cryptographic key material and a context string, services a subkey of
* any length. See {@link https://docs.rs/blake3/0.1.3/blake3/fn.derive_key.html}
* for more information.
*/
function deriveKey(context, material, { length = hash_fn_1.defaultHashLength } = {}) {
const derive = wasm_1.getWasm().create_derive(context);
derive.update(exports.normalizeInput(material));
const result = new hash_1.Hash(length);
derive.digest(result);
return result;
}
exports.deriveKey = deriveKey;
/**
* The keyed hash function. See {@link https://docs.rs/blake3/0.1.3/blake3/fn.keyed_hash.html}.
*/
function keyedHash(key, input, { length = hash_fn_1.defaultHashLength } = {}) {
if (key.length !== 32) {
throw new Error(`key provided to keyedHash must be 32 bytes, got ${key.length}`);
}
const derive = wasm_1.getWasm().create_keyed(key);
derive.update(exports.normalizeInput(input));
const result = new hash_1.Hash(length);
derive.digest(result);
return result;
}
exports.keyedHash = keyedHash;
//# sourceMappingURL=hash-fn.js.map

View file

@ -0,0 +1 @@
{"version":3,"file":"hash-fn.js","sourceRoot":"","sources":["../../ts/browser/hash-fn.ts"],"names":[],"mappings":";;AAAA,6CAAmG;AACnG,iCAA8B;AAC9B,iCAAiC;AAOjC,MAAM,WAAW,GAAG,IAAI,WAAW,EAAE,CAAC;AAEtC;;GAEG;AACU,QAAA,cAAc,GAAG,CAAC,KAAgB,EAAc,EAAE,CAC7D,sBAAY,CAAC,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,WAAW,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;AAE9E;;GAEG;AACH,SAAgB,IAAI,CAClB,KAAgB,EAChB,EAAE,MAAM,GAAG,2BAAiB,KAAuB,EAAE;IAErD,MAAM,MAAM,GAAG,IAAI,WAAI,CAAC,MAAM,CAAC,CAAC;IAChC,cAAO,EAAE,CAAC,IAAI,CAAC,sBAAc,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC,CAAC;IAC9C,OAAO,MAAM,CAAC;AAChB,CAAC;AAPD,oBAOC;AAED;;;;GAIG;AACH,SAAgB,SAAS,CACvB,OAAe,EACf,QAAmB,EACnB,EAAE,MAAM,GAAG,2BAAiB,KAAuB,EAAE;IAErD,MAAM,MAAM,GAAG,cAAO,EAAE,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC;IAChD,MAAM,CAAC,MAAM,CAAC,sBAAc,CAAC,QAAQ,CAAC,CAAC,CAAC;IACxC,MAAM,MAAM,GAAG,IAAI,WAAI,CAAC,MAAM,CAAC,CAAC;IAChC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IACtB,OAAO,MAAM,CAAC;AAChB,CAAC;AAVD,8BAUC;AAED;;GAEG;AACH,SAAgB,SAAS,CACvB,GAAe,EACf,KAAgB,EAChB,EAAE,MAAM,GAAG,2BAAiB,KAAuB,EAAE;IAErD,IAAI,GAAG,CAAC,MAAM,KAAK,EAAE,EAAE;QACrB,MAAM,IAAI,KAAK,CAAC,mDAAmD,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC;KAClF;IAED,MAAM,MAAM,GAAG,cAAO,EAAE,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC;IAC3C,MAAM,CAAC,MAAM,CAAC,sBAAc,CAAC,KAAK,CAAC,CAAC,CAAC;IACrC,MAAM,MAAM,GAAG,IAAI,WAAI,CAAC,MAAM,CAAC,CAAC;IAChC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IACtB,OAAO,MAAM,CAAC;AAChB,CAAC;AAdD,8BAcC"}

View file

@ -0,0 +1,31 @@
import { BaseHash as BaseHasher } from '../base/index';
import { HashInput } from './hash-fn';
import { BrowserEncoding } from './encoding';
import { IBaseHashOptions } from '../base/hash-fn';
import { BrowserHashReader } from './hash-reader';
import { IInternalReader } from '../base/hash-reader';
import { Hash } from './hash';
/**
* @inheritdoc
*/
export declare class BrowserHasher extends BaseHasher<Hash, IInternalReader, BrowserHashReader> {
/**
* @inheritdoc
* @override
*/
update(data: HashInput): this;
/**
* Returns a digest of the hash with the given encoding.
*/
digest(options?: IBaseHashOptions): Hash;
digest(encoding: undefined, options: IBaseHashOptions): Hash;
digest(encoding: BrowserEncoding, options?: IBaseHashOptions): string;
}
/**
* A Node.js crypto-like createHash method.
*/
export declare const createHash: () => BrowserHasher;
/**
* A Node.js crypto-like createHash method.
*/
export declare const createKeyed: (key: Uint8Array) => BrowserHasher;

View file

@ -0,0 +1,44 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const index_1 = require("../base/index");
const hash_fn_1 = require("./hash-fn");
const encoding_1 = require("./encoding");
const hash_reader_1 = require("./hash-reader");
const hash_1 = require("./hash");
const wasm_1 = require("./wasm");
/**
* @inheritdoc
*/
class BrowserHasher extends index_1.BaseHash {
/**
* @inheritdoc
* @override
*/
update(data) {
return super.update(hash_fn_1.normalizeInput(data));
}
digest(encoding, options) {
let resolvedOpts;
let resolvedEnc;
if (encoding && typeof encoding === 'object') {
resolvedOpts = encoding;
resolvedEnc = undefined;
}
else {
resolvedOpts = options;
resolvedEnc = encoding;
}
const result = super.digest(resolvedOpts);
return resolvedEnc ? encoding_1.mustGetEncoder(resolvedEnc)(result) : result;
}
}
exports.BrowserHasher = BrowserHasher;
/**
* A Node.js crypto-like createHash method.
*/
exports.createHash = () => new BrowserHasher(wasm_1.getWasm().create_hasher(), l => new hash_1.Hash(l), r => new hash_reader_1.BrowserHashReader(r));
/**
* A Node.js crypto-like createHash method.
*/
exports.createKeyed = (key) => new BrowserHasher(wasm_1.getWasm().create_keyed(key), l => new hash_1.Hash(l), r => new hash_reader_1.BrowserHashReader(r));
//# sourceMappingURL=hash-instance.js.map

View file

@ -0,0 +1 @@
{"version":3,"file":"hash-instance.js","sourceRoot":"","sources":["../../ts/browser/hash-instance.ts"],"names":[],"mappings":";;AAAA,yCAAuD;AACvD,uCAAsD;AACtD,yCAA6D;AAE7D,+CAAkD;AAElD,iCAA8B;AAC9B,iCAAiC;AAEjC;;GAEG;AACH,MAAa,aAAc,SAAQ,gBAAoD;IACrF;;;OAGG;IACI,MAAM,CAAC,IAAe;QAC3B,OAAO,KAAK,CAAC,MAAM,CAAC,wBAAc,CAAC,IAAI,CAAC,CAAC,CAAC;IAC5C,CAAC;IAQM,MAAM,CACX,QAA6C,EAC7C,OAA0B;QAE1B,IAAI,YAA0C,CAAC;QAC/C,IAAI,WAAwC,CAAC;QAC7C,IAAI,QAAQ,IAAI,OAAO,QAAQ,KAAK,QAAQ,EAAE;YAC5C,YAAY,GAAG,QAAQ,CAAC;YACxB,WAAW,GAAG,SAAS,CAAC;SACzB;aAAM;YACL,YAAY,GAAG,OAAO,CAAC;YACvB,WAAW,GAAG,QAAQ,CAAC;SACxB;QAED,MAAM,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC;QAC1C,OAAO,WAAW,CAAC,CAAC,CAAC,yBAAc,CAAC,WAAW,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;IACpE,CAAC;CACF;AAhCD,sCAgCC;AAED;;GAEG;AACU,QAAA,UAAU,GAAG,GAAG,EAAE,CAC7B,IAAI,aAAa,CACf,cAAO,EAAE,CAAC,aAAa,EAAE,EACzB,CAAC,CAAC,EAAE,CAAC,IAAI,WAAI,CAAC,CAAC,CAAC,EAChB,CAAC,CAAC,EAAE,CAAC,IAAI,+BAAiB,CAAC,CAAC,CAAC,CAC9B,CAAC;AAEJ;;GAEG;AACU,QAAA,WAAW,GAAG,CAAC,GAAe,EAAE,EAAE,CAC7C,IAAI,aAAa,CACf,cAAO,EAAE,CAAC,YAAY,CAAC,GAAG,CAAC,EAC3B,CAAC,CAAC,EAAE,CAAC,IAAI,WAAI,CAAC,CAAC,CAAC,EAChB,CAAC,CAAC,EAAE,CAAC,IAAI,+BAAiB,CAAC,CAAC,CAAC,CAC9B,CAAC"}

View file

@ -0,0 +1,17 @@
import { BaseHashReader } from '../base/hash-reader';
import { BrowserEncoding } from './encoding';
import { Hash } from './hash';
/**
* A hash reader for WebAssembly targets.
*/
export declare class BrowserHashReader extends BaseHashReader<Hash> {
/**
* Converts first 32 bytes of the hash to a string with the given encoding.
*/
toString(encoding?: BrowserEncoding): string;
/**
* Converts first 32 bytes of the hash to an array.
*/
toArray(): Hash;
protected alloc(bytes: number): Hash;
}

View file

@ -0,0 +1,28 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const hash_reader_1 = require("../base/hash-reader");
const hash_1 = require("./hash");
const index_1 = require("../base/index");
/**
* A hash reader for WebAssembly targets.
*/
class BrowserHashReader extends hash_reader_1.BaseHashReader {
/**
* Converts first 32 bytes of the hash to a string with the given encoding.
*/
toString(encoding = 'hex') {
return this.toArray().toString(encoding);
}
/**
* Converts first 32 bytes of the hash to an array.
*/
toArray() {
this.position = BigInt(0);
return this.read(index_1.defaultHashLength);
}
alloc(bytes) {
return new hash_1.Hash(bytes);
}
}
exports.BrowserHashReader = BrowserHashReader;
//# sourceMappingURL=hash-reader.js.map

View file

@ -0,0 +1 @@
{"version":3,"file":"hash-reader.js","sourceRoot":"","sources":["../../ts/browser/hash-reader.ts"],"names":[],"mappings":";;AAAA,qDAAqD;AAErD,iCAA8B;AAC9B,yCAAkD;AAElD;;GAEG;AACH,MAAa,iBAAkB,SAAQ,4BAAoB;IACzD;;OAEG;IACI,QAAQ,CAAC,WAA4B,KAAK;QAC/C,OAAO,IAAI,CAAC,OAAO,EAAE,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;IAC3C,CAAC;IAED;;OAEG;IACI,OAAO;QACZ,IAAI,CAAC,QAAQ,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;QAC1B,OAAO,IAAI,CAAC,IAAI,CAAC,yBAAiB,CAAC,CAAC;IACtC,CAAC;IAES,KAAK,CAAC,KAAa;QAC3B,OAAO,IAAI,WAAI,CAAC,KAAK,CAAC,CAAC;IACzB,CAAC;CACF;AAnBD,8CAmBC"}

View file

@ -0,0 +1,11 @@
import { BrowserEncoding } from './encoding';
/**
* Hash returned from functions in the browser.
*/
export declare class Hash extends Uint8Array {
/**
* A constant-time comparison against the other hash/array.
*/
equals(other: unknown): boolean;
toString(encoding?: BrowserEncoding): string;
}

View file

@ -0,0 +1,29 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const encoding_1 = require("./encoding");
/**
* Hash returned from functions in the browser.
*/
class Hash extends Uint8Array {
/**
* A constant-time comparison against the other hash/array.
*/
equals(other) {
if (!(other instanceof Uint8Array)) {
return false;
}
if (other.length !== this.length) {
return false;
}
let cmp = 0;
for (let i = 0; i < this.length; i++) {
cmp |= this[i] ^ other[i];
}
return cmp === 0;
}
toString(encoding = 'hex') {
return encoding_1.mustGetEncoder(encoding)(this);
}
}
exports.Hash = Hash;
//# sourceMappingURL=hash.js.map

View file

@ -0,0 +1 @@
{"version":3,"file":"hash.js","sourceRoot":"","sources":["../../ts/browser/hash.ts"],"names":[],"mappings":";;AAAA,yCAA6D;AAE7D;;GAEG;AACH,MAAa,IAAK,SAAQ,UAAU;IAClC;;OAEG;IACI,MAAM,CAAC,KAAc;QAC1B,IAAI,CAAC,CAAC,KAAK,YAAY,UAAU,CAAC,EAAE;YAClC,OAAO,KAAK,CAAC;SACd;QAED,IAAI,KAAK,CAAC,MAAM,KAAK,IAAI,CAAC,MAAM,EAAE;YAChC,OAAO,KAAK,CAAC;SACd;QAED,IAAI,GAAG,GAAG,CAAC,CAAC;QACZ,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;YACpC,GAAG,IAAI,IAAI,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;SAC3B;QAED,OAAO,GAAG,KAAK,CAAC,CAAC;IACnB,CAAC;IAEM,QAAQ,CAAC,WAA4B,KAAK;QAC/C,OAAO,yBAAc,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,CAAC;IACxC,CAAC;CACF;AAxBD,oBAwBC"}

View file

@ -0,0 +1,3 @@
export { hash, HashInput, deriveKey, keyedHash } from './hash-fn';
export * from './hash-instance';
export * from '../base/index';

View file

@ -0,0 +1,12 @@
"use strict";
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
Object.defineProperty(exports, "__esModule", { value: true });
var hash_fn_1 = require("./hash-fn");
exports.hash = hash_fn_1.hash;
exports.deriveKey = hash_fn_1.deriveKey;
exports.keyedHash = hash_fn_1.keyedHash;
__export(require("./hash-instance"));
__export(require("../base/index"));
//# sourceMappingURL=index.js.map

View file

@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../ts/browser/index.ts"],"names":[],"mappings":";;;;;AAAA,qCAAkE;AAAzD,yBAAA,IAAI,CAAA;AAAa,8BAAA,SAAS,CAAA;AAAE,8BAAA,SAAS,CAAA;AAC9C,qCAAgC;AAChC,mCAA8B"}

View file

@ -0,0 +1,10 @@
import * as rawWasm from '../../dist/wasm/browser/blake3_js';
/**
* Gets the webassembly module provided in provideWasm.
*/
export declare const getWasm: () => typeof rawWasm;
/**
* Sets the webassembly module used for the browser build. This indirection is
* needed to provide compatibility between the "browser" and "browser-async" modes.
*/
export declare const provideWasm: (w: typeof rawWasm) => void;

View file

@ -0,0 +1,20 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
let wasm;
/**
* Gets the webassembly module provided in provideWasm.
*/
exports.getWasm = () => {
if (!wasm) {
throw new Error('BLAKE3 webassembly not loaded. Please import the module via `blake3/browser` or `blake3/browser-async`');
}
return wasm;
};
/**
* Sets the webassembly module used for the browser build. This indirection is
* needed to provide compatibility between the "browser" and "browser-async" modes.
*/
exports.provideWasm = (w) => {
wasm = w;
};
//# sourceMappingURL=wasm.js.map

View file

@ -0,0 +1 @@
{"version":3,"file":"wasm.js","sourceRoot":"","sources":["../../ts/browser/wasm.ts"],"names":[],"mappings":";;AAEA,IAAI,IAAoB,CAAC;AAEzB;;GAEG;AACU,QAAA,OAAO,GAAG,GAAG,EAAE;IAC1B,IAAI,CAAC,IAAI,EAAE;QACT,MAAM,IAAI,KAAK,CACb,wGAAwG,CACzG,CAAC;KACH;IAED,OAAO,IAAI,CAAC;AACd,CAAC,CAAC;AAEF;;;GAGG;AACU,QAAA,WAAW,GAAG,CAAC,CAAiB,EAAE,EAAE;IAC/C,IAAI,GAAG,CAAC,CAAC;AACX,CAAC,CAAC"}