chore: node-acme-client转换为esm

This commit is contained in:
xiaojunnuo
2024-11-12 12:15:06 +08:00
parent a9a0967a6f
commit 087c0b8253
21 changed files with 149 additions and 147 deletions
+13 -15
View File
@@ -6,11 +6,10 @@
*
* @namespace forge
*/
const net = require('net');
const { promisify } = require('util');
const forge = require('node-forge');
const { createPrivateEcdsaKey, getPublicKey } = require('./index');
import net from 'net';
import { promisify } from 'util';
import forge from 'node-forge';
import { createPrivateEcdsaKey } from './index.js';
const generateKeyPair = promisify(forge.pki.rsa.generateKeyPair);
@@ -113,13 +112,12 @@ function parseDomains(obj) {
* ```
*/
async function createPrivateKey(size = 2048) {
export async function createPrivateKey(size = 2048) {
const keyPair = await generateKeyPair({ bits: size });
const pemKey = forge.pki.privateKeyToPem(keyPair.privateKey);
return Buffer.from(pemKey);
}
exports.createPrivateKey = createPrivateKey;
/**
* Create public key from a private RSA key
@@ -133,7 +131,7 @@ exports.createPrivateKey = createPrivateKey;
* ```
*/
exports.createPublicKey = async (key) => {
export const createPublicKey = async (key) => {
const privateKey = forge.pki.privateKeyFromPem(key);
const publicKey = forge.pki.rsa.setPublicKey(privateKey.n, privateKey.e);
const pemKey = forge.pki.publicKeyToPem(publicKey);
@@ -148,7 +146,7 @@ exports.createPublicKey = async (key) => {
* @returns {string} PEM body
*/
exports.getPemBody = (str) => {
export const getPemBody = (str) => {
const msg = forge.pem.decode(str)[0];
return forge.util.encode64(msg.body);
};
@@ -160,7 +158,7 @@ exports.getPemBody = (str) => {
* @returns {string[]} Array of PEM bodies
*/
exports.splitPemChain = (str) => forge.pem.decode(str).map(forge.pem.encode);
export const splitPemChain = (str) => forge.pem.decode(str).map(forge.pem.encode);
/**
* Get modulus
@@ -176,7 +174,7 @@ exports.splitPemChain = (str) => forge.pem.decode(str).map(forge.pem.encode);
* ```
*/
exports.getModulus = async (input) => {
export const getModulus = async (input) => {
if (!Buffer.isBuffer(input)) {
input = Buffer.from(input);
}
@@ -199,7 +197,7 @@ exports.getModulus = async (input) => {
* ```
*/
exports.getPublicExponent = async (input) => {
export const getPublicExponent = async (input) => {
if (!Buffer.isBuffer(input)) {
input = Buffer.from(input);
}
@@ -223,7 +221,7 @@ exports.getPublicExponent = async (input) => {
* ```
*/
exports.readCsrDomains = async (csr) => {
export const readCsrDomains = async (csr) => {
if (!Buffer.isBuffer(csr)) {
csr = Buffer.from(csr);
}
@@ -251,7 +249,7 @@ exports.readCsrDomains = async (csr) => {
* ```
*/
exports.readCertificateInfo = async (cert) => {
export const readCertificateInfo = async (cert) => {
if (!Buffer.isBuffer(cert)) {
cert = Buffer.from(cert);
}
@@ -379,7 +377,7 @@ function formatCsrAltNames(altNames) {
* }, certificateKey);
*/
exports.createCsr = async (data, keyType = null) => {
export const createCsr = async (data, keyType = null) => {
let key = null;
if (keyType === 'ec') {
key = await createPrivateEcdsaKey();
+17 -20
View File
@@ -3,12 +3,12 @@
*
* @namespace crypto
*/
import net from 'net';
import { promisify } from 'util';
import crypto from 'crypto';
import asn1js from 'asn1js';
import x509 from '@peculiar/x509';
const net = require('net');
const { promisify } = require('util');
const crypto = require('crypto');
const asn1js = require('asn1js');
const x509 = require('@peculiar/x509');
const randomInt = promisify(crypto.randomInt);
const generateKeyPair = promisify(crypto.generateKeyPair);
@@ -67,7 +67,7 @@ function getKeyInfo(keyPem) {
* ```
*/
async function createPrivateRsaKey(modulusLength = 2048, encodingType = 'pkcs8') {
export async function createPrivateRsaKey(modulusLength = 2048, encodingType = 'pkcs8') {
const pair = await generateKeyPair('rsa', {
modulusLength,
privateKeyEncoding: {
@@ -79,7 +79,6 @@ async function createPrivateRsaKey(modulusLength = 2048, encodingType = 'pkcs8')
return Buffer.from(pair.privateKey);
}
exports.createPrivateRsaKey = createPrivateRsaKey;
/**
* Alias of `createPrivateRsaKey()`
@@ -87,7 +86,7 @@ exports.createPrivateRsaKey = createPrivateRsaKey;
* @function
*/
exports.createPrivateKey = createPrivateRsaKey;
export const createPrivateKey = createPrivateRsaKey;
/**
* Generate a private ECDSA key
@@ -106,7 +105,7 @@ exports.createPrivateKey = createPrivateRsaKey;
* ```
*/
exports.createPrivateEcdsaKey = async (namedCurve = 'P-256', encodingType = 'pkcs8') => {
export const createPrivateEcdsaKey = async (namedCurve = 'P-256', encodingType = 'pkcs8') => {
const pair = await generateKeyPair('ec', {
namedCurve,
privateKeyEncoding: {
@@ -130,7 +129,7 @@ exports.createPrivateEcdsaKey = async (namedCurve = 'P-256', encodingType = 'pkc
* ```
*/
exports.getPublicKey = (keyPem) => {
export const getPublicKey = (keyPem) => {
const info = getKeyInfo(keyPem);
const publicKey = info.publicKey.export({
@@ -155,7 +154,7 @@ exports.getPublicKey = (keyPem) => {
* ```
*/
function getJwk(keyPem) {
export function getJwk(keyPem) {
const jwk = crypto.createPublicKey(keyPem).export({
format: 'jwk',
});
@@ -167,7 +166,6 @@ function getJwk(keyPem) {
}, {});
}
exports.getJwk = getJwk;
/**
* Produce CryptoKeyPair and signing algorithm from a PEM encoded private key
@@ -215,7 +213,7 @@ async function getWebCryptoKeyPair(keyPem) {
* @returns {string[]} Array of PEM objects including headers
*/
function splitPemChain(chainPem) {
export function splitPemChain(chainPem) {
if (Buffer.isBuffer(chainPem)) {
chainPem = chainPem.toString();
}
@@ -225,7 +223,6 @@ function splitPemChain(chainPem) {
.map((params) => x509.PemConverter.encode([params]));
}
exports.splitPemChain = splitPemChain;
/**
* Parse body of PEM encoded object and return a Base64URL string
@@ -235,7 +232,7 @@ exports.splitPemChain = splitPemChain;
* @returns {string} Base64URL-encoded body
*/
exports.getPemBodyAsB64u = (pem) => {
export const getPemBodyAsB64u = (pem) => {
const chain = splitPemChain(pem);
if (!chain.length) {
@@ -286,7 +283,7 @@ function parseDomains(input) {
* ```
*/
exports.readCsrDomains = (csrPem) => {
export const readCsrDomains = (csrPem) => {
if (Buffer.isBuffer(csrPem)) {
csrPem = csrPem.toString();
}
@@ -315,7 +312,7 @@ exports.readCsrDomains = (csrPem) => {
* ```
*/
exports.readCertificateInfo = (certPem) => {
export const readCertificateInfo = (certPem) => {
if (Buffer.isBuffer(certPem)) {
certPem = certPem.toString();
}
@@ -449,7 +446,7 @@ function createSubjectAltNameExtension(altNames) {
* ```
*/
exports.createCsr = async (data, keyPem = null) => {
export const createCsr = async (data, keyPem = null) => {
if (!keyPem) {
keyPem = await createPrivateRsaKey(data.keySize);
}
@@ -520,7 +517,7 @@ exports.createCsr = async (data, keyPem = null) => {
* ```
*/
exports.createAlpnCertificate = async (authz, keyAuthorization, keyPem = null) => {
export const createAlpnCertificate = async (authz, keyAuthorization, keyPem = null) => {
if (!keyPem) {
keyPem = await createPrivateRsaKey();
}
@@ -583,7 +580,7 @@ exports.createAlpnCertificate = async (authz, keyAuthorization, keyPem = null) =
* @returns {boolean} True when valid
*/
exports.isAlpnCertificateAuthorizationValid = (certPem, keyAuthorization) => {
export const isAlpnCertificateAuthorizationValid = (certPem, keyAuthorization) => {
const expected = crypto.createHash('sha256').update(keyAuthorization).digest('hex');
/* Attempt to locate ALPN extension */