UNPKG

@ayonli/jsext

Version:

A JavaScript extension package for building strong and modern applications.

85 lines (83 loc) 3.18 kB
export type DataSource = string | BufferSource | ReadableStream<Uint8Array> | Blob; /** * Calculates the hash of the given data, the result is a 32-bit unsigned integer. * * 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 default function hash(data: string | BufferSource): number; /** * Calculates the Adler-32 checksum of the given data, the result is a 32-bit * unsigned integer. * * Adler-32 checksum is used in zlib and libpng, it is similar to CRC32 but * faster and less reliable. * * @param previous The previous Adler-32 value, default is `1`. This is useful * when calculating the checksum of a large data in chunks. * * @example * ```ts * import { adler32 } from "@ayonli/jsext/hash"; * * console.log(adler32("Hello, World!")); // 530449514 * console.log(adler32(new Uint8Array([1, 2, 3]))); // 851975 * * // calculate chunks * const chunks = ["Hello, ", "World!"]; * let checksum = 1; * * for (const chunk of chunks) { * checksum = adler32(chunk, checksum); * } * * console.log(checksum); // 530449514 * ``` */ export declare function adler32(data: string | BufferSource, previous?: number): number; /** * Calculates the CRC-32 checksum of the given data, the result is a 32-bit * unsigned integer. * * This function is based on IEEE polynomial, which is widely used by Ethernet * (IEEE 802.3), v.42, fddi, gzip, zip, png and other technologies. * * @param previous The previous checksum value, default is `0`. This is useful * when calculating the CRC of a large data in chunks. * * @example * ```ts * import { crc32 } from "@ayonli/jsext/hash"; * * console.log(crc32("Hello, World!")); // 3964322768 * console.log(crc32(new Uint8Array([1, 2, 3]))); // 1438416925 * * // calculate chunks * const chunks = ["Hello, ", "World!"]; * let checksum = 0; * * for (const chunk of chunks) { * checksum = crc32(chunk, checksum); * } * * console.log(checksum); // 3964322768 * ``` */ export declare function crc32(data: string | BufferSource, previous?: number): number; 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: string | BufferSource, data: DataSource): Promise<ArrayBuffer>; export declare function hmac(algorithm: "sha1" | "sha256" | "sha512", key: string | BufferSource, data: DataSource, encoding: "hex" | "base64"): Promise<string>; export = hash;