nuxt-security
Version:
🛡️ Security Module for Nuxt based on HTTP Headers and Middleware
24 lines (21 loc) • 745 B
JavaScript
import { webcrypto } from 'node:crypto';
globalThis.crypto ??= webcrypto;
async function generateHash(content, hashAlgorithm) {
let buffer;
if (typeof content === "string") {
buffer = new TextEncoder().encode(content);
} else {
buffer = new Uint8Array(content);
}
const hashBuffer = await crypto.subtle.digest(hashAlgorithm, buffer);
const base64 = btoa(String.fromCharCode(...new Uint8Array(hashBuffer)));
const prefix = hashAlgorithm.replace("-", "").toLowerCase();
return `${prefix}-${base64}`;
}
function generateRandomNonce() {
const array = new Uint8Array(18);
crypto.getRandomValues(array);
const nonce = btoa(String.fromCharCode(...array));
return nonce;
}
export { generateHash, generateRandomNonce };