@ayonli/jsext
Version:
A JavaScript extension package for building strong and modern applications.
27 lines (26 loc) • 1.47 kB
TypeScript
export type BufferSource = string | ArrayBuffer | ArrayBufferView;
export type DataSource = BufferSource | ReadableStream<Uint8Array> | Blob;
/**
* Calculates the hash of the given data.
*
* This function uses the same algorithm as the [string-hash](https://www.npmjs.com/package/string-hash)
* package, non-string data are converted to strings before hashing.
*
* @example
* ```ts
* import hash from "@ayonli/jsext/hash";
*
* console.log(hash("Hello, World!")); // 4010631688
* console.log(hash(new Uint8Array([1, 2, 3]))); // 193378021
* ```
*/
export declare function hash(data: BufferSource): number;
export declare function toBytes(data: DataSource): Promise<Uint8Array>;
export declare function sha1(data: DataSource): Promise<ArrayBuffer>;
export declare function sha1(data: DataSource, encoding: "hex" | "base64"): Promise<string>;
export declare function sha256(data: DataSource): Promise<ArrayBuffer>;
export declare function sha256(data: DataSource, encoding: "hex" | "base64"): Promise<string>;
export declare function sha512(data: DataSource): Promise<ArrayBuffer>;
export declare function sha512(data: DataSource, encoding: "hex" | "base64"): Promise<string>;
export declare function hmac(algorithm: "sha1" | "sha256" | "sha512", key: BufferSource, data: DataSource): Promise<ArrayBuffer>;
export declare function hmac(algorithm: "sha1" | "sha256" | "sha512", key: BufferSource, data: DataSource, encoding: "hex" | "base64"): Promise<string>;