Skip to content

sha256Hex

The SHA-256 digest of some data as a hexadecimal string, optionally cut short: a compact content key, not a security check (a short prefix is easy to collide).

Outside a runtime with Web Crypto (crypto.subtle), it rejects with a TypeError.

Available since v3.1.10

import { sha256Hex } from '@helpers4/browser';
// or, from the all-in-one package (same code, one install):
import { sha256Hex } from 'helpers4/browser';
sha256Hex(data: BufferSource, length: number): Promise<string>
ParameterTypeDescription
dataBufferSourceThe data to hash
lengthnumberHow many hex digits to keep, from `1` to `64`. Defaults to `16`

Promise<string> — The first length digits of the digest, in lowercase

A short prefix of the digest names a piece of content.

await sha256Hex(new TextEncoder().encode('abc'), 8) // => 'ba7816bf'

View source on GitHub