Tidied up 'PEM to Hex' operation

This commit is contained in:
n1474335 2022-06-03 22:57:49 +01:00
parent 1464e5d5e4
commit dc46018757
2 changed files with 13 additions and 13 deletions

View file

@ -5,10 +5,10 @@
* @license Apache-2.0 * @license Apache-2.0
*/ */
import {fromBase64} from "../lib/Base64.mjs"; import { fromBase64 } from "../lib/Base64.mjs";
import { toHexFast } from "../lib/Hex.mjs";
import Operation from "../Operation.mjs"; import Operation from "../Operation.mjs";
import OperationError from "../errors/OperationError.mjs"; import OperationError from "../errors/OperationError.mjs";
import Utils from "../Utils.mjs";
/** /**
* PEM to Hex operation * PEM to Hex operation
@ -22,9 +22,9 @@ class PEMToHex extends Operation {
super(); super();
this.name = "PEM to Hex"; this.name = "PEM to Hex";
this.module = "PublicKey"; this.module = "Default";
this.description = "Converts PEM (Privacy Enhanced Mail) format to a hexadecimal DER (Distinguished Encoding Rules) string."; this.description = "Converts PEM (Privacy Enhanced Mail) format to a hexadecimal DER (Distinguished Encoding Rules) string.";
this.infoURL = "https://wikipedia.org/wiki/X.690#DER_encoding"; this.infoURL = "https://en.wikipedia.org/wiki/Privacy-Enhanced_Mail#Format";
this.inputType = "string"; this.inputType = "string";
this.outputType = "string"; this.outputType = "string";
this.args = []; this.args = [];
@ -42,7 +42,7 @@ class PEMToHex extends Operation {
* @returns {string} * @returns {string}
*/ */
run(input, args) { run(input, args) {
let output = ""; const output = [];
let match; let match;
const regex = /-----BEGIN ([A-Z][A-Z ]+[A-Z])-----/g; const regex = /-----BEGIN ([A-Z][A-Z ]+[A-Z])-----/g;
while ((match = regex.exec(input)) !== null) { while ((match = regex.exec(input)) !== null) {
@ -57,10 +57,10 @@ class PEMToHex extends Operation {
// decode base64 content // decode base64 content
const base64 = input.substring(indexBase64, indexFooter); const base64 = input.substring(indexBase64, indexFooter);
const bytes = fromBase64(base64, "A-Za-z0-9+/=", "byteArray", true); const bytes = fromBase64(base64, "A-Za-z0-9+/=", "byteArray", true);
const hex = bytes.map(b => Utils.hex(b)).join(""); const hex = toHexFast(bytes);
output += hex; output.push(hex);
} }
return output; return output.join("\n");
} }
} }

View file

@ -74,7 +74,7 @@ iwY6d+at4xDlIHwvZZmG4Smk56eHhvQE3I8sSAzgoLMBamQ5m3MbiULAYtxskCpC
fjFxrL6Ziaaj7HZoneF40R30KCI9ygF8vkzxLwe3t5Y4XgHL9TYQm1+BDninupIB fjFxrL6Ziaaj7HZoneF40R30KCI9ygF8vkzxLwe3t5Y4XgHL9TYQm1+BDninupIB
/zTeO1ygBGA66m6zpmkmuG7d8HXIducz+wIDAQAB /zTeO1ygBGA66m6zpmkmuG7d8HXIducz+wIDAQAB
-----END RSA PUBLIC KEY-----`; -----END RSA PUBLIC KEY-----`;
const PEMS_RSA_PUBLIC_KEY_PKCS8 = `-----BEGIN PUBLIC KEY----- const PEMS_RSA_PUBLIC_KEY_PKCS8 = `-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA5WykLKHiBAhmZh5Whocg MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA5WykLKHiBAhmZh5Whocg
pQQqZjdrApuRxRT21SJZx6Ce+Oz2V17/heozu5LEz63jCxW1NrBckzl/Ys8p9Leq pQQqZjdrApuRxRT21SJZx6Ce+Oz2V17/heozu5LEz63jCxW1NrBckzl/Ys8p9Leq
@ -144,7 +144,7 @@ TestRegister.addTests([
}, },
{ {
name: "PEMtoHex: No footer", name: "PEMtoHex: No footer",
input: PEMS_RSA_PRIVATE_KEY_PKCS1.substr(0, 200), input: PEMS_RSA_PRIVATE_KEY_PKCS1.substring(0, 200),
expectedOutput: "PEM footer '-----END RSA PRIVATE KEY-----' not found", expectedOutput: "PEM footer '-----END RSA PRIVATE KEY-----' not found",
recipeConfig: [ recipeConfig: [
{ {
@ -155,7 +155,7 @@ TestRegister.addTests([
}, },
{ {
name: "PEMtoHex: Multiple PEMs", name: "PEMtoHex: Multiple PEMs",
input: PEMS_FOO + '\n' + PEMS_BAR, input: PEMS_FOO + "\n" + PEMS_BAR,
expectedOutput: "FOOBAR", expectedOutput: "FOOBAR",
recipeConfig: [ recipeConfig: [
{ {
@ -164,13 +164,13 @@ TestRegister.addTests([
}, },
{ {
"op": "From Hex", "op": "From Hex",
"args": ["None"] "args": ["Auto"]
} }
] ]
}, },
{ {
name: "PEMtoHex: Single line PEM", name: "PEMtoHex: Single line PEM",
input: PEMS_FOO.replace(/(\n|\r)/gm,""), input: PEMS_FOO.replace(/(\n|\r)/gm, ""),
expectedOutput: "FOO", expectedOutput: "FOO",
recipeConfig: [ recipeConfig: [
{ {