feat: update publickey.ts

This commit is contained in:
2025-01-19 01:00:31 +08:00
parent 1b4ceb5552
commit 52ffab9f2b
2 changed files with 24 additions and 8 deletions

View File

@@ -20,8 +20,8 @@
}, },
"publickey.ts": { "publickey.ts": {
"script_name": "publickey.ts", "script_name": "publickey.ts",
"script_length": 1148, "script_length": 1530,
"script_sha256": "6be353c25d28e88e973c91f92d93728a7216dd08845fe76a7456121b5c3374a1", "script_sha256": "ffd1181afb7bbc9cf0f45c2617dc956d4d363fd893456ad0ebc03fc63bab64ad",
"script_full_url": "https://git.hatter.ink/hatter/ts-scripts/raw/branch/main/single-scripts/publickey.ts", "script_full_url": "https://git.hatter.ink/hatter/ts-scripts/raw/branch/main/single-scripts/publickey.ts",
"single_script_file": true "single_script_file": true
}, },

View File

@@ -8,7 +8,7 @@
const {X509Certificate, createPublicKey} = await import('node:crypto'); const {X509Certificate, createPublicKey} = await import('node:crypto');
if (Deno.args.length === 0) { if (Deno.args.length === 0) {
console.log("Usage: certificate.ts <certificate|publickey>"); console.log("Usage: publickey.ts <certificate|publickey|jwk>");
Deno.exit(1); Deno.exit(1);
} }
@@ -21,15 +21,31 @@ if (Deno.args.length === 0) {
// console.log(x509.publicKey.export({format: "pem", type: "spki"})); // console.log(x509.publicKey.export({format: "pem", type: "spki"}));
// } // }
try { function printPublicKey(publicKey) {
const publicKey = createPublicKey(Deno.args[0]);
console.log('JWK:'); console.log('JWK:');
const exportedJwk = publicKey.export({format: "jwk"}); const exportedJwk = publicKey.export({format: "jwk"});
console.log(JSON.stringify(exportedJwk, null, 4)); console.log(JSON.stringify(exportedJwk, null, 4));
console.log(); console.log();
console.log("PEM:"); console.log("PEM:");
console.log(publicKey.export({format: "pem", type: "spki"})); console.log(publicKey.export({format: "pem", type: "spki"}));
} catch (e) { }
console.error(`Parse certificate or public key failed: ${e}`);
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);
} }