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

@@ -35,12 +35,12 @@
}, },
"publickey.ts": { "publickey.ts": {
"script_name": "publickey.ts", "script_name": "publickey.ts",
"script_length": 1572, "script_length": 1720,
"script_sha256": "83947c19952ef8f69f50354b809bc584e4ff702a663364c9572db8a0bb32cc8e", "script_sha256": "05096a661ceb9fe417fe0628c69ab5276a92057c12507358fa0e0539ee22c455",
"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,
"publish_time": 1737272626140, "publish_time": 1737272626140,
"update_time": 1737272626140 "update_time": 1737275658224
}, },
"sigstore-verify.ts": { "sigstore-verify.ts": {
"script_name": "sigstore-verify.ts", "script_name": "sigstore-verify.ts",

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