Developer Documentation
TypeScript
Updated September 19, 2026

Cryptographic Hashing in TypeScript

Compute secure SHA-256 digests and encode them as hexadecimal or Base64 in TypeScript.

Cryptographic hash functions turn arbitrary input into a fixed-size deterministic digest. SHA-256 is a sensible default for checksums, content fingerprints, and integrity verification.

Quick Start#

The following example uses the standard TypeScript API or a widely used package for this task.

Web Crypto quick start#

TypeScript
1async function sha256(text: string): Promise<string> {2  const bytes = new TextEncoder().encode(text);3  const digest = await crypto.subtle.digest("SHA-256", bytes);4  return Array.from(new Uint8Array(digest))5    .map((b) => b.toString().padStart(, "0"))6    .join("");7}89console.log(await sha256("ToolPorta"));

Common Operations#

This second pattern covers a common production operation or a safer way to handle real input.

Node.js#

TypeScript
1import { createHash } from "node:crypto";23function sha256Node(text: string): string {4  return createHash("sha256").update(text, "utf8").digest("hex");5}

Important Notes#

Hash bytes, not abstract strings: use UTF-8 consistently when two systems must produce the same digest.

Best Practices#

  1. Treat external input as untrusted and handle parse/validation errors explicitly.
  2. Keep encoding, units, formats, and library-specific mapping rules explicit at system boundaries.
  3. Add test vectors that cover empty input, Unicode text, malformed input, and representative production data.

References#

Implement Hashing in other languages: