-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'qrcodes' of https://github.com/j433866/CyberChef into j…
…433866-qrcodes
- Loading branch information
Showing
7 changed files
with
289 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/** | ||
* @author j433866 [j433866@gmail.com] | ||
* @copyright Crown Copyright 2018 | ||
* @license Apache-2.0 | ||
*/ | ||
|
||
import Operation from "../Operation"; | ||
import OperationError from "../errors/OperationError"; | ||
import qr from "qr-image"; | ||
import { toBase64 } from "../lib/Base64"; | ||
import Magic from "../lib/Magic"; | ||
|
||
/** | ||
* Generate QR Code operation | ||
*/ | ||
class GenerateQRCode extends Operation { | ||
|
||
/** | ||
* GenerateQRCode constructor | ||
*/ | ||
constructor() { | ||
super(); | ||
|
||
this.name = "Generate QR Code"; | ||
this.module = "Image"; | ||
this.description = "Generates a QR code from text."; | ||
this.infoURL = "https://wikipedia.org/wiki/QR_code"; | ||
this.inputType = "string"; | ||
this.outputType = "byteArray"; | ||
this.presentType = "html"; | ||
this.args = [ | ||
{ | ||
"name": "Image Format", | ||
"type": "option", | ||
"value": ["PNG", "SVG"] | ||
}, | ||
{ | ||
"name": "Size of QR module", | ||
"type": "number", | ||
"value": 5 | ||
}, | ||
{ | ||
"name": "Margin", | ||
"type": "number", | ||
"value": 2 | ||
} | ||
]; | ||
} | ||
|
||
/** | ||
* @param {string} input | ||
* @param {Object[]} args | ||
* @returns {File} | ||
*/ | ||
run(input, args) { | ||
// Create new QR image from the input data, and convert it to a buffer | ||
const [format, size, margin] = args; | ||
const qrImage = qr.imageSync(input, { type: format, size: size, margin: margin }); | ||
if (qrImage == null) { | ||
throw new OperationError("Error generating QR code."); | ||
} | ||
if (format === "SVG") { | ||
return [...Buffer.from(qrImage)]; | ||
} else if (format === "PNG") { | ||
// Return the QR image buffer as a byte array | ||
return [...qrImage]; | ||
} else { | ||
throw new OperationError("Error generating QR code."); | ||
} | ||
} | ||
|
||
/** | ||
* Displays the QR image using HTML for web apps | ||
* | ||
* @param {byteArray} data | ||
* @returns {html} | ||
*/ | ||
present(data, args) { | ||
if (!data.length) return ""; | ||
|
||
const [format] = args; | ||
if (format === "SVG") { | ||
let outputData = ""; | ||
for (let i = 0; i < data.length; i++){ | ||
outputData += String.fromCharCode(parseInt(data[i])); | ||
} | ||
return outputData; | ||
} else { | ||
let dataURI = "data:"; | ||
const type = Magic.magicFileType(data); | ||
if (type && type.mime.indexOf("image") === 0){ | ||
dataURI += type.mime + ";"; | ||
} else { | ||
throw new OperationError("Invalid file type"); | ||
} | ||
dataURI += "base64," + toBase64(data); | ||
|
||
return "<img src='" + dataURI + "'>"; | ||
} | ||
} | ||
|
||
} | ||
|
||
export default GenerateQRCode; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/** | ||
* @author j433866 [j433866@gmail.com] | ||
* @copyright Crown Copyright 2018 | ||
* @license Apache-2.0 | ||
*/ | ||
|
||
import Operation from "../Operation"; | ||
import OperationError from "../errors/OperationError"; | ||
import Magic from "../lib/Magic"; | ||
import jsqr from "jsqr"; | ||
import jimp from "jimp"; | ||
|
||
/** | ||
* Parse QR Code operation | ||
*/ | ||
class ParseQRCode extends Operation { | ||
|
||
/** | ||
* ParseQRCode constructor | ||
*/ | ||
constructor() { | ||
super(); | ||
|
||
this.name = "Parse QR Code"; | ||
this.module = "Image"; | ||
this.description = "Reads an image file and attempts to detect and read a QR code from the image.<br><br><u>Normalise Image</u><br>Attempt to normalise the image before parsing it, to try and improve detection of a QR code."; | ||
this.infoURL = "https://wikipedia.org/wiki/QR_code"; | ||
this.inputType = "byteArray"; | ||
this.outputType = "string"; | ||
this.args = [ | ||
{ | ||
"name": "Normalise image", | ||
"type": "boolean", | ||
"value": true | ||
} | ||
]; | ||
} | ||
|
||
/** | ||
* @param {byteArray} input | ||
* @param {Object[]} args | ||
* @returns {string} | ||
*/ | ||
async run(input, args) { | ||
const type = Magic.magicFileType(input); | ||
const [normalise] = args; | ||
// Make sure that the input is an image | ||
if (type && type.mime.indexOf("image") === 0){ | ||
let normalisedImage = null; | ||
if (normalise){ | ||
// Process the image to be easier to read by jsqr | ||
// Disables the alpha channel | ||
// Sets the image default background to white | ||
// Normalises the image colours | ||
// Makes the image greyscale | ||
// Converts image to a JPEG | ||
normalisedImage = await new Promise((resolve, reject) => { | ||
jimp.read(Buffer.from(input)) | ||
.then(image => { | ||
image | ||
.rgba(false) | ||
.background(0xFFFFFFFF) | ||
.normalize() | ||
.greyscale() | ||
.getBuffer(jimp.MIME_JPEG, (error, result) => { | ||
resolve([...result]); | ||
}); | ||
}) | ||
.catch(err => { | ||
reject(new OperationError("Error reading the image file.")); | ||
}); | ||
}); | ||
} else { | ||
normalisedImage = input; | ||
} | ||
if (normalisedImage instanceof OperationError){ | ||
return normalisedImage; | ||
} | ||
return new Promise((resolve, reject) => { | ||
jimp.read(Buffer.from(normalisedImage)) | ||
.then(image => { | ||
if (image.bitmap != null){ | ||
const qrData = jsqr(image.bitmap.data, image.getWidth(), image.getHeight()); | ||
if (qrData != null){ | ||
resolve(qrData.data); | ||
} else { | ||
reject(new OperationError("Couldn't read a QR code from the image.")); | ||
} | ||
} else { | ||
reject(new OperationError("Error reading the normalised image file.")); | ||
} | ||
}) | ||
.catch(err => { | ||
reject(new OperationError("Error reading the normalised image file.")); | ||
}); | ||
}); | ||
} else { | ||
throw new OperationError("Invalid file type."); | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
export default ParseQRCode; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters