Tiny hashing module that uses the native crypto API in Node.js and the browser
npx @tessl/cli install tessl/npm-crypto-hash@3.1.0Crypto Hash is a tiny isomorphic hashing library that provides a unified API for cryptographic hashing across Node.js and browser environments. It implements SHA-1, SHA-256, SHA-384, and SHA-512 algorithms using native crypto APIs for optimal performance and security.
npm install crypto-hashimport { sha256, sha512, sha1, sha384 } from "crypto-hash";Environment-specific imports (advanced):
// Force Node.js implementation
import { sha256 } from "crypto-hash/index.js";
// Force browser implementation
import { sha256 } from "crypto-hash/browser.js";import { sha256 } from "crypto-hash";
// Hash a string
const hash = await sha256("π¦");
console.log(hash);
// => '36bf255468003165652fe978eaaa8898e191664028475f83f506dabd95298efc'
// Hash binary data with different output format
const buffer = new TextEncoder().encode("Hello World");
const hashBuffer = await sha256(buffer, { outputFormat: "buffer" });Crypto Hash provides platform-specific optimizations while maintaining a consistent API:
node:crypto with worker thread optimization for non-blocking operationscrypto.subtle.digest) with minimal bundle size (~300 bytes minified & gzipped)Computes SHA-1 hash of input data. Warning: SHA-1 is cryptographically insecure and should not be used for sensitive applications.
function sha1(
input: string | ArrayBuffer | ArrayBufferView,
options?: OptionsHexOutput
): Promise<string>;
function sha1(
input: string | ArrayBuffer | ArrayBufferView,
options: OptionBufferOutput
): Promise<ArrayBuffer>;Usage Example:
import { sha1 } from "crypto-hash";
const hash = await sha1("π¦");
console.log(hash);
// => '5df82936cbf0864be4b7ba801bee392457fde9e4'
// Get ArrayBuffer output
const buffer = await sha1("data", { outputFormat: "buffer" });Computes SHA-256 hash of input data using industry-standard secure algorithm.
function sha256(
input: string | ArrayBuffer | ArrayBufferView,
options?: OptionsHexOutput
): Promise<string>;
function sha256(
input: string | ArrayBuffer | ArrayBufferView,
options: OptionBufferOutput
): Promise<ArrayBuffer>;Usage Example:
import { sha256 } from "crypto-hash";
// String input
const hash = await sha256("π¦");
console.log(hash);
// => '36bf255468003165652fe978eaaa8898e191664028475f83f506dabd95298efc'
// ArrayBuffer input
const data = new Uint8Array([1, 2, 3, 4, 5]);
const bufferHash = await sha256(data);Computes SHA-384 hash of input data for applications requiring longer hash values.
function sha384(
input: string | ArrayBuffer | ArrayBufferView,
options?: OptionsHexOutput
): Promise<string>;
function sha384(
input: string | ArrayBuffer | ArrayBufferView,
options: OptionBufferOutput
): Promise<ArrayBuffer>;Usage Example:
import { sha384 } from "crypto-hash";
const hash = await sha384("π¦");
console.log(hash);
// => 'a9d4dfb503394bd9701d60eb5fb1d7fb800580b43d874165103b16d311fb5c97545cb89f06c31f30e219f5b603e834ca'Computes SHA-512 hash of input data providing the highest level of hash security in the SHA-2 family.
function sha512(
input: string | ArrayBuffer | ArrayBufferView,
options?: OptionsHexOutput
): Promise<string>;
function sha512(
input: string | ArrayBuffer | ArrayBufferView,
options: OptionBufferOutput
): Promise<ArrayBuffer>;Usage Example:
import { sha512 } from "crypto-hash";
const hash = await sha512("π¦");
console.log(hash);
// => '7d9e515c59bd15d0692f9bc0c68f50f82b62a99bef4b8dc490cec165296210dff005529a4cb84a655eee6ddec82339e6bdbab21bdb287b71a543a56cfab53905'type OptionsHexOutput = {
outputFormat?: 'hex';
};
type OptionBufferOutput = {
outputFormat: 'buffer';
};Input Types:
string - UTF-8 encoded stringsArrayBuffer - Raw binary data buffersArrayBufferView - Typed arrays like Uint8Array, Int32Array, etc.Output Formats:
'hex' (default) - Returns lowercase hexadecimal string representation'buffer' - Returns ArrayBuffer with raw binary hash datanode:crypto.createHash() for hash computationcrypto.subtle.digest) for native performanceCommon error scenarios: