@envelop/response-cache
Version:
15 lines (14 loc) • 617 B
JavaScript
import { crypto, TextEncoder } from '@whatwg-node/fetch';
import { handleMaybePromise } from '@whatwg-node/promise-helpers';
export function hashSHA256(text) {
const inputUint8Array = new TextEncoder().encode(text);
return handleMaybePromise(() => crypto.subtle.digest({ name: 'SHA-256' }, inputUint8Array), arrayBuf => {
const outputUint8Array = new Uint8Array(arrayBuf);
let hash = '';
for (const hexNum of outputUint8Array) {
const hex = hexNum.toString(16);
hash += '00'.slice(0, Math.max(0, 2 - hex.length)) + hex;
}
return hash;
});
}