feat: update publickey.ts

This commit is contained in:
2025-01-19 16:34:32 +08:00
parent 86d2b0b3f6
commit 05f64ab761
2 changed files with 14 additions and 13 deletions

View File

@@ -1,11 +1,13 @@
#!/usr/bin/env -S deno run --allow-env
#!/usr/bin/env -S deno run --allow-env --allow-import
// Reference:
// - https://docs.deno.com/examples/command_line_arguments/
// - https://docs.deno.com/api/node/crypto/~/X509Certificate#property_issuercertificate
// - https://docs.deno.com/api/node/crypto/~/createPublicKey
const {X509Certificate, createPublicKey} = await import("node:crypto");
import {log} from "https://hatter.ink/script/fetch/library/deno-commons-mod.ts?202501191623";
const { X509Certificate, createPublicKey } = await import("node:crypto");
if (Deno.args.length === 0) {
console.log("Usage: publickey.ts <certificate|publickey|jwk>");
@@ -23,12 +25,11 @@ if (Deno.args.length === 0) {
// deno-lint-ignore no-explicit-any
function printPublicKey(publicKey: any) {
console.log('JWK:');
const exportedJwk = publicKey.export({format: "jwk"});
console.log(JSON.stringify(exportedJwk, null, 4));
console.log();
console.log("PEM:");
console.log(publicKey.export({format: "pem", type: "spki"}));
const exportedJwk = publicKey.export({ format: "jwk" });
const exportedJwkJson = JSON.stringify(exportedJwk, null, 4);
const exportedPem = publicKey.export({ format: "pem", type: "spki" });
log.success(`JWK\n${exportedJwkJson}`);
log.success(`PEM:\n${exportedPem}`);
}
const firstArgument = Deno.args[0];
@@ -40,13 +41,13 @@ try {
// try jwk
try {
const key = JSON.parse(firstArgument);
const publicKey = createPublicKey({key, format: "jwk"});
const publicKey = createPublicKey({ key, format: "jwk" });
printPublicKey(publicKey);
Deno.exit(0);
} catch (_e) {
// IGNORE
}
console.error(`Parse certificate or public key failed: ${e}`);
log.error(`Parse certificate or public key failed: ${e}`);
Deno.exit(1);
}