UNPKG

@safeapi/safeapi

Version:

SafeAPI: Secure, deterministic, and tamper-resistant API policy engine for Node and browser.

17 lines (16 loc) 455 B
import { canonicalJSONStringify } from "./canonical"; /** @internal */ export function stableHash(value) { const json = canonicalJSONStringify(value, 0); let hash = 0; for (let i = 0; i < json.length; i++) { const chr = json.charCodeAt(i); hash = (hash << 5) - hash + chr; hash |= 0; } return `sha256_${Math.abs(hash)}`; } /** @internal */ export function hashCanonical(value) { return stableHash(value); }