nhb-toolbox
Version:
A versatile collection of smart, efficient, and reusable utility functions, classes and types for everyday development needs.
67 lines (66 loc) • 2.62 kB
TypeScript
/**
* * Computes the `MD5` digest of the given string using a pure JavaScript implementation.
*
* @remarks
* - Pure JavaScript implementation — runs on any JS engine. Does not rely on `crypto` or **Web APIs** or other external libraries.
* - Highly inspired by the algorithm used in {@link https://github.com/eustatos/pure-md5.git pure-md5} package.
*
* @param str - Input text to hash.
*
* @returns The `MD5` hash as a 32-character hex string.
*
* @example
* const hash = md5("hello");
* // → "5d41402abc4b2a76b9719d911017c592" *
*
* @example
* // Used inside UUID v3
* const digest = md5(namespace + name);
*/
export declare function md5(str: string): string;
/**
* * Computes the `SHA-1` digest of the given string using a pure JavaScript implementation.
*
* @remarks Pure JavaScript implementation — runs on any JS engine. Does not rely on `crypto` or **Web APIs** or other external libraries.
*
* @param msg - Input text to hash.
*
* @returns The `SHA-1` hash as a 40-character hex string.
*
* @example
* const hash = sha1("hello");
* // → "aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d"
*
* @example
* // Used inside UUID v5
* const digest = sha1(namespace + name);
*/
export declare function sha1(msg: string): string;
/**
* * Computes the `SHA-256` hash of a `UTF-8` string and returns it as a lowercase hexadecimal string.
*
* @param msg - The input string to hash. Can contain any `UTF-8` characters.
* @returns A 64-character lowercase hexadecimal string representing the `SHA-256` hash.
*
* @remarks Pure JavaScript implementation — runs on any JS engine. Does not rely on `crypto` or **Web APIs** or other external libraries.
*
* @example
* ```typescript
* // Basic usage
* const hash = sha256('hello');
* // Returns: '2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824'
*
* // Empty string
* const emptyHash = sha256('');
* // Returns: 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'
*
* // Unicode string
* const unicodeHash = sha256('Hello পৃথিবী!');
* // Returns: '7037e204b825b83553ba336a6ec35b796d505599286ae864729ed6cb33ae9fe1'
* ```
*
* @see {@link https://toolbox.nazmul-nhb.dev/docs/utilities/hash/encoding#sha256bytes sha256Bytes} for hashing raw bytes
* @see {@link https://toolbox.nazmul-nhb.dev/docs/utilities/hash/encoding#utf8tobytes utf8ToBytes} for converting string to bytes
* @see {@link https://toolbox.nazmul-nhb.dev/docs/utilities/hash/encoding#bytestohex bytesToHex} for converting bytes to a hexadecimal string
*/
export declare function sha256(msg: string): string;