2017-03-23 17:52:20 +00:00
|
|
|
import Utils from "../Utils.js";
|
|
|
|
import CryptoJS from "crypto-js";
|
|
|
|
import CryptoApi from "crypto-api";
|
|
|
|
import Checksum from "./Checksum.js";
|
2017-03-06 12:45:51 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Hashing operations.
|
|
|
|
*
|
|
|
|
* @author n1474335 [n1474335@gmail.com]
|
|
|
|
* @copyright Crown Copyright 2016
|
|
|
|
* @license Apache-2.0
|
|
|
|
*
|
|
|
|
* @namespace
|
|
|
|
*/
|
2017-03-23 17:52:20 +00:00
|
|
|
const Hash = {
|
2017-01-16 16:40:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* MD2 operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runMD2: function (input, args) {
|
|
|
|
return Utils.toHexFast(CryptoApi.hash("md2", input, {}));
|
2017-01-16 16:40:43 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MD4 operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runMD4: function (input, args) {
|
|
|
|
return Utils.toHexFast(CryptoApi.hash("md4", input, {}));
|
2017-01-16 16:40:43 +00:00
|
|
|
},
|
|
|
|
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* MD5 operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runMD5: function (input, args) {
|
2016-11-28 10:42:58 +00:00
|
|
|
input = CryptoJS.enc.Latin1.parse(input); // Cast to WordArray
|
|
|
|
return CryptoJS.MD5(input).toString(CryptoJS.enc.Hex);
|
|
|
|
},
|
2017-01-16 16:40:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SHA0 operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runSHA0: function (input, args) {
|
|
|
|
return Utils.toHexFast(CryptoApi.hash("sha0", input, {}));
|
2017-01-16 16:40:43 +00:00
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* SHA1 operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runSHA1: function (input, args) {
|
2016-11-28 10:42:58 +00:00
|
|
|
input = CryptoJS.enc.Latin1.parse(input);
|
|
|
|
return CryptoJS.SHA1(input).toString(CryptoJS.enc.Hex);
|
|
|
|
},
|
|
|
|
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* SHA224 operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runSHA224: function (input, args) {
|
2016-11-28 10:42:58 +00:00
|
|
|
input = CryptoJS.enc.Latin1.parse(input);
|
|
|
|
return CryptoJS.SHA224(input).toString(CryptoJS.enc.Hex);
|
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* SHA256 operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runSHA256: function (input, args) {
|
2016-11-28 10:42:58 +00:00
|
|
|
input = CryptoJS.enc.Latin1.parse(input);
|
|
|
|
return CryptoJS.SHA256(input).toString(CryptoJS.enc.Hex);
|
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* SHA384 operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runSHA384: function (input, args) {
|
2016-11-28 10:42:58 +00:00
|
|
|
input = CryptoJS.enc.Latin1.parse(input);
|
|
|
|
return CryptoJS.SHA384(input).toString(CryptoJS.enc.Hex);
|
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* SHA512 operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runSHA512: function (input, args) {
|
2016-11-28 10:42:58 +00:00
|
|
|
input = CryptoJS.enc.Latin1.parse(input);
|
|
|
|
return CryptoJS.SHA512(input).toString(CryptoJS.enc.Hex);
|
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
SHA3_LENGTH: ["512", "384", "256", "224"],
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* SHA3 operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runSHA3: function (input, args) {
|
2016-11-28 10:42:58 +00:00
|
|
|
input = CryptoJS.enc.Latin1.parse(input);
|
2017-04-13 18:08:50 +01:00
|
|
|
let sha3Length = args[0],
|
2016-11-28 10:42:58 +00:00
|
|
|
options = {
|
2017-01-31 18:24:56 +00:00
|
|
|
outputLength: parseInt(sha3Length, 10)
|
2016-11-28 10:42:58 +00:00
|
|
|
};
|
|
|
|
return CryptoJS.SHA3(input, options).toString(CryptoJS.enc.Hex);
|
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* RIPEMD-160 operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runRIPEMD160: function (input, args) {
|
2016-11-28 10:42:58 +00:00
|
|
|
input = CryptoJS.enc.Latin1.parse(input);
|
|
|
|
return CryptoJS.RIPEMD160(input).toString(CryptoJS.enc.Hex);
|
|
|
|
},
|
|
|
|
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
HMAC_FUNCTIONS: ["MD5", "SHA1", "SHA224", "SHA256", "SHA384", "SHA512", "SHA3", "RIPEMD-160"],
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* HMAC operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runHMAC: function (input, args) {
|
2017-04-13 18:08:50 +01:00
|
|
|
const hashFunc = args[1];
|
2016-11-28 10:42:58 +00:00
|
|
|
input = CryptoJS.enc.Latin1.parse(input);
|
2017-04-13 18:08:50 +01:00
|
|
|
const execute = {
|
2016-11-28 10:42:58 +00:00
|
|
|
"MD5": CryptoJS.HmacMD5(input, args[0]),
|
|
|
|
"SHA1": CryptoJS.HmacSHA1(input, args[0]),
|
|
|
|
"SHA224": CryptoJS.HmacSHA224(input, args[0]),
|
|
|
|
"SHA256": CryptoJS.HmacSHA256(input, args[0]),
|
|
|
|
"SHA384": CryptoJS.HmacSHA384(input, args[0]),
|
|
|
|
"SHA512": CryptoJS.HmacSHA512(input, args[0]),
|
|
|
|
"SHA3": CryptoJS.HmacSHA3(input, args[0]),
|
|
|
|
"RIPEMD-160": CryptoJS.HmacRIPEMD160(input, args[0]),
|
|
|
|
};
|
2017-01-31 18:24:56 +00:00
|
|
|
return execute[hashFunc].toString(CryptoJS.enc.Hex);
|
2016-11-28 10:42:58 +00:00
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Generate all hashes operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runAll: function (input, args) {
|
2017-04-13 18:08:50 +01:00
|
|
|
let byteArray = Utils.strToByteArray(input),
|
2017-01-31 18:24:56 +00:00
|
|
|
output = "MD2: " + Hash.runMD2(input, []) +
|
|
|
|
"\nMD4: " + Hash.runMD4(input, []) +
|
|
|
|
"\nMD5: " + Hash.runMD5(input, []) +
|
|
|
|
"\nSHA0: " + Hash.runSHA0(input, []) +
|
|
|
|
"\nSHA1: " + Hash.runSHA1(input, []) +
|
|
|
|
"\nSHA2 224: " + Hash.runSHA224(input, []) +
|
|
|
|
"\nSHA2 256: " + Hash.runSHA256(input, []) +
|
|
|
|
"\nSHA2 384: " + Hash.runSHA384(input, []) +
|
|
|
|
"\nSHA2 512: " + Hash.runSHA512(input, []) +
|
|
|
|
"\nSHA3 224: " + Hash.runSHA3(input, ["224"]) +
|
|
|
|
"\nSHA3 256: " + Hash.runSHA3(input, ["256"]) +
|
|
|
|
"\nSHA3 384: " + Hash.runSHA3(input, ["384"]) +
|
|
|
|
"\nSHA3 512: " + Hash.runSHA3(input, ["512"]) +
|
|
|
|
"\nRIPEMD-160: " + Hash.runRIPEMD160(input, []) +
|
2016-11-28 10:42:58 +00:00
|
|
|
"\n\nChecksums:" +
|
2017-01-31 18:24:56 +00:00
|
|
|
"\nFletcher-8: " + Checksum.runFletcher8(byteArray, []) +
|
|
|
|
"\nFletcher-16: " + Checksum.runFletcher16(byteArray, []) +
|
|
|
|
"\nFletcher-32: " + Checksum.runFletcher32(byteArray, []) +
|
|
|
|
"\nFletcher-64: " + Checksum.runFletcher64(byteArray, []) +
|
|
|
|
"\nAdler-32: " + Checksum.runAdler32(byteArray, []) +
|
|
|
|
"\nCRC-32: " + Checksum.runCRC32(byteArray, []);
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
return output;
|
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Analyse hash operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runAnalyse: function(input, args) {
|
2016-11-28 10:42:58 +00:00
|
|
|
input = input.replace(/\s/g, "");
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-04-13 18:08:50 +01:00
|
|
|
let output = "",
|
2017-01-31 18:24:56 +00:00
|
|
|
byteLength = input.length / 2,
|
|
|
|
bitLength = byteLength * 8,
|
|
|
|
possibleHashFunctions = [];
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
if (!/^[a-f0-9]+$/i.test(input)) {
|
|
|
|
return "Invalid hash";
|
|
|
|
}
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
output += "Hash length: " + input.length + "\n" +
|
2017-01-31 18:24:56 +00:00
|
|
|
"Byte length: " + byteLength + "\n" +
|
|
|
|
"Bit length: " + bitLength + "\n\n" +
|
2016-11-28 10:42:58 +00:00
|
|
|
"Based on the length, this hash could have been generated by one of the following hashing functions:\n";
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-01-31 18:24:56 +00:00
|
|
|
switch (bitLength) {
|
2016-11-28 10:42:58 +00:00
|
|
|
case 4:
|
2017-01-31 18:24:56 +00:00
|
|
|
possibleHashFunctions = [
|
2016-11-28 10:42:58 +00:00
|
|
|
"Fletcher-4",
|
|
|
|
"Luhn algorithm",
|
|
|
|
"Verhoeff algorithm",
|
|
|
|
];
|
|
|
|
break;
|
|
|
|
case 8:
|
2017-01-31 18:24:56 +00:00
|
|
|
possibleHashFunctions = [
|
2016-11-28 10:42:58 +00:00
|
|
|
"Fletcher-8",
|
|
|
|
];
|
|
|
|
break;
|
|
|
|
case 16:
|
2017-01-31 18:24:56 +00:00
|
|
|
possibleHashFunctions = [
|
2016-11-28 10:42:58 +00:00
|
|
|
"BSD checksum",
|
|
|
|
"CRC-16",
|
|
|
|
"SYSV checksum",
|
|
|
|
"Fletcher-16"
|
|
|
|
];
|
|
|
|
break;
|
|
|
|
case 32:
|
2017-01-31 18:24:56 +00:00
|
|
|
possibleHashFunctions = [
|
2016-11-28 10:42:58 +00:00
|
|
|
"CRC-32",
|
|
|
|
"Fletcher-32",
|
|
|
|
"Adler-32",
|
|
|
|
];
|
|
|
|
break;
|
|
|
|
case 64:
|
2017-01-31 18:24:56 +00:00
|
|
|
possibleHashFunctions = [
|
2016-11-28 10:42:58 +00:00
|
|
|
"CRC-64",
|
|
|
|
"RIPEMD-64",
|
|
|
|
"SipHash",
|
|
|
|
];
|
|
|
|
break;
|
|
|
|
case 128:
|
2017-01-31 18:24:56 +00:00
|
|
|
possibleHashFunctions = [
|
2016-11-28 10:42:58 +00:00
|
|
|
"MD5",
|
|
|
|
"MD4",
|
|
|
|
"MD2",
|
|
|
|
"HAVAL-128",
|
|
|
|
"RIPEMD-128",
|
|
|
|
"Snefru",
|
|
|
|
"Tiger-128",
|
|
|
|
];
|
|
|
|
break;
|
|
|
|
case 160:
|
2017-01-31 18:24:56 +00:00
|
|
|
possibleHashFunctions = [
|
2016-11-28 10:42:58 +00:00
|
|
|
"SHA-1",
|
|
|
|
"SHA-0",
|
|
|
|
"FSB-160",
|
|
|
|
"HAS-160",
|
|
|
|
"HAVAL-160",
|
|
|
|
"RIPEMD-160",
|
|
|
|
"Tiger-160",
|
|
|
|
];
|
|
|
|
break;
|
|
|
|
case 192:
|
2017-01-31 18:24:56 +00:00
|
|
|
possibleHashFunctions = [
|
2016-11-28 10:42:58 +00:00
|
|
|
"Tiger",
|
|
|
|
"HAVAL-192",
|
|
|
|
];
|
|
|
|
break;
|
|
|
|
case 224:
|
2017-01-31 18:24:56 +00:00
|
|
|
possibleHashFunctions = [
|
2016-11-28 10:42:58 +00:00
|
|
|
"SHA-224",
|
|
|
|
"SHA3-224",
|
|
|
|
"ECOH-224",
|
|
|
|
"FSB-224",
|
|
|
|
"HAVAL-224",
|
|
|
|
];
|
|
|
|
break;
|
|
|
|
case 256:
|
2017-01-31 18:24:56 +00:00
|
|
|
possibleHashFunctions = [
|
2016-11-28 10:42:58 +00:00
|
|
|
"SHA-256",
|
|
|
|
"SHA3-256",
|
|
|
|
"BLAKE-256",
|
|
|
|
"ECOH-256",
|
|
|
|
"FSB-256",
|
|
|
|
"GOST",
|
|
|
|
"Grøstl-256",
|
|
|
|
"HAVAL-256",
|
|
|
|
"PANAMA",
|
|
|
|
"RIPEMD-256",
|
|
|
|
"Snefru",
|
|
|
|
];
|
|
|
|
break;
|
|
|
|
case 320:
|
2017-01-31 18:24:56 +00:00
|
|
|
possibleHashFunctions = [
|
2016-11-28 10:42:58 +00:00
|
|
|
"RIPEMD-320",
|
|
|
|
];
|
|
|
|
break;
|
|
|
|
case 384:
|
2017-01-31 18:24:56 +00:00
|
|
|
possibleHashFunctions = [
|
2016-11-28 10:42:58 +00:00
|
|
|
"SHA-384",
|
|
|
|
"SHA3-384",
|
|
|
|
"ECOH-384",
|
|
|
|
"FSB-384",
|
|
|
|
];
|
|
|
|
break;
|
|
|
|
case 512:
|
2017-01-31 18:24:56 +00:00
|
|
|
possibleHashFunctions = [
|
2016-11-28 10:42:58 +00:00
|
|
|
"SHA-512",
|
|
|
|
"SHA3-512",
|
|
|
|
"BLAKE-512",
|
|
|
|
"ECOH-512",
|
|
|
|
"FSB-512",
|
|
|
|
"Grøstl-512",
|
|
|
|
"JH",
|
|
|
|
"MD6",
|
|
|
|
"Spectral Hash",
|
|
|
|
"SWIFFT",
|
|
|
|
"Whirlpool",
|
|
|
|
];
|
|
|
|
break;
|
|
|
|
case 1024:
|
2017-01-31 18:24:56 +00:00
|
|
|
possibleHashFunctions = [
|
2016-11-28 10:42:58 +00:00
|
|
|
"Fowler-Noll-Vo",
|
|
|
|
];
|
|
|
|
break;
|
|
|
|
default:
|
2017-01-31 18:24:56 +00:00
|
|
|
possibleHashFunctions = [
|
2016-11-28 10:42:58 +00:00
|
|
|
"Unknown"
|
|
|
|
];
|
|
|
|
break;
|
|
|
|
}
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-01-31 18:24:56 +00:00
|
|
|
return output + possibleHashFunctions.join("\n");
|
2016-11-28 10:42:58 +00:00
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
};
|
2017-03-23 17:52:20 +00:00
|
|
|
|
|
|
|
export default Hash;
|