UNPKG

htmelt

Version:

Bundle your HTML assets with Esbuild and LightningCSS. Custom plugins, HMR platform, and more.

118 lines (117 loc) 2.66 kB
// src/https/createCertificate.mts import forge from "node-forge/lib/forge.js"; import "node-forge/lib/pki.js"; function toPositiveHex(hexString) { let mostSignificativeHexAsInt = parseInt(hexString[0], 16); if (mostSignificativeHexAsInt < 8) { return hexString; } mostSignificativeHexAsInt -= 8; return mostSignificativeHexAsInt.toString() + hexString.substring(1); } function createCertificate() { const days = 30; const keySize = 2048; const extensions = [ // { // name: 'basicConstraints', // cA: true, // }, { name: "keyUsage", keyCertSign: true, digitalSignature: true, nonRepudiation: true, keyEncipherment: true, dataEncipherment: true }, { name: "extKeyUsage", serverAuth: true, clientAuth: true, codeSigning: true, timeStamping: true }, { name: "subjectAltName", altNames: [ { // type 2 is DNS type: 2, value: "localhost" }, { type: 2, value: "localhost.localdomain" }, { type: 2, value: "lvh.me" }, { type: 2, value: "*.lvh.me" }, { type: 2, value: "[::1]" }, { // type 7 is IP type: 7, ip: "127.0.0.1" }, { type: 7, ip: "fe80::1" } ] } ]; const attrs = [ { name: "commonName", value: "example.org" }, { name: "countryName", value: "US" }, { shortName: "ST", value: "Virginia" }, { name: "localityName", value: "Blacksburg" }, { name: "organizationName", value: "Test" }, { shortName: "OU", value: "Test" } ]; const keyPair = forge.pki.rsa.generateKeyPair(keySize); const cert = forge.pki.createCertificate(); cert.serialNumber = toPositiveHex( forge.util.bytesToHex(forge.random.getBytesSync(9)) ); cert.validity.notBefore = /* @__PURE__ */ new Date(); cert.validity.notAfter = /* @__PURE__ */ new Date(); cert.validity.notAfter.setDate(cert.validity.notBefore.getDate() + days); cert.setSubject(attrs); cert.setIssuer(attrs); cert.publicKey = keyPair.publicKey; cert.setExtensions(extensions); const algorithm = forge.md.sha256.create(); cert.sign(keyPair.privateKey, algorithm); const privateKeyPem = forge.pki.privateKeyToPem(keyPair.privateKey); const certPem = forge.pki.certificateToPem(cert); return privateKeyPem + certPem; } export { createCertificate };