Suite of AT Protocol TypeScript libraries built on web standards
1import * as noble from "@noble/hashes/sha2.js";
2import { fromString, toString } from "@atp/bytes";
3
4/**
5 * Creates a SHA-256 hash of the input.
6 * Takes either bytes of utf8 input
7 * @param input - Bytes to hash.
8 */
9export const sha256 = (
10 input: Uint8Array | string,
11): Uint8Array => {
12 const bytes = typeof input === "string" ? fromString(input, "utf8") : input;
13 return noble.sha256(bytes);
14};
15
16/**
17 * Hashes the input using SHA-256 and returns the result as a hexadecimal string.
18 * @param input - Bytes to hash.
19 */
20export const sha256Hex = (
21 input: Uint8Array | string,
22): string => {
23 const hash = sha256(input);
24 return toString(hash, "hex");
25};