Packages

A Gleam implementation of CUID2, the secure, collision-resistant ids optimized for horizontal scaling and performance.

Current section

Files

Jump to
glecuid src crypto_ffi.ts
Raw

src/crypto_ffi.ts

// @ts-ignore
import { BitArray } from './gleam.mjs';
let hasher: ((data: Uint8Array<ArrayBuffer>) => Uint8Array) | null = null;
try {
const crypto = await import('@noble/hashes/sha3.js');
hasher = (data) => crypto.sha3_512(data);
} catch {
const crypto = await import('node:crypto');
hasher = (data) => crypto.hash('SHA3-512', data, 'buffer');
}
/**
* Hashes the input using SHA3-512 algorithm.
* @param input - Input string.
* @returns Hash output as Gleam's `BitArray`.
*/
export function hash(input: string): BitArray {
const encoder = new TextEncoder();
const data = encoder.encode(input);
if (!hasher) throw new Error('Hasher is not available.');
const hash = hasher(data);
return new BitArray(hash);
}