CyberChef/src/core/operations/EncodeText.mjs

59 lines
1.4 KiB
JavaScript
Raw Normal View History

2018-05-15 17:03:41 +02:00
/**
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*/
import Operation from "../Operation.mjs";
import cptable from "codepage";
import {CHR_ENC_CODE_PAGES} from "../lib/ChrEnc.mjs";
2018-05-15 17:03:41 +02:00
/**
* Encode text operation
*/
class EncodeText extends Operation {
/**
* EncodeText constructor
*/
constructor() {
super();
this.name = "Encode text";
this.module = "Encodings";
2018-05-15 17:03:41 +02:00
this.description = [
"Encodes text into the chosen character encoding.",
"<br><br>",
"Supported charsets are:",
"<ul>",
Object.keys(CHR_ENC_CODE_PAGES).map(e => `<li>${e}</li>`).join("\n"),
2018-05-15 17:03:41 +02:00
"</ul>",
].join("\n");
this.infoURL = "https://wikipedia.org/wiki/Character_encoding";
2018-05-15 17:03:41 +02:00
this.inputType = "string";
this.outputType = "ArrayBuffer";
2018-05-15 17:03:41 +02:00
this.args = [
{
"name": "Encoding",
"type": "option",
"value": Object.keys(CHR_ENC_CODE_PAGES)
2018-05-15 17:03:41 +02:00
}
];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {ArrayBuffer}
2018-05-15 17:03:41 +02:00
*/
run(input, args) {
const format = CHR_ENC_CODE_PAGES[args[0]];
const encoded = cptable.utils.encode(format, input);
return new Uint8Array(encoded).buffer;
2018-05-15 17:03:41 +02:00
}
}
export default EncodeText;