hashmgr.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /*
  2. ---------------------------------------------------------------------------
  3. Copyright (c) 2003, Dominik Reichl <dominik.reichl@t-online.de>, Germany.
  4. All rights reserved.
  5. Distributed under the terms of the GNU General Public License v2.
  6. This software is provided 'as is' with no explicit or implied warranties
  7. in respect of its properties, including, but not limited to, correctness
  8. and/or fitness for purpose.
  9. ---------------------------------------------------------------------------
  10. */
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include "hashmgr.h"
  14. #include "rehash.h"
  15. CHashManager::CHashManager()
  16. {
  17. m_bFullPath = false;
  18. m_bRecursive = true;
  19. SelectAllAlgorithms();
  20. gosthash_init();
  21. }
  22. CHashManager::~CHashManager()
  23. {
  24. }
  25. void CHashManager::SelectAllAlgorithms(bool bSelect)
  26. {
  27. int i = 0;
  28. for(i = 0; i < RH_MAX_ALGORITHMS; i++) m_bAlgorithm[i] = bSelect;
  29. }
  30. bool CHashManager::SelectAlgorithm(int nAlgorithm, bool bSelect)
  31. {
  32. if(nAlgorithm < 0) return false;
  33. if(nAlgorithm >= RH_MAX_ALGORITHMS) return false;
  34. m_bAlgorithm[nAlgorithm] = bSelect;
  35. return true;
  36. }
  37. bool CHashManager::SetOption(int nOption, bool bState)
  38. {
  39. if(nOption <= 0) return false;
  40. if(nOption >= OPT_MAX_OPTIONS) return false;
  41. if(nOption == OPT_FULLPATH) m_bFullPath = bState;
  42. if(nOption == OPT_RECURSIVE) m_bRecursive = bState;
  43. return true;
  44. }
  45. int CHashManager::HashPath(char *pszBasePath, char *pszFileSpec)
  46. {
  47. recursivesafe long lHandle = 0;
  48. recursivesafe long lSearcherHandle = 0;
  49. recursivesafe finddata fdInfo;
  50. recursivesafe char szFull[RH_MAX_PATH];
  51. recursivesafe char szAll[RH_MAX_PATH];
  52. if(pszBasePath == NULL) return RH_INVALID_PATH;
  53. if(pszFileSpec == NULL) return RH_INVALID_PATH;
  54. fmtPath(pszBasePath);
  55. fmtPath(pszFileSpec);
  56. if(strlen(pszBasePath) == 0) getcwd(pszBasePath, RH_MAX_PATH);
  57. fileonly(pszFileSpec);
  58. strcpy(szAll, pszBasePath);
  59. catdirsep(szAll);
  60. strcat(szAll, SZ_DIR_ALL);
  61. #ifdef RH_DEBUG
  62. printf("Function HashPath: pszBasePath=%s, pszFileSpec=%s", pszBasePath, pszFileSpec);
  63. printf(CPS_NEWLINE);
  64. printf("Function HashPath: szAll=%s", szAll);
  65. printf(CPS_NEWLINE);
  66. #endif
  67. //////////////////////////////////////////////////////////////////////////
  68. // Start directory enumeration code
  69. lSearcherHandle = findfirst(szAll, &fdInfo);
  70. while(1)
  71. {
  72. if(fdInfo.attrib & _A_SUBDIR)
  73. {
  74. if((ispathnav(fdInfo.name) == false) && (m_bRecursive))
  75. {
  76. if(chdir(fdInfo.name) == 0)
  77. {
  78. getcwd(szFull, RH_MAX_PATH);
  79. #ifdef RH_DEBUG
  80. printf("Opening new scan path: %s, filemask: %s", szFull, pszFileSpec);
  81. printf(CPS_NEWLINE);
  82. #endif
  83. HashPath(szFull, pszFileSpec);
  84. chdir(SZ_LEVEL_UP);
  85. }
  86. }
  87. }
  88. if(findnext(lSearcherHandle, &fdInfo) != 0) break;
  89. }
  90. findclose(lSearcherHandle);
  91. lSearcherHandle = 0;
  92. // End directory enumeration code
  93. //////////////////////////////////////////////////////////////////////////
  94. memset(&fdInfo, 0, sizeof(finddata));
  95. lHandle = findfirst(pszFileSpec, &fdInfo);
  96. if(lHandle == EINVAL) return RH_INVALID_PATH;
  97. if(lHandle == ENOENT) return RH_NO_PATTERN_MATCH;
  98. if(lHandle == -1) return RH_CANNOT_OPEN_FILE;
  99. while(1)
  100. {
  101. if(fdInfo.attrib & _A_SUBDIR)
  102. {
  103. // Don't process directories here
  104. }
  105. else
  106. {
  107. if(m_bFullPath)
  108. {
  109. fullpath(szFull, fdInfo.name, RH_MAX_PATH);
  110. HashFile(szFull);
  111. }
  112. else
  113. {
  114. HashFile(fdInfo.name);
  115. }
  116. printf(CPS_NEWLINE);
  117. }
  118. if(findnext(lHandle, &fdInfo) != 0) break;
  119. }
  120. findclose(lHandle);
  121. lHandle = 0;
  122. return RH_SUCCESS;
  123. }
  124. int CHashManager::HashFile(char *pszFile)
  125. {
  126. FILE *fp = NULL;
  127. unsigned char pBuf[SIZE_HASH_BUFFER];
  128. unsigned long uRead = 0;
  129. unsigned char pTemp[256];
  130. char szTemp[RH_MAX_BUFFER];
  131. int i = 0;
  132. printf("File: <");
  133. printf(pszFile);
  134. printf(">");
  135. printf(CPS_NEWLINE);
  136. fp = fopen(pszFile, "rb");
  137. if(fp == NULL) return RH_CANNOT_OPEN_FILE;
  138. if(m_bAlgorithm[HASHID_CRC16]) crc16_init(&m_crc16);
  139. if(m_bAlgorithm[HASHID_CRC16CCITT]) crc16ccitt_init(&m_crc16ccitt);
  140. if(m_bAlgorithm[HASHID_CRC32]) crc32Init(&m_crc32);
  141. if(m_bAlgorithm[HASHID_FCS_16]) fcs16_init(&m_fcs16);
  142. if(m_bAlgorithm[HASHID_FCS_32]) fcs32_init(&m_fcs32);
  143. if(m_bAlgorithm[HASHID_GHASH_32_3] || m_bAlgorithm[HASHID_GHASH_32_5]) m_ghash.Init();
  144. if(m_bAlgorithm[HASHID_GOST]) gosthash_reset(&m_gost);
  145. if(m_bAlgorithm[HASHID_HAVAL]) haval_start(&m_haval);
  146. if(m_bAlgorithm[HASHID_MD2]) m_md2.Init();
  147. if(m_bAlgorithm[HASHID_MD4]) MD4Init(&m_md4);
  148. if(m_bAlgorithm[HASHID_MD5]) MD5Init(&m_md5, 0);
  149. if(m_bAlgorithm[HASHID_SHA1]) sha1_begin(&m_sha1);
  150. if(m_bAlgorithm[HASHID_SHA2_256]) sha256_begin(&m_sha256);
  151. if(m_bAlgorithm[HASHID_SHA2_384]) sha384_begin(&m_sha384);
  152. if(m_bAlgorithm[HASHID_SHA2_512]) sha512_begin(&m_sha512);
  153. if(m_bAlgorithm[HASHID_SIZE_32]) sizehash32_begin(&m_uSizeHash32);
  154. if(m_bAlgorithm[HASHID_TIGER]) tiger_init(&m_tiger);
  155. while(1)
  156. {
  157. uRead = fread(pBuf, 1, SIZE_HASH_BUFFER, fp);
  158. if(uRead != 0)
  159. {
  160. if(m_bAlgorithm[HASHID_CRC16])
  161. crc16_update(&m_crc16, pBuf, uRead);
  162. if(m_bAlgorithm[HASHID_CRC16CCITT])
  163. crc16ccitt_update(&m_crc16ccitt, pBuf, uRead);
  164. if(m_bAlgorithm[HASHID_CRC32])
  165. crc32Update(&m_crc32, pBuf, uRead);
  166. if(m_bAlgorithm[HASHID_FCS_16])
  167. fcs16_update(&m_fcs16, pBuf, uRead);
  168. if(m_bAlgorithm[HASHID_FCS_32])
  169. fcs32_update(&m_fcs32, pBuf, uRead);
  170. if(m_bAlgorithm[HASHID_GHASH_32_3] || m_bAlgorithm[HASHID_GHASH_32_5])
  171. m_ghash.Update(pBuf, uRead);
  172. if(m_bAlgorithm[HASHID_GOST])
  173. gosthash_update(&m_gost, pBuf, uRead);
  174. if(m_bAlgorithm[HASHID_HAVAL])
  175. haval_hash(&m_haval, pBuf, uRead);
  176. if(m_bAlgorithm[HASHID_MD2])
  177. m_md2.Update(pBuf, uRead);
  178. if(m_bAlgorithm[HASHID_MD4])
  179. MD4Update(&m_md4, pBuf, uRead);
  180. if(m_bAlgorithm[HASHID_MD5])
  181. MD5Update(&m_md5, pBuf, uRead);
  182. if(m_bAlgorithm[HASHID_SHA1])
  183. sha1_hash(pBuf, uRead, &m_sha1);
  184. if(m_bAlgorithm[HASHID_SHA2_256])
  185. sha256_hash(pBuf, uRead, &m_sha256);
  186. if(m_bAlgorithm[HASHID_SHA2_384])
  187. sha384_hash(pBuf, uRead, &m_sha384);
  188. if(m_bAlgorithm[HASHID_SHA2_512])
  189. sha512_hash(pBuf, uRead, &m_sha512);
  190. if(m_bAlgorithm[HASHID_SIZE_32])
  191. sizehash32_hash(&m_uSizeHash32, uRead);
  192. if(m_bAlgorithm[HASHID_TIGER])
  193. tiger_process(&m_tiger, pBuf, uRead);
  194. }
  195. if(uRead != SIZE_HASH_BUFFER) break;
  196. }
  197. fclose(fp); fp = NULL;
  198. // SizeHash-32 is the first hash, because it's the simplest one,
  199. // the fastest, and most widely used one. ;-)
  200. if(m_bAlgorithm[HASHID_SIZE_32])
  201. {
  202. sizehash32_end(&m_uSizeHash32);
  203. printf(SZ_SIZEHASH_32);
  204. printf(SZ_HASHPRE);
  205. printf("%08X", m_uSizeHash32);
  206. printf(CPS_NEWLINE);
  207. }
  208. if(m_bAlgorithm[HASHID_CRC16])
  209. {
  210. crc16_final(&m_crc16);
  211. printf(SZ_CRC16);
  212. printf(SZ_HASHPRE);
  213. printf("%04X", m_crc16);
  214. printf(CPS_NEWLINE);
  215. }
  216. if(m_bAlgorithm[HASHID_CRC16CCITT])
  217. {
  218. crc16ccitt_final(&m_crc16ccitt);
  219. printf(SZ_CRC16CCITT);
  220. printf(SZ_HASHPRE);
  221. printf("%04X", m_crc16ccitt);
  222. printf(CPS_NEWLINE);
  223. }
  224. if(m_bAlgorithm[HASHID_CRC32])
  225. {
  226. crc32Finish(&m_crc32);
  227. printf(SZ_CRC32);
  228. printf(SZ_HASHPRE);
  229. printf("%08X", m_crc32);
  230. printf(CPS_NEWLINE);
  231. }
  232. if(m_bAlgorithm[HASHID_FCS_16])
  233. {
  234. fcs16_final(&m_fcs16);
  235. printf(SZ_FCS_16);
  236. printf(SZ_HASHPRE);
  237. printf("%04X", m_fcs16);
  238. printf(CPS_NEWLINE);
  239. }
  240. if(m_bAlgorithm[HASHID_FCS_32])
  241. {
  242. fcs32_final(&m_fcs32);
  243. printf(SZ_FCS_32);
  244. printf(SZ_HASHPRE);
  245. printf("%08X", m_fcs32);
  246. printf(CPS_NEWLINE);
  247. }
  248. if(m_bAlgorithm[HASHID_GHASH_32_3])
  249. {
  250. m_ghash.FinalToStr(szTemp, 3);
  251. printf(SZ_GHASH_32_3);
  252. printf(SZ_HASHPRE);
  253. printf(szTemp);
  254. printf(CPS_NEWLINE);
  255. }
  256. if(m_bAlgorithm[HASHID_GHASH_32_5])
  257. {
  258. m_ghash.FinalToStr(szTemp, 5);
  259. printf(SZ_GHASH_32_5);
  260. printf(SZ_HASHPRE);
  261. printf(szTemp);
  262. printf(CPS_NEWLINE);
  263. }
  264. if(m_bAlgorithm[HASHID_GOST])
  265. {
  266. gosthash_final(&m_gost, pTemp);
  267. printf(SZ_GOST);
  268. printf(SZ_HASHPRE);
  269. for(i = 0; i < 32; i++)
  270. {
  271. fmtFixHashOutput(i);
  272. printf("%02X", pTemp[i]);
  273. }
  274. printf(CPS_NEWLINE);
  275. }
  276. if(m_bAlgorithm[HASHID_HAVAL])
  277. {
  278. haval_end(&m_haval, pTemp);
  279. printf(SZ_HAVAL);
  280. printf(SZ_HASHPRE);
  281. for(i = 0; i < 32; i++)
  282. {
  283. fmtFixHashOutput(i);
  284. printf("%02X", pTemp[i]);
  285. }
  286. printf(CPS_NEWLINE);
  287. }
  288. if(m_bAlgorithm[HASHID_MD2])
  289. {
  290. m_md2.TruncatedFinal(pTemp, 16);
  291. printf(SZ_MD2);
  292. printf(SZ_HASHPRE);
  293. for(i = 0; i < 16; i++)
  294. {
  295. fmtFixHashOutput(i);
  296. printf("%02X", pTemp[i]);
  297. }
  298. printf(CPS_NEWLINE);
  299. }
  300. if(m_bAlgorithm[HASHID_MD4])
  301. {
  302. MD4Final(pTemp, &m_md4);
  303. printf(SZ_MD4);
  304. printf(SZ_HASHPRE);
  305. for(i = 0; i < 16; i++)
  306. {
  307. fmtFixHashOutput(i);
  308. printf("%02X", pTemp[i]);
  309. }
  310. printf(CPS_NEWLINE);
  311. }
  312. if(m_bAlgorithm[HASHID_MD5])
  313. {
  314. MD5Final(&m_md5);
  315. printf(SZ_MD5);
  316. printf(SZ_HASHPRE);
  317. for(i = 0; i < 16; i++)
  318. {
  319. fmtFixHashOutput(i);
  320. printf("%02X", m_md5.digest[i]);
  321. }
  322. printf(CPS_NEWLINE);
  323. }
  324. if(m_bAlgorithm[HASHID_SHA1])
  325. {
  326. sha1_end(pTemp, &m_sha1);
  327. printf(SZ_SHA1);
  328. printf(SZ_HASHPRE);
  329. for(i = 0; i < 20; i++)
  330. {
  331. fmtFixHashOutput(i);
  332. printf("%02X", pTemp[i]);
  333. }
  334. printf(CPS_NEWLINE);
  335. }
  336. if(m_bAlgorithm[HASHID_SHA2_256])
  337. {
  338. sha256_end(pTemp, &m_sha256);
  339. printf(SZ_SHA2_256);
  340. printf(SZ_HASHPRE);
  341. for(i = 0; i < 32; i++)
  342. {
  343. fmtFixHashOutput(i);
  344. printf("%02X", pTemp[i]);
  345. }
  346. printf(CPS_NEWLINE);
  347. }
  348. if(m_bAlgorithm[HASHID_SHA2_384])
  349. {
  350. sha384_end(pTemp, &m_sha384);
  351. printf(SZ_SHA2_384);
  352. printf(SZ_HASHPRE);
  353. for(i = 0; i < 48; i++)
  354. {
  355. fmtFixHashOutput(i);
  356. printf("%02X", pTemp[i]);
  357. }
  358. printf(CPS_NEWLINE);
  359. }
  360. if(m_bAlgorithm[HASHID_SHA2_512])
  361. {
  362. sha512_end(pTemp, &m_sha512);
  363. printf(SZ_SHA2_512);
  364. printf(SZ_HASHPRE);
  365. for(i = 0; i < 64; i++)
  366. {
  367. fmtFixHashOutput(i);
  368. printf("%02X", pTemp[i]);
  369. }
  370. printf(CPS_NEWLINE);
  371. }
  372. if(m_bAlgorithm[HASHID_TIGER])
  373. {
  374. tiger_done(&m_tiger, pTemp);
  375. printf(SZ_TIGER);
  376. printf(SZ_HASHPRE);
  377. for(i = 0; i < 8; i++) { fmtFixHashOutput(i); printf("%02X", pTemp[7-i]); }
  378. for(i = 8; i < 16; i++) { fmtFixHashOutput(i); printf("%02X", pTemp[23-i]); }
  379. for(i = 16; i < 24; i++) { fmtFixHashOutput(i); printf("%02X", pTemp[39-i]); }
  380. printf(CPS_NEWLINE);
  381. }
  382. return RH_SUCCESS;
  383. }
  384. void CHashManager::fmtFixHashOutput(int nCursorPos)
  385. {
  386. unsigned int i = 0;
  387. if((nCursorPos % 4 == 0) && (nCursorPos != 0))
  388. {
  389. if(nCursorPos % NUM_DIGBLOCKS_PER_LINE == 0)
  390. {
  391. printf(CPS_NEWLINE);
  392. printf(SZ_EMPTY);
  393. for(i = 0; i < strlen(SZ_HASHPRE); i++) printf(" ");
  394. }
  395. else
  396. {
  397. printf(SZ_HASHSEP);
  398. }
  399. }
  400. }