2019-03-26 19:09:57 +00:00
|
|
|
/**
|
2019-03-26 19:13:00 +00:00
|
|
|
* @author h345983745
|
2019-03-26 19:09:57 +00:00
|
|
|
* @copyright Crown Copyright 2019
|
|
|
|
* @license Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Operation from "../Operation";
|
|
|
|
import blakejs from "blakejs";
|
2019-03-26 21:12:03 +00:00
|
|
|
import OperationError from "../errors/OperationError";
|
|
|
|
import Utils from "../Utils";
|
|
|
|
import { toBase64 } from "../lib/Base64";
|
2019-03-26 19:09:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* BLAKE2b operation
|
|
|
|
*/
|
|
|
|
class BLAKE2b extends Operation {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* BLAKE2b constructor
|
|
|
|
*/
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.name = "BLAKE2b";
|
|
|
|
this.module = "Hashing";
|
|
|
|
this.description = "Performs BLAKE2b hashing on the input. Returns the output HEX encoded.";
|
2019-03-26 20:32:42 +00:00
|
|
|
this.infoURL = "https://wikipedia.org/wiki/BLAKE_(hash_function)#BLAKE2b_algorithm";
|
2019-03-26 19:09:57 +00:00
|
|
|
this.inputType = "string";
|
|
|
|
this.outputType = "string";
|
|
|
|
this.args = [
|
|
|
|
{
|
|
|
|
"name": "Size",
|
|
|
|
"type": "option",
|
|
|
|
"value": ["512", "384", "256", "160", "128"]
|
2019-03-26 21:12:03 +00:00
|
|
|
}, {
|
|
|
|
"name": "Output Encoding",
|
|
|
|
"type": "option",
|
|
|
|
"value": ["Hex", "Base64", "Raw"]
|
2019-03-26 19:09:57 +00:00
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
2019-03-26 21:12:03 +00:00
|
|
|
* @returns {string} The input having been hashed with BLAKE2b in the encoding format speicifed.
|
2019-03-26 19:09:57 +00:00
|
|
|
*/
|
|
|
|
run(input, args) {
|
2019-03-26 21:12:03 +00:00
|
|
|
const [outSize, outFormat] = args;
|
|
|
|
|
|
|
|
switch (outFormat) {
|
|
|
|
case "Hex":
|
|
|
|
return blakejs.blake2bHex(input, null, outSize / 8);
|
|
|
|
case "Base64":
|
|
|
|
return toBase64(blakejs.blake2b(input, null, outSize / 8));
|
|
|
|
case "Raw":
|
|
|
|
return Utils.arrayBufferToStr(blakejs.blake2b(input, null, outSize / 8).buffer);
|
|
|
|
default:
|
|
|
|
return new OperationError("Unsupported Output Type");
|
|
|
|
}
|
2019-03-26 19:09:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export default BLAKE2b;
|