57 lines
1.8 KiB
TypeScript
Executable File
57 lines
1.8 KiB
TypeScript
Executable File
#!/usr/bin/env runts -- --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
|
|
|
|
import { log } from "https://global.hatter.ink/script/get/@1/deno-commons-mod.ts";
|
|
|
|
const { X509Certificate, createPublicKey } = await import("node:crypto");
|
|
|
|
if (Deno.args.length === 0) {
|
|
console.log("Usage: publickey.ts <certificate|publickey|jwk>");
|
|
Deno.exit(1);
|
|
}
|
|
|
|
// function tryParseCertificate() {
|
|
// const x509 = new X509Certificate(Deno.args[0]);
|
|
//
|
|
// const exportedJwk = x509.publicKey.export({format: "jwk"});
|
|
// console.log(JSON.stringify(exportedJwk, null, 4));
|
|
//
|
|
// console.log(x509.publicKey.export({format: "pem", type: "spki"}));
|
|
// }
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
function printPublicKey(publicKey: any) {
|
|
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];
|
|
try {
|
|
const publicKey = createPublicKey(firstArgument);
|
|
printPublicKey(publicKey);
|
|
Deno.exit(0);
|
|
} catch (e) {
|
|
// try jwk
|
|
try {
|
|
const key = JSON.parse(firstArgument);
|
|
const publicKey = createPublicKey({ key, format: "jwk" });
|
|
printPublicKey(publicKey);
|
|
Deno.exit(0);
|
|
} catch (_e) {
|
|
// IGNORE
|
|
}
|
|
|
|
log.error(`Parse certificate or public key failed: ${e}`);
|
|
Deno.exit(1);
|
|
}
|
|
|
|
// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20250621T102817+08:00.MEUCIQCPEZwTEQZItYIu2Gxt
|
|
// Fjrt9RMPnZkktrMNsjsia9rZIgIgMQGerRyb2lKuEUmC94iMPa9cgAciBJGhBi7l1rPf3XI=
|