Hash.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. import Utils from "../Utils.js";
  2. import CryptoJS from "crypto-js";
  3. import CryptoApi from "crypto-api";
  4. import Checksum from "./Checksum.js";
  5. /**
  6. * Hashing operations.
  7. *
  8. * @author n1474335 [n1474335@gmail.com]
  9. * @copyright Crown Copyright 2016
  10. * @license Apache-2.0
  11. *
  12. * @namespace
  13. */
  14. const Hash = {
  15. /**
  16. * MD2 operation.
  17. *
  18. * @param {string} input
  19. * @param {Object[]} args
  20. * @returns {string}
  21. */
  22. runMD2: function (input, args) {
  23. return Utils.toHexFast(CryptoApi.hash("md2", input, {}));
  24. },
  25. /**
  26. * MD4 operation.
  27. *
  28. * @param {string} input
  29. * @param {Object[]} args
  30. * @returns {string}
  31. */
  32. runMD4: function (input, args) {
  33. return Utils.toHexFast(CryptoApi.hash("md4", input, {}));
  34. },
  35. /**
  36. * MD5 operation.
  37. *
  38. * @param {string} input
  39. * @param {Object[]} args
  40. * @returns {string}
  41. */
  42. runMD5: function (input, args) {
  43. input = CryptoJS.enc.Latin1.parse(input); // Cast to WordArray
  44. return CryptoJS.MD5(input).toString(CryptoJS.enc.Hex);
  45. },
  46. /**
  47. * SHA0 operation.
  48. *
  49. * @param {string} input
  50. * @param {Object[]} args
  51. * @returns {string}
  52. */
  53. runSHA0: function (input, args) {
  54. return Utils.toHexFast(CryptoApi.hash("sha0", input, {}));
  55. },
  56. /**
  57. * SHA1 operation.
  58. *
  59. * @param {string} input
  60. * @param {Object[]} args
  61. * @returns {string}
  62. */
  63. runSHA1: function (input, args) {
  64. input = CryptoJS.enc.Latin1.parse(input);
  65. return CryptoJS.SHA1(input).toString(CryptoJS.enc.Hex);
  66. },
  67. /**
  68. * SHA224 operation.
  69. *
  70. * @param {string} input
  71. * @param {Object[]} args
  72. * @returns {string}
  73. */
  74. runSHA224: function (input, args) {
  75. input = CryptoJS.enc.Latin1.parse(input);
  76. return CryptoJS.SHA224(input).toString(CryptoJS.enc.Hex);
  77. },
  78. /**
  79. * SHA256 operation.
  80. *
  81. * @param {string} input
  82. * @param {Object[]} args
  83. * @returns {string}
  84. */
  85. runSHA256: function (input, args) {
  86. input = CryptoJS.enc.Latin1.parse(input);
  87. return CryptoJS.SHA256(input).toString(CryptoJS.enc.Hex);
  88. },
  89. /**
  90. * SHA384 operation.
  91. *
  92. * @param {string} input
  93. * @param {Object[]} args
  94. * @returns {string}
  95. */
  96. runSHA384: function (input, args) {
  97. input = CryptoJS.enc.Latin1.parse(input);
  98. return CryptoJS.SHA384(input).toString(CryptoJS.enc.Hex);
  99. },
  100. /**
  101. * SHA512 operation.
  102. *
  103. * @param {string} input
  104. * @param {Object[]} args
  105. * @returns {string}
  106. */
  107. runSHA512: function (input, args) {
  108. input = CryptoJS.enc.Latin1.parse(input);
  109. return CryptoJS.SHA512(input).toString(CryptoJS.enc.Hex);
  110. },
  111. /**
  112. * @constant
  113. * @default
  114. */
  115. SHA3_LENGTH: ["512", "384", "256", "224"],
  116. /**
  117. * SHA3 operation.
  118. *
  119. * @param {string} input
  120. * @param {Object[]} args
  121. * @returns {string}
  122. */
  123. runSHA3: function (input, args) {
  124. input = CryptoJS.enc.Latin1.parse(input);
  125. var sha3Length = args[0],
  126. options = {
  127. outputLength: parseInt(sha3Length, 10)
  128. };
  129. return CryptoJS.SHA3(input, options).toString(CryptoJS.enc.Hex);
  130. },
  131. /**
  132. * RIPEMD-160 operation.
  133. *
  134. * @param {string} input
  135. * @param {Object[]} args
  136. * @returns {string}
  137. */
  138. runRIPEMD160: function (input, args) {
  139. input = CryptoJS.enc.Latin1.parse(input);
  140. return CryptoJS.RIPEMD160(input).toString(CryptoJS.enc.Hex);
  141. },
  142. /**
  143. * @constant
  144. * @default
  145. */
  146. HMAC_FUNCTIONS: ["MD5", "SHA1", "SHA224", "SHA256", "SHA384", "SHA512", "SHA3", "RIPEMD-160"],
  147. /**
  148. * HMAC operation.
  149. *
  150. * @param {string} input
  151. * @param {Object[]} args
  152. * @returns {string}
  153. */
  154. runHMAC: function (input, args) {
  155. var hashFunc = args[1];
  156. input = CryptoJS.enc.Latin1.parse(input);
  157. var execute = {
  158. "MD5": CryptoJS.HmacMD5(input, args[0]),
  159. "SHA1": CryptoJS.HmacSHA1(input, args[0]),
  160. "SHA224": CryptoJS.HmacSHA224(input, args[0]),
  161. "SHA256": CryptoJS.HmacSHA256(input, args[0]),
  162. "SHA384": CryptoJS.HmacSHA384(input, args[0]),
  163. "SHA512": CryptoJS.HmacSHA512(input, args[0]),
  164. "SHA3": CryptoJS.HmacSHA3(input, args[0]),
  165. "RIPEMD-160": CryptoJS.HmacRIPEMD160(input, args[0]),
  166. };
  167. return execute[hashFunc].toString(CryptoJS.enc.Hex);
  168. },
  169. /**
  170. * Generate all hashes operation.
  171. *
  172. * @param {string} input
  173. * @param {Object[]} args
  174. * @returns {string}
  175. */
  176. runAll: function (input, args) {
  177. var byteArray = Utils.strToByteArray(input),
  178. output = "MD2: " + Hash.runMD2(input, []) +
  179. "\nMD4: " + Hash.runMD4(input, []) +
  180. "\nMD5: " + Hash.runMD5(input, []) +
  181. "\nSHA0: " + Hash.runSHA0(input, []) +
  182. "\nSHA1: " + Hash.runSHA1(input, []) +
  183. "\nSHA2 224: " + Hash.runSHA224(input, []) +
  184. "\nSHA2 256: " + Hash.runSHA256(input, []) +
  185. "\nSHA2 384: " + Hash.runSHA384(input, []) +
  186. "\nSHA2 512: " + Hash.runSHA512(input, []) +
  187. "\nSHA3 224: " + Hash.runSHA3(input, ["224"]) +
  188. "\nSHA3 256: " + Hash.runSHA3(input, ["256"]) +
  189. "\nSHA3 384: " + Hash.runSHA3(input, ["384"]) +
  190. "\nSHA3 512: " + Hash.runSHA3(input, ["512"]) +
  191. "\nRIPEMD-160: " + Hash.runRIPEMD160(input, []) +
  192. "\n\nChecksums:" +
  193. "\nFletcher-8: " + Checksum.runFletcher8(byteArray, []) +
  194. "\nFletcher-16: " + Checksum.runFletcher16(byteArray, []) +
  195. "\nFletcher-32: " + Checksum.runFletcher32(byteArray, []) +
  196. "\nFletcher-64: " + Checksum.runFletcher64(byteArray, []) +
  197. "\nAdler-32: " + Checksum.runAdler32(byteArray, []) +
  198. "\nCRC-32: " + Checksum.runCRC32(byteArray, []);
  199. return output;
  200. },
  201. /**
  202. * Analyse hash operation.
  203. *
  204. * @param {string} input
  205. * @param {Object[]} args
  206. * @returns {string}
  207. */
  208. runAnalyse: function(input, args) {
  209. input = input.replace(/\s/g, "");
  210. var output = "",
  211. byteLength = input.length / 2,
  212. bitLength = byteLength * 8,
  213. possibleHashFunctions = [];
  214. if (!/^[a-f0-9]+$/i.test(input)) {
  215. return "Invalid hash";
  216. }
  217. output += "Hash length: " + input.length + "\n" +
  218. "Byte length: " + byteLength + "\n" +
  219. "Bit length: " + bitLength + "\n\n" +
  220. "Based on the length, this hash could have been generated by one of the following hashing functions:\n";
  221. switch (bitLength) {
  222. case 4:
  223. possibleHashFunctions = [
  224. "Fletcher-4",
  225. "Luhn algorithm",
  226. "Verhoeff algorithm",
  227. ];
  228. break;
  229. case 8:
  230. possibleHashFunctions = [
  231. "Fletcher-8",
  232. ];
  233. break;
  234. case 16:
  235. possibleHashFunctions = [
  236. "BSD checksum",
  237. "CRC-16",
  238. "SYSV checksum",
  239. "Fletcher-16"
  240. ];
  241. break;
  242. case 32:
  243. possibleHashFunctions = [
  244. "CRC-32",
  245. "Fletcher-32",
  246. "Adler-32",
  247. ];
  248. break;
  249. case 64:
  250. possibleHashFunctions = [
  251. "CRC-64",
  252. "RIPEMD-64",
  253. "SipHash",
  254. ];
  255. break;
  256. case 128:
  257. possibleHashFunctions = [
  258. "MD5",
  259. "MD4",
  260. "MD2",
  261. "HAVAL-128",
  262. "RIPEMD-128",
  263. "Snefru",
  264. "Tiger-128",
  265. ];
  266. break;
  267. case 160:
  268. possibleHashFunctions = [
  269. "SHA-1",
  270. "SHA-0",
  271. "FSB-160",
  272. "HAS-160",
  273. "HAVAL-160",
  274. "RIPEMD-160",
  275. "Tiger-160",
  276. ];
  277. break;
  278. case 192:
  279. possibleHashFunctions = [
  280. "Tiger",
  281. "HAVAL-192",
  282. ];
  283. break;
  284. case 224:
  285. possibleHashFunctions = [
  286. "SHA-224",
  287. "SHA3-224",
  288. "ECOH-224",
  289. "FSB-224",
  290. "HAVAL-224",
  291. ];
  292. break;
  293. case 256:
  294. possibleHashFunctions = [
  295. "SHA-256",
  296. "SHA3-256",
  297. "BLAKE-256",
  298. "ECOH-256",
  299. "FSB-256",
  300. "GOST",
  301. "Grøstl-256",
  302. "HAVAL-256",
  303. "PANAMA",
  304. "RIPEMD-256",
  305. "Snefru",
  306. ];
  307. break;
  308. case 320:
  309. possibleHashFunctions = [
  310. "RIPEMD-320",
  311. ];
  312. break;
  313. case 384:
  314. possibleHashFunctions = [
  315. "SHA-384",
  316. "SHA3-384",
  317. "ECOH-384",
  318. "FSB-384",
  319. ];
  320. break;
  321. case 512:
  322. possibleHashFunctions = [
  323. "SHA-512",
  324. "SHA3-512",
  325. "BLAKE-512",
  326. "ECOH-512",
  327. "FSB-512",
  328. "Grøstl-512",
  329. "JH",
  330. "MD6",
  331. "Spectral Hash",
  332. "SWIFFT",
  333. "Whirlpool",
  334. ];
  335. break;
  336. case 1024:
  337. possibleHashFunctions = [
  338. "Fowler-Noll-Vo",
  339. ];
  340. break;
  341. default:
  342. possibleHashFunctions = [
  343. "Unknown"
  344. ];
  345. break;
  346. }
  347. return output + possibleHashFunctions.join("\n");
  348. },
  349. };
  350. export default Hash;