|
@@ -1,6 +1,7 @@
|
|
|
import Utils from "../Utils.js";
|
|
|
-import CryptoJS from "crypto-js";
|
|
|
import CryptoApi from "crypto-api";
|
|
|
+import MD6 from "node-md6";
|
|
|
+import * as SHA3 from "js-sha3";
|
|
|
import Checksum from "./Checksum.js";
|
|
|
|
|
|
|
|
@@ -23,7 +24,7 @@ const Hash = {
|
|
|
* @returns {string}
|
|
|
*/
|
|
|
runMD2: function (input, args) {
|
|
|
- return Utils.toHexFast(CryptoApi.hash("md2", input, {}));
|
|
|
+ return CryptoApi.hash("md2", input, {}).stringify("hex");
|
|
|
},
|
|
|
|
|
|
|
|
@@ -35,7 +36,7 @@ const Hash = {
|
|
|
* @returns {string}
|
|
|
*/
|
|
|
runMD4: function (input, args) {
|
|
|
- return Utils.toHexFast(CryptoApi.hash("md4", input, {}));
|
|
|
+ return CryptoApi.hash("md4", input, {}).stringify("hex");
|
|
|
},
|
|
|
|
|
|
|
|
@@ -47,85 +48,158 @@ const Hash = {
|
|
|
* @returns {string}
|
|
|
*/
|
|
|
runMD5: function (input, args) {
|
|
|
- input = CryptoJS.enc.Latin1.parse(input); // Cast to WordArray
|
|
|
- return CryptoJS.MD5(input).toString(CryptoJS.enc.Hex);
|
|
|
+ return CryptoApi.hash("md5", input, {}).stringify("hex");
|
|
|
},
|
|
|
|
|
|
|
|
|
/**
|
|
|
- * SHA0 operation.
|
|
|
+ * @constant
|
|
|
+ * @default
|
|
|
+ */
|
|
|
+ MD6_SIZE: 256,
|
|
|
+ /**
|
|
|
+ * @constant
|
|
|
+ * @default
|
|
|
+ */
|
|
|
+ MD6_LEVELS: 64,
|
|
|
+
|
|
|
+ /**
|
|
|
+ * MD6 operation.
|
|
|
*
|
|
|
* @param {string} input
|
|
|
* @param {Object[]} args
|
|
|
* @returns {string}
|
|
|
*/
|
|
|
- runSHA0: function (input, args) {
|
|
|
- return Utils.toHexFast(CryptoApi.hash("sha0", input, {}));
|
|
|
+ runMD6: function (input, args) {
|
|
|
+ const size = args[0],
|
|
|
+ levels = args[1],
|
|
|
+ key = args[2];
|
|
|
+
|
|
|
+ if (size < 0 || size > 512)
|
|
|
+ return "Size must be between 0 and 512";
|
|
|
+ if (levels < 0)
|
|
|
+ return "Levels must be greater than 0";
|
|
|
+
|
|
|
+ return MD6.getHashOfText(input, size, key, levels);
|
|
|
},
|
|
|
|
|
|
|
|
|
/**
|
|
|
- * SHA1 operation.
|
|
|
+ * SHA0 operation.
|
|
|
*
|
|
|
* @param {string} input
|
|
|
* @param {Object[]} args
|
|
|
* @returns {string}
|
|
|
*/
|
|
|
- runSHA1: function (input, args) {
|
|
|
- input = CryptoJS.enc.Latin1.parse(input);
|
|
|
- return CryptoJS.SHA1(input).toString(CryptoJS.enc.Hex);
|
|
|
+ runSHA0: function (input, args) {
|
|
|
+ return CryptoApi.hash("sha0", input, {}).stringify("hex");
|
|
|
},
|
|
|
|
|
|
|
|
|
/**
|
|
|
- * SHA224 operation.
|
|
|
+ * SHA1 operation.
|
|
|
*
|
|
|
* @param {string} input
|
|
|
* @param {Object[]} args
|
|
|
* @returns {string}
|
|
|
*/
|
|
|
- runSHA224: function (input, args) {
|
|
|
- input = CryptoJS.enc.Latin1.parse(input);
|
|
|
- return CryptoJS.SHA224(input).toString(CryptoJS.enc.Hex);
|
|
|
+ runSHA1: function (input, args) {
|
|
|
+ return CryptoApi.hash("sha1", input, {}).stringify("hex");
|
|
|
},
|
|
|
|
|
|
|
|
|
/**
|
|
|
- * SHA256 operation.
|
|
|
+ * @constant
|
|
|
+ * @default
|
|
|
+ */
|
|
|
+ SHA2_SIZE: ["512", "256", "384", "224", "512/256", "512/224"],
|
|
|
+
|
|
|
+ /**
|
|
|
+ * SHA2 operation.
|
|
|
*
|
|
|
* @param {string} input
|
|
|
* @param {Object[]} args
|
|
|
* @returns {string}
|
|
|
*/
|
|
|
- runSHA256: function (input, args) {
|
|
|
- input = CryptoJS.enc.Latin1.parse(input);
|
|
|
- return CryptoJS.SHA256(input).toString(CryptoJS.enc.Hex);
|
|
|
+ runSHA2: function (input, args) {
|
|
|
+ const size = args[0];
|
|
|
+ return CryptoApi.hash("sha" + size, input, {}).stringify("hex");
|
|
|
},
|
|
|
|
|
|
|
|
|
/**
|
|
|
- * SHA384 operation.
|
|
|
+ * @constant
|
|
|
+ * @default
|
|
|
+ */
|
|
|
+ SHA3_SIZE: ["512", "384", "256", "224"],
|
|
|
+
|
|
|
+ /**
|
|
|
+ * SHA3 operation.
|
|
|
*
|
|
|
* @param {string} input
|
|
|
* @param {Object[]} args
|
|
|
* @returns {string}
|
|
|
*/
|
|
|
- runSHA384: function (input, args) {
|
|
|
- input = CryptoJS.enc.Latin1.parse(input);
|
|
|
- return CryptoJS.SHA384(input).toString(CryptoJS.enc.Hex);
|
|
|
+ runSHA3: function (input, args) {
|
|
|
+ const size = parseInt(args[0], 10);
|
|
|
+ let algo;
|
|
|
+
|
|
|
+ switch (size) {
|
|
|
+ case 224:
|
|
|
+ algo = SHA3.sha3_224;
|
|
|
+ break;
|
|
|
+ case 384:
|
|
|
+ algo = SHA3.sha3_384;
|
|
|
+ break;
|
|
|
+ case 256:
|
|
|
+ algo = SHA3.sha3_256;
|
|
|
+ break;
|
|
|
+ case 512:
|
|
|
+ algo = SHA3.sha3_512;
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ return "Invalid size";
|
|
|
+ }
|
|
|
+
|
|
|
+ return algo(input);
|
|
|
},
|
|
|
|
|
|
|
|
|
/**
|
|
|
- * SHA512 operation.
|
|
|
+ * @constant
|
|
|
+ * @default
|
|
|
+ */
|
|
|
+ KECCAK_SIZE: ["512", "384", "256", "224"],
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Keccak operation.
|
|
|
*
|
|
|
* @param {string} input
|
|
|
* @param {Object[]} args
|
|
|
* @returns {string}
|
|
|
*/
|
|
|
- runSHA512: function (input, args) {
|
|
|
- input = CryptoJS.enc.Latin1.parse(input);
|
|
|
- return CryptoJS.SHA512(input).toString(CryptoJS.enc.Hex);
|
|
|
+ runKeccak: function (input, args) {
|
|
|
+ const size = parseInt(args[0], 10);
|
|
|
+ let algo;
|
|
|
+
|
|
|
+ switch (size) {
|
|
|
+ case 224:
|
|
|
+ algo = SHA3.keccak224;
|
|
|
+ break;
|
|
|
+ case 384:
|
|
|
+ algo = SHA3.keccak384;
|
|
|
+ break;
|
|
|
+ case 256:
|
|
|
+ algo = SHA3.keccak256;
|
|
|
+ break;
|
|
|
+ case 512:
|
|
|
+ algo = SHA3.keccak512;
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ return "Invalid size";
|
|
|
+ }
|
|
|
+
|
|
|
+ return algo(input);
|
|
|
},
|
|
|
|
|
|
|
|
@@ -133,35 +207,59 @@ const Hash = {
|
|
|
* @constant
|
|
|
* @default
|
|
|
*/
|
|
|
- SHA3_LENGTH: ["512", "384", "256", "224"],
|
|
|
+ SHAKE_CAPACITY: ["256", "128"],
|
|
|
+ /**
|
|
|
+ * @constant
|
|
|
+ * @default
|
|
|
+ */
|
|
|
+ SHAKE_SIZE: 512,
|
|
|
|
|
|
/**
|
|
|
- * SHA3 operation.
|
|
|
+ * Shake operation.
|
|
|
*
|
|
|
* @param {string} input
|
|
|
* @param {Object[]} args
|
|
|
* @returns {string}
|
|
|
*/
|
|
|
- runSHA3: function (input, args) {
|
|
|
- input = CryptoJS.enc.Latin1.parse(input);
|
|
|
- let sha3Length = args[0],
|
|
|
- options = {
|
|
|
- outputLength: parseInt(sha3Length, 10)
|
|
|
- };
|
|
|
- return CryptoJS.SHA3(input, options).toString(CryptoJS.enc.Hex);
|
|
|
+ runShake: function (input, args) {
|
|
|
+ const capacity = parseInt(args[0], 10),
|
|
|
+ size = args[1];
|
|
|
+ let algo;
|
|
|
+
|
|
|
+ if (size < 0)
|
|
|
+ return "Size must be greater than 0";
|
|
|
+
|
|
|
+ switch (capacity) {
|
|
|
+ case 128:
|
|
|
+ algo = SHA3.shake128;
|
|
|
+ break;
|
|
|
+ case 256:
|
|
|
+ algo = SHA3.shake256;
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ return "Invalid size";
|
|
|
+ }
|
|
|
+
|
|
|
+ return algo(input, size);
|
|
|
},
|
|
|
|
|
|
|
|
|
/**
|
|
|
- * RIPEMD-160 operation.
|
|
|
+ * @constant
|
|
|
+ * @default
|
|
|
+ */
|
|
|
+ RIPEMD_SIZE: ["320", "256", "160", "128"],
|
|
|
+
|
|
|
+ /**
|
|
|
+ * RIPEMD operation.
|
|
|
*
|
|
|
* @param {string} input
|
|
|
* @param {Object[]} args
|
|
|
* @returns {string}
|
|
|
*/
|
|
|
- runRIPEMD160: function (input, args) {
|
|
|
- input = CryptoJS.enc.Latin1.parse(input);
|
|
|
- return CryptoJS.RIPEMD160(input).toString(CryptoJS.enc.Hex);
|
|
|
+ runRIPEMD: function (input, args) {
|
|
|
+ const size = args[0];
|
|
|
+ return CryptoApi.hash("ripemd" + size, input, {}).stringify("hex");
|
|
|
},
|
|
|
|
|
|
|
|
@@ -169,7 +267,23 @@ const Hash = {
|
|
|
* @constant
|
|
|
* @default
|
|
|
*/
|
|
|
- HMAC_FUNCTIONS: ["MD5", "SHA1", "SHA224", "SHA256", "SHA384", "SHA512", "SHA3", "RIPEMD-160"],
|
|
|
+ HMAC_FUNCTIONS: [
|
|
|
+ "MD2",
|
|
|
+ "MD4",
|
|
|
+ "MD5",
|
|
|
+ "SHA0",
|
|
|
+ "SHA1",
|
|
|
+ "SHA224",
|
|
|
+ "SHA256",
|
|
|
+ "SHA384",
|
|
|
+ "SHA512",
|
|
|
+ "SHA512/224",
|
|
|
+ "SHA512/256",
|
|
|
+ "RIPEMD128",
|
|
|
+ "RIPEMD160",
|
|
|
+ "RIPEMD256",
|
|
|
+ "RIPEMD320",
|
|
|
+ ],
|
|
|
|
|
|
/**
|
|
|
* HMAC operation.
|
|
@@ -179,19 +293,12 @@ const Hash = {
|
|
|
* @returns {string}
|
|
|
*/
|
|
|
runHMAC: function (input, args) {
|
|
|
- const hashFunc = args[1];
|
|
|
- input = CryptoJS.enc.Latin1.parse(input);
|
|
|
- const execute = {
|
|
|
- "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]),
|
|
|
- };
|
|
|
- return execute[hashFunc].toString(CryptoJS.enc.Hex);
|
|
|
+ const password = args[0],
|
|
|
+ hashFunc = args[1].toLowerCase(),
|
|
|
+ hmac = CryptoApi.mac("hmac", password, hashFunc, {});
|
|
|
+
|
|
|
+ hmac.update(input);
|
|
|
+ return hmac.finalize().stringify("hex");
|
|
|
},
|
|
|
|
|
|
|
|
@@ -207,24 +314,35 @@ const Hash = {
|
|
|
output = "MD2: " + Hash.runMD2(input, []) +
|
|
|
"\nMD4: " + Hash.runMD4(input, []) +
|
|
|
"\nMD5: " + Hash.runMD5(input, []) +
|
|
|
+ "\nMD6: " + Hash.runMD6(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, []) +
|
|
|
+ "\nSHA2 224: " + Hash.runSHA2(input, ["224"]) +
|
|
|
+ "\nSHA2 256: " + Hash.runSHA2(input, ["256"]) +
|
|
|
+ "\nSHA2 384: " + Hash.runSHA2(input, ["384"]) +
|
|
|
+ "\nSHA2 512: " + Hash.runSHA2(input, ["512"]) +
|
|
|
"\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, []) +
|
|
|
+ "\nKeccak 224: " + Hash.runKeccak(input, ["224"]) +
|
|
|
+ "\nKeccak 256: " + Hash.runKeccak(input, ["256"]) +
|
|
|
+ "\nKeccak 384: " + Hash.runKeccak(input, ["384"]) +
|
|
|
+ "\nKeccak 512: " + Hash.runKeccak(input, ["512"]) +
|
|
|
+ "\nShake 128: " + Hash.runShake(input, ["128", 256]) +
|
|
|
+ "\nShake 256: " + Hash.runShake(input, ["256", 512]) +
|
|
|
+ "\nRIPEMD-128: " + Hash.runRIPEMD(input, ["128"]) +
|
|
|
+ "\nRIPEMD-160: " + Hash.runRIPEMD(input, ["160"]) +
|
|
|
+ "\nRIPEMD-256: " + Hash.runRIPEMD(input, ["256"]) +
|
|
|
+ "\nRIPEMD-320: " + Hash.runRIPEMD(input, ["320"]) +
|
|
|
"\n\nChecksums:" +
|
|
|
"\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, []);
|
|
|
+ "\nCRC-16: " + Checksum.runCRC16(input, []) +
|
|
|
+ "\nCRC-32: " + Checksum.runCRC32(input, []);
|
|
|
|
|
|
return output;
|
|
|
},
|