es-next-tools
Version:
A comprehensive utility library for JavaScript and TypeScript that provides a wide range of functions for common programming tasks, including mathematical operations, date manipulations, array and object handling, string utilities, and more.
17 lines (16 loc) • 589 B
JavaScript
import { createHash } from 'crypto';
/**
* Generates a hash of the given text using the specified algorithm.
* @param {string} text - The text to hash.
* @param {'sha256' | 'md5'} [algorithm='sha256'] - The hashing algorithm to use.
* @returns {string} The resulting hash as a hexadecimal string.
* @example
* // Returns the SHA-256 hash of "Hello, World!"
* hash("Hello, World!");
*
* // Returns the MD5 hash of "Hello, World!"
* hash("Hello, World!", "md5");
*/
export function hash(text, algorithm = 'sha256') {
return createHash(algorithm).update(text).digest('hex');
}