2018-05-17 15:11:34 +00:00
|
|
|
/**
|
|
|
|
* @author n1474335 [n1474335@gmail.com]
|
|
|
|
* @copyright Crown Copyright 2016
|
|
|
|
* @license Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2019-07-09 12:23:59 +01:00
|
|
|
import Operation from "../Operation.mjs";
|
|
|
|
import OperationError from "../errors/OperationError.mjs";
|
2018-05-17 15:11:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Analyse hash operation
|
|
|
|
*/
|
|
|
|
class AnalyseHash extends Operation {
|
|
|
|
/**
|
|
|
|
* AnalyseHash constructor
|
|
|
|
*/
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.name = "Analyse hash";
|
2018-12-25 23:58:00 +00:00
|
|
|
this.module = "Crypto";
|
2024-02-24 22:59:51 -05:00
|
|
|
this.description =
|
|
|
|
"Tries to determine information about a given hash and suggests which algorithm may have been used to generate it based on its length.";
|
|
|
|
this.infoURL =
|
|
|
|
"https://wikipedia.org/wiki/Comparison_of_cryptographic_hash_functions";
|
2018-05-17 15:11:34 +00:00
|
|
|
this.inputType = "string";
|
|
|
|
this.outputType = "string";
|
|
|
|
this.args = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
run(input, args) {
|
|
|
|
input = input.replace(/\s/g, "");
|
|
|
|
|
|
|
|
let output = "",
|
|
|
|
possibleHashFunctions = [];
|
|
|
|
const byteLength = input.length / 2,
|
|
|
|
bitLength = byteLength * 8;
|
|
|
|
|
|
|
|
if (!/^[a-f0-9]+$/i.test(input)) {
|
|
|
|
throw new OperationError("Invalid hash");
|
|
|
|
}
|
|
|
|
|
2024-02-24 22:59:51 -05:00
|
|
|
output +=
|
|
|
|
"Hash length: " +
|
|
|
|
input.length +
|
|
|
|
"\n" +
|
|
|
|
"Byte length: " +
|
|
|
|
byteLength +
|
|
|
|
"\n" +
|
|
|
|
"Bit length: " +
|
|
|
|
bitLength +
|
|
|
|
"\n\n" +
|
2018-05-17 15:11:34 +00:00
|
|
|
"Based on the length, this hash could have been generated by one of the following hashing functions:\n";
|
|
|
|
|
|
|
|
switch (bitLength) {
|
|
|
|
case 4:
|
|
|
|
possibleHashFunctions = [
|
|
|
|
"Fletcher-4",
|
|
|
|
"Luhn algorithm",
|
|
|
|
"Verhoeff algorithm",
|
|
|
|
];
|
|
|
|
break;
|
|
|
|
case 8:
|
2024-02-24 22:59:51 -05:00
|
|
|
possibleHashFunctions = ["Fletcher-8"];
|
2018-05-17 15:11:34 +00:00
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
possibleHashFunctions = [
|
|
|
|
"BSD checksum",
|
|
|
|
"CRC-16",
|
|
|
|
"SYSV checksum",
|
2024-02-24 22:59:51 -05:00
|
|
|
"Fletcher-16",
|
2018-05-17 15:11:34 +00:00
|
|
|
];
|
|
|
|
break;
|
|
|
|
case 32:
|
2024-02-24 22:59:51 -05:00
|
|
|
possibleHashFunctions = ["CRC-32", "Fletcher-32", "Adler-32"];
|
2018-05-17 15:11:34 +00:00
|
|
|
break;
|
|
|
|
case 64:
|
2024-02-24 22:59:51 -05:00
|
|
|
possibleHashFunctions = ["CRC-64", "RIPEMD-64", "SipHash"];
|
2018-05-17 15:11:34 +00:00
|
|
|
break;
|
|
|
|
case 128:
|
|
|
|
possibleHashFunctions = [
|
|
|
|
"MD5",
|
|
|
|
"MD4",
|
|
|
|
"MD2",
|
|
|
|
"HAVAL-128",
|
|
|
|
"RIPEMD-128",
|
|
|
|
"Snefru",
|
|
|
|
"Tiger-128",
|
|
|
|
];
|
|
|
|
break;
|
|
|
|
case 160:
|
|
|
|
possibleHashFunctions = [
|
|
|
|
"SHA-1",
|
|
|
|
"SHA-0",
|
|
|
|
"FSB-160",
|
|
|
|
"HAS-160",
|
|
|
|
"HAVAL-160",
|
|
|
|
"RIPEMD-160",
|
|
|
|
"Tiger-160",
|
|
|
|
];
|
|
|
|
break;
|
|
|
|
case 192:
|
2024-02-24 22:59:51 -05:00
|
|
|
possibleHashFunctions = ["Tiger", "HAVAL-192"];
|
2018-05-17 15:11:34 +00:00
|
|
|
break;
|
|
|
|
case 224:
|
|
|
|
possibleHashFunctions = [
|
|
|
|
"SHA-224",
|
|
|
|
"SHA3-224",
|
|
|
|
"ECOH-224",
|
|
|
|
"FSB-224",
|
|
|
|
"HAVAL-224",
|
|
|
|
];
|
|
|
|
break;
|
|
|
|
case 256:
|
|
|
|
possibleHashFunctions = [
|
|
|
|
"SHA-256",
|
|
|
|
"SHA3-256",
|
|
|
|
"BLAKE-256",
|
|
|
|
"ECOH-256",
|
|
|
|
"FSB-256",
|
|
|
|
"GOST",
|
|
|
|
"Grøstl-256",
|
|
|
|
"HAVAL-256",
|
|
|
|
"PANAMA",
|
|
|
|
"RIPEMD-256",
|
|
|
|
"Snefru",
|
|
|
|
];
|
|
|
|
break;
|
|
|
|
case 320:
|
2024-02-24 22:59:51 -05:00
|
|
|
possibleHashFunctions = ["RIPEMD-320"];
|
2018-05-17 15:11:34 +00:00
|
|
|
break;
|
|
|
|
case 384:
|
|
|
|
possibleHashFunctions = [
|
|
|
|
"SHA-384",
|
|
|
|
"SHA3-384",
|
|
|
|
"ECOH-384",
|
|
|
|
"FSB-384",
|
|
|
|
];
|
|
|
|
break;
|
|
|
|
case 512:
|
|
|
|
possibleHashFunctions = [
|
|
|
|
"SHA-512",
|
|
|
|
"SHA3-512",
|
|
|
|
"BLAKE-512",
|
|
|
|
"ECOH-512",
|
|
|
|
"FSB-512",
|
|
|
|
"Grøstl-512",
|
|
|
|
"JH",
|
|
|
|
"MD6",
|
|
|
|
"Spectral Hash",
|
|
|
|
"SWIFFT",
|
|
|
|
"Whirlpool",
|
|
|
|
];
|
|
|
|
break;
|
|
|
|
case 1024:
|
2024-02-24 22:59:51 -05:00
|
|
|
possibleHashFunctions = ["Fowler-Noll-Vo"];
|
2018-05-17 15:11:34 +00:00
|
|
|
break;
|
|
|
|
default:
|
2024-02-24 22:59:51 -05:00
|
|
|
possibleHashFunctions = ["Unknown"];
|
2018-05-17 15:11:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return output + possibleHashFunctions.join("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default AnalyseHash;
|