Hash.js 10 KB

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