CyberChef/src/core/operations/BLAKE2b.mjs

80 lines
2.5 KiB
JavaScript
Raw Normal View History

/**
2019-03-26 20:13:00 +01:00
* @author h345983745
* @copyright Crown Copyright 2019
* @license Apache-2.0
*/
import Operation from "../Operation";
import blakejs from "blakejs";
2019-03-26 22:12:03 +01:00
import OperationError from "../errors/OperationError";
import Utils from "../Utils";
import { toBase64 } from "../lib/Base64";
/**
* BLAKE2b operation
*/
class BLAKE2b extends Operation {
/**
* BLAKE2b constructor
*/
constructor() {
super();
this.name = "BLAKE2b";
this.module = "Hashing";
2019-03-31 19:46:29 +02:00
this.description = `Performs BLAKE2b hashing on the input.
<br><br> BLAKE2b is a flavour of the BLAKE cryptographic hash function that is optimized for 64-bit platforms and produces digests of any size between 1 and 64 bytes.
<br><br> Supports the use of an optional key.`;
this.infoURL = "https://wikipedia.org/wiki/BLAKE_(hash_function)#BLAKE2b_algorithm";
this.inputType = "ArrayBuffer";
this.outputType = "string";
this.args = [
{
"name": "Size",
"type": "option",
"value": ["512", "384", "256", "160", "128"]
2019-03-26 22:12:03 +01:00
}, {
"name": "Output Encoding",
"type": "option",
"value": ["Hex", "Base64", "Raw"]
2019-03-31 19:32:53 +02:00
}, {
"name": "Key",
"type": "toggleString",
"value": "",
"toggleValues": ["UTF8", "Decimal", "Base64", "Hex", "Latin1"]
}
];
}
/**
* @param {ArrayBuffer} input
* @param {Object[]} args
2019-03-26 22:12:03 +01:00
* @returns {string} The input having been hashed with BLAKE2b in the encoding format speicifed.
*/
run(input, args) {
2019-03-26 22:12:03 +01:00
const [outSize, outFormat] = args;
2019-03-31 19:32:53 +02:00
let key = Utils.convertToByteArray(args[2].string || "", args[2].option);
if (key.length === 0) {
2019-03-31 19:32:53 +02:00
key = null;
} else if (key.length > 64) {
2019-03-31 19:46:29 +02:00
throw new OperationError(["Key cannot be greater than 64 bytes", "It is currently " + key.length + " bytes."].join("\n"));
2019-03-31 19:32:53 +02:00
}
input = new Uint8Array(input);
2019-03-26 22:12:03 +01:00
switch (outFormat) {
case "Hex":
2019-03-31 19:32:53 +02:00
return blakejs.blake2bHex(input, key, outSize / 8);
2019-03-26 22:12:03 +01:00
case "Base64":
2019-03-31 19:32:53 +02:00
return toBase64(blakejs.blake2b(input, key, outSize / 8));
2019-03-26 22:12:03 +01:00
case "Raw":
2019-03-31 19:32:53 +02:00
return Utils.arrayBufferToStr(blakejs.blake2b(input, key, outSize / 8).buffer);
2019-03-26 22:12:03 +01:00
default:
return new OperationError("Unsupported Output Type");
}
}
}
export default BLAKE2b;