mirror of
https://github.com/gchq/CyberChef
synced 2025-01-12 04:28:53 +00:00
Output generated RSA keys as JSON Web Key
This commit is contained in:
parent
cc28c6af1a
commit
9f7a75536e
1 changed files with 35 additions and 0 deletions
|
@ -7,6 +7,8 @@
|
||||||
|
|
||||||
import Operation from "../Operation.mjs";
|
import Operation from "../Operation.mjs";
|
||||||
import forge from "node-forge";
|
import forge from "node-forge";
|
||||||
|
import { toBase64 } from "../lib/Base64.mjs";
|
||||||
|
import { fromHex } from "../lib/Hex.mjs";
|
||||||
import { cryptNotice } from "../lib/Crypt.mjs";
|
import { cryptNotice } from "../lib/Crypt.mjs";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -41,6 +43,7 @@ class GenerateRSAKeyPair extends Operation {
|
||||||
type: "option",
|
type: "option",
|
||||||
value: [
|
value: [
|
||||||
"PEM",
|
"PEM",
|
||||||
|
"JWK",
|
||||||
"JSON",
|
"JSON",
|
||||||
"DER"
|
"DER"
|
||||||
]
|
]
|
||||||
|
@ -70,6 +73,38 @@ class GenerateRSAKeyPair extends Operation {
|
||||||
case "PEM":
|
case "PEM":
|
||||||
result = forge.pki.publicKeyToPem(keypair.publicKey) + "\n" + forge.pki.privateKeyToPem(keypair.privateKey);
|
result = forge.pki.publicKeyToPem(keypair.publicKey) + "\n" + forge.pki.privateKeyToPem(keypair.privateKey);
|
||||||
break;
|
break;
|
||||||
|
case "JWK": {
|
||||||
|
const base64urlUInt = function (bigInt) {
|
||||||
|
let hex = bigInt.toString(16);
|
||||||
|
// prepend 0 if not even
|
||||||
|
if (hex.length % 2 === 1) {
|
||||||
|
hex = "0" + hex;
|
||||||
|
}
|
||||||
|
return toBase64(fromHex(hex), "A-Za-z0-9-_");
|
||||||
|
};
|
||||||
|
const pubKey = {
|
||||||
|
kty: "RSA",
|
||||||
|
kid: "PublicKey",
|
||||||
|
key_ops: ["verify", "encrypt"], // eslint-disable-line camelcase
|
||||||
|
n: base64urlUInt(keypair.publicKey.n),
|
||||||
|
e: base64urlUInt(keypair.publicKey.e)
|
||||||
|
};
|
||||||
|
const privKey = {
|
||||||
|
kty: "RSA",
|
||||||
|
kid: "PrivateKey",
|
||||||
|
key_ops: ["sign", "decrypt"], // eslint-disable-line camelcase
|
||||||
|
n: base64urlUInt(keypair.privateKey.n),
|
||||||
|
e: base64urlUInt(keypair.privateKey.e),
|
||||||
|
d: base64urlUInt(keypair.privateKey.d),
|
||||||
|
p: base64urlUInt(keypair.privateKey.p),
|
||||||
|
q: base64urlUInt(keypair.privateKey.q),
|
||||||
|
dp: base64urlUInt(keypair.privateKey.dP),
|
||||||
|
dq: base64urlUInt(keypair.privateKey.dQ),
|
||||||
|
qi: base64urlUInt(keypair.privateKey.qInv)
|
||||||
|
};
|
||||||
|
result = JSON.stringify({keys: [privKey, pubKey]}, null, 4);
|
||||||
|
break;
|
||||||
|
}
|
||||||
case "JSON":
|
case "JSON":
|
||||||
result = JSON.stringify(keypair);
|
result = JSON.stringify(keypair);
|
||||||
break;
|
break;
|
||||||
|
|
Loading…
Reference in a new issue