Skip to content

5.4 hash payload

wiki[bot] edited this page Aug 23, 2026 · 1 revision

Hash Payload

Generates cryptographic hashes for strings, objects, or binary data using Node.js crypto module.

import { hashPayload } from "@triplef/helpers/hash-payload";

Signature

function hashPayload(
  payload: string | Record<any, any> | Buffer | Uint8Array,
  algorithm: HashPayloadSupportedAlgorithm = "sha256",
  encoder: BinaryToTextEncoding = "hex"
): string;

Supported Algorithms

  • "sha256" (default)
  • "sha384"
  • "sha512"

Output Encoders

  • "hex" (default) - Hexadecimal string
  • "base64" - Base64 encoded string
  • "latin1" - Latin-1 encoded string

Parameters

Parameter Type Description
payload string | object | Buffer | Uint8Array Data to hash
algorithm HashPayloadSupportedAlgorithm Hash algorithm (default: "sha256")
encoder BinaryToTextEncoding Output encoding (default: "hex")

Input Handling

Input Type Behavior
string Hashed directly with UTF-8 encoding
object Automatically JSON.stringified before hashing
Buffer Hashed directly
Uint8Array Hashed directly

Examples

// Hash a string (default SHA-256, hex encoded)
const hash1 = hashPayload("hello");
// Output: "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"

// Hash an object (automatically JSON.stringified)
const hash2 = hashPayload({ foo: "bar" });
// Output: SHA-256 hash of '{"foo":"bar"}'

// SHA-512 with base64 encoding
const hash3 = hashPayload("data", "sha512", "base64");

// SHA-384
const hash4 = hashPayload(buffer, "sha384");

// Hash a Buffer
const buffer = Buffer.from("hello");
const hash5 = hashPayload(buffer);

Clone this wiki locally