Files
ts-scripts/single-scripts/publickey.ts

53 lines
1.5 KiB
TypeScript
Executable File

#!/usr/bin/env -S deno run --allow-env
// 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");
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) {
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 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
}
console.error(`Parse certificate or public key failed: ${e}`);
Deno.exit(1);
}