CyberChef/src/core/operations/GenerateQRCode.mjs

95 lines
2.7 KiB
JavaScript
Raw Normal View History

2018-12-19 14:27:45 +01:00
/**
* @author j433866 [j433866@gmail.com]
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/
import Operation from "../Operation.mjs";
import OperationError from "../errors/OperationError.mjs";
import { generateQrCode } from "../lib/QRCode.mjs";
import { toBase64 } from "../lib/Base64.mjs";
import { isImage } from "../lib/FileType.mjs";
import Utils from "../Utils.mjs";
2018-12-19 14:27:45 +01:00
/**
* Generate QR Code operation
*/
class GenerateQRCode extends Operation {
/**
* GenerateQRCode constructor
*/
constructor() {
super();
this.name = "Generate QR Code";
this.module = "Image";
2018-12-25 22:54:38 +01:00
this.description = "Generates a Quick Response (QR) code from the input text.<br><br>A QR code is a type of matrix barcode (or two-dimensional barcode) first designed in 1994 for the automotive industry in Japan. A barcode is a machine-readable optical label that contains information about the item to which it is attached.";
this.infoURL = "https://wikipedia.org/wiki/QR_code";
2018-12-19 14:27:45 +01:00
this.inputType = "string";
this.outputType = "ArrayBuffer";
this.presentType = "html";
this.args = [
{
"name": "Image Format",
"type": "option",
2018-12-25 22:54:38 +01:00
"value": ["PNG", "SVG", "EPS", "PDF"]
},
{
2018-12-25 22:54:38 +01:00
"name": "Module size (px)",
"type": "number",
2019-03-08 14:38:59 +01:00
"value": 5,
"min": 1
},
{
2018-12-25 22:54:38 +01:00
"name": "Margin (num modules)",
"type": "number",
2019-03-08 14:38:59 +01:00
"value": 2,
"min": 0
2018-12-25 22:54:38 +01:00
},
{
"name": "Error correction",
"type": "option",
"value": ["Low", "Medium", "Quartile", "High"],
"defaultIndex": 1
}
];
2018-12-19 14:27:45 +01:00
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {ArrayBuffer}
2018-12-19 14:27:45 +01:00
*/
run(input, args) {
2018-12-25 22:54:38 +01:00
const [format, size, margin, errorCorrection] = args;
return generateQrCode(input, format, size, margin, errorCorrection);
}
/**
* Displays the QR image using HTML for web apps
2018-12-25 22:54:38 +01:00
*
* @param {ArrayBuffer} data
* @returns {html}
*/
present(data, args) {
if (!data.byteLength && !data.length) return "";
const dataArray = new Uint8Array(data),
[format] = args;
2018-12-25 22:54:38 +01:00
if (format === "PNG") {
const type = isImage(dataArray);
if (!type) {
throw new OperationError("Invalid file type.");
}
return `<img src="data:${type};base64,${toBase64(dataArray)}">`;
2018-12-19 14:27:45 +01:00
}
2018-12-25 22:54:38 +01:00
return Utils.arrayBufferToStr(data);
2018-12-19 14:27:45 +01:00
}
}
export default GenerateQRCode;