2018-05-28 00:53:43 +02:00
|
|
|
/**
|
|
|
|
* @author n1474335 [n1474335@gmail.com]
|
2021-11-07 11:21:17 +01:00
|
|
|
* @author cplussharp
|
2018-05-28 00:53:43 +02:00
|
|
|
* @copyright Crown Copyright 2016
|
|
|
|
* @license Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2021-11-07 11:21:17 +01:00
|
|
|
import {fromBase64} from "../lib/Base64.mjs";
|
2019-07-09 13:23:59 +02:00
|
|
|
import Operation from "../Operation.mjs";
|
2021-11-07 11:21:17 +01:00
|
|
|
import OperationError from "../errors/OperationError.mjs";
|
|
|
|
import Utils from "../Utils.mjs";
|
2018-05-28 00:53:43 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* PEM to Hex operation
|
|
|
|
*/
|
|
|
|
class PEMToHex extends Operation {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PEMToHex constructor
|
|
|
|
*/
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.name = "PEM to Hex";
|
|
|
|
this.module = "PublicKey";
|
|
|
|
this.description = "Converts PEM (Privacy Enhanced Mail) format to a hexadecimal DER (Distinguished Encoding Rules) string.";
|
2019-03-31 22:40:54 +02:00
|
|
|
this.infoURL = "https://wikipedia.org/wiki/X.690#DER_encoding";
|
2018-05-28 00:53:43 +02:00
|
|
|
this.inputType = "string";
|
|
|
|
this.outputType = "string";
|
|
|
|
this.args = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
run(input, args) {
|
2021-11-07 11:21:17 +01:00
|
|
|
let output = "";
|
|
|
|
let match;
|
|
|
|
const regex = /-----BEGIN ([A-Z][A-Z ]+[A-Z])-----/g;
|
|
|
|
while ((match = regex.exec(input)) !== null) {
|
|
|
|
// find corresponding end tag
|
|
|
|
const indexBase64 = match.index + match[0].length;
|
|
|
|
const footer = `-----END ${match[1]}-----`;
|
|
|
|
const indexFooter = input.indexOf(footer, indexBase64);
|
|
|
|
if (indexFooter === -1) {
|
|
|
|
throw new OperationError(`PEM footer '${footer}' not found`);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
2018-05-28 00:53:43 +02:00
|
|
|
}
|
2021-11-07 11:21:17 +01:00
|
|
|
return output;
|
2018-05-28 00:53:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export default PEMToHex;
|