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

@ -6,9 +6,9 @@
*/ */
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

@ -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,7 +164,7 @@ TestRegister.addTests([
}, },
{ {
"op": "From Hex", "op": "From Hex",
"args": ["None"] "args": ["Auto"]
} }
] ]
}, },