diff --git a/src/core/operations/PEMToHex.mjs b/src/core/operations/PEMToHex.mjs index 1c42d6a9..a4d0bdf5 100644 --- a/src/core/operations/PEMToHex.mjs +++ b/src/core/operations/PEMToHex.mjs @@ -5,10 +5,10 @@ * @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 OperationError from "../errors/OperationError.mjs"; -import Utils from "../Utils.mjs"; /** * PEM to Hex operation @@ -22,9 +22,9 @@ class PEMToHex extends Operation { super(); 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.infoURL = "https://wikipedia.org/wiki/X.690#DER_encoding"; + this.infoURL = "https://en.wikipedia.org/wiki/Privacy-Enhanced_Mail#Format"; this.inputType = "string"; this.outputType = "string"; this.args = []; @@ -42,7 +42,7 @@ class PEMToHex extends Operation { * @returns {string} */ run(input, args) { - let output = ""; + const output = []; let match; const regex = /-----BEGIN ([A-Z][A-Z ]+[A-Z])-----/g; while ((match = regex.exec(input)) !== null) { @@ -57,10 +57,10 @@ class PEMToHex extends Operation { // decode base64 content const base64 = input.substring(indexBase64, indexFooter); const bytes = fromBase64(base64, "A-Za-z0-9+/=", "byteArray", true); - const hex = bytes.map(b => Utils.hex(b)).join(""); - output += hex; + const hex = toHexFast(bytes); + output.push(hex); } - return output; + return output.join("\n"); } } diff --git a/tests/operations/tests/PEMtoHex.mjs b/tests/operations/tests/PEMtoHex.mjs index 7929bfdd..b05c40a7 100644 --- a/tests/operations/tests/PEMtoHex.mjs +++ b/tests/operations/tests/PEMtoHex.mjs @@ -74,7 +74,7 @@ iwY6d+at4xDlIHwvZZmG4Smk56eHhvQE3I8sSAzgoLMBamQ5m3MbiULAYtxskCpC fjFxrL6Ziaaj7HZoneF40R30KCI9ygF8vkzxLwe3t5Y4XgHL9TYQm1+BDninupIB /zTeO1ygBGA66m6zpmkmuG7d8HXIducz+wIDAQAB -----END RSA PUBLIC KEY-----`; - + const PEMS_RSA_PUBLIC_KEY_PKCS8 = `-----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA5WykLKHiBAhmZh5Whocg pQQqZjdrApuRxRT21SJZx6Ce+Oz2V17/heozu5LEz63jCxW1NrBckzl/Ys8p9Leq @@ -144,7 +144,7 @@ TestRegister.addTests([ }, { 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", recipeConfig: [ { @@ -155,7 +155,7 @@ TestRegister.addTests([ }, { name: "PEMtoHex: Multiple PEMs", - input: PEMS_FOO + '\n' + PEMS_BAR, + input: PEMS_FOO + "\n" + PEMS_BAR, expectedOutput: "FOOBAR", recipeConfig: [ { @@ -164,13 +164,13 @@ TestRegister.addTests([ }, { "op": "From Hex", - "args": ["None"] + "args": ["Auto"] } ] }, { name: "PEMtoHex: Single line PEM", - input: PEMS_FOO.replace(/(\n|\r)/gm,""), + input: PEMS_FOO.replace(/(\n|\r)/gm, ""), expectedOutput: "FOO", recipeConfig: [ {