Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

215 lines
5.0 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1996 - 1999
  6. //
  7. // File: crypthash.cpp
  8. //
  9. // Contents: performance suite
  10. //
  11. // History: 04-Dec-1997 pberkman created
  12. //
  13. //--------------------------------------------------------------------------
  14. #include "global.hxx"
  15. #include "sha.h"
  16. #include "md5.h"
  17. BOOL _HashFile(HANDLE hFile, char *pszFile, BOOL fSha1);
  18. DWORD WINAPI TestCryptHash(ThreadData *psData)
  19. {
  20. COleDateTime tStart;
  21. COleDateTime tEnd;
  22. DWORD i;
  23. char szFile[MAX_PATH];
  24. BOOL fFind;
  25. HANDLE hFind;
  26. WIN32_FIND_DATA sFindData;
  27. HCRYPTPROV hProv;
  28. HANDLE hFile;
  29. WCHAR *pwszLastSlash;
  30. WCHAR wszDir[MAX_PATH];
  31. WCHAR wszFile[MAX_PATH];
  32. DWORD dwDirLen;
  33. hFind = INVALID_HANDLE_VALUE;
  34. psData->dwTotalProcessed = 0;
  35. printf("\n CRYPT_HASH");
  36. if (pwszLastSlash = wcsrchr(pwszInFile, L'\\'))
  37. {
  38. *pwszLastSlash = NULL;
  39. wcscpy(&wszDir[0], pwszInFile);
  40. wcscat(&wszDir[0], L"\\");
  41. *pwszLastSlash = L'\\';
  42. dwDirLen = wcslen(&wszDir[0]);
  43. }
  44. else
  45. {
  46. wszDir[0] = NULL;
  47. dwDirLen = 0;
  48. }
  49. tStart = COleDateTime::GetCurrentTime();
  50. for (i = 0; i < cPasses; i++)
  51. {
  52. szFile[0] = NULL;
  53. WideCharToMultiByte(0, 0, pwszInFile, -1, &szFile[0], MAX_PATH, NULL, NULL);
  54. if ((hFind = FindFirstFile(&szFile[0], &sFindData)) == INVALID_HANDLE_VALUE)
  55. {
  56. goto FileFindError;
  57. }
  58. fFind = TRUE;
  59. while (fFind)
  60. {
  61. if (!(sFindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
  62. {
  63. if (dwDirLen > 0)
  64. {
  65. wcscpy(&wszFile[0], &wszDir[0]);
  66. }
  67. wszFile[dwDirLen] = NULL;
  68. MultiByteToWideChar(0, 0, &sFindData.cFileName[0], -1, &wszFile[dwDirLen], MAX_PATH * sizeof(WCHAR));
  69. hFile = CreateFileU(&wszFile[0], GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
  70. NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  71. if (hFile != INVALID_HANDLE_VALUE)
  72. {
  73. if (_HashFile(hFile, sFindData.cFileName,
  74. (psData->dwPassThrough & PASSTHROUGH_SHA1) ? TRUE : FALSE))
  75. {
  76. psData->dwTotalProcessed++;
  77. }
  78. CloseHandle(hFile);
  79. }
  80. }
  81. fFind = FindNextFile(hFind, &sFindData);
  82. }
  83. if (hFind != INVALID_HANDLE_VALUE)
  84. {
  85. FindClose(hFind);
  86. }
  87. }
  88. ErrorReturn:
  89. tEnd = COleDateTime::GetCurrentTime();
  90. psData->tsTotal = tEnd - tStart;
  91. return(0);
  92. TRACE_ERROR_EX(DBG_SS_APP, FileFindError);
  93. }
  94. BOOL _HashFile(HANDLE hFile, char *pszFile, BOOL fSha1)
  95. {
  96. DWORD cbHash;
  97. BYTE bHash[30];
  98. BYTE *pbFile;
  99. DWORD cbFile;
  100. HANDLE hMappedFile;
  101. BOOL fRet;
  102. MD5_CTX sMD5;
  103. A_SHA_CTX sSHA1;
  104. pbFile = NULL;
  105. hMappedFile = NULL;
  106. if (fVerbose)
  107. {
  108. printf("\n %s ", pszFile);
  109. }
  110. hMappedFile = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
  111. if (!(hMappedFile) || (hMappedFile == INVALID_HANDLE_VALUE))
  112. {
  113. hMappedFile = NULL;
  114. goto CreateMapError;
  115. }
  116. if (!(pbFile = (BYTE *)MapViewOfFile(hMappedFile, FILE_MAP_READ, 0, 0, 0)))
  117. {
  118. goto MapViewError;
  119. }
  120. cbFile = GetFileSize(hFile, NULL);
  121. if (fSha1)
  122. {
  123. memset(&sSHA1, 0x00, sizeof(A_SHA_CTX));
  124. A_SHAInit(&sSHA1);
  125. A_SHAUpdate(&sSHA1, pbFile, cbFile);
  126. A_SHAFinal(&sSHA1, &bHash[0]);
  127. cbHash = A_SHA_DIGEST_LEN;
  128. }
  129. else
  130. {
  131. memset(&sMD5, 0x00, sizeof(MD5_CTX));
  132. MD5Init(&sMD5);
  133. MD5Update(&sMD5, pbFile, cbFile);
  134. MD5Final(&sMD5);
  135. memcpy(&bHash[0], sMD5.digest, MD5DIGESTLEN);
  136. cbHash = MD5DIGESTLEN;
  137. }
  138. //dwTotalBytes += cbFile;
  139. if (fVerbose)
  140. {
  141. DWORD i;
  142. for (i = 0; i < cbHash; i++)
  143. {
  144. printf("%02.2X", bHash[i]);
  145. }
  146. }
  147. fRet = TRUE;
  148. CommonReturn:
  149. if (hMappedFile)
  150. {
  151. CloseHandle(hMappedFile);
  152. }
  153. if (pbFile)
  154. {
  155. UnmapViewOfFile(pbFile);
  156. }
  157. return(fRet);
  158. ErrorReturn:
  159. if (fVerbose)
  160. {
  161. printf("*failed*");
  162. }
  163. fRet = FALSE;
  164. goto CommonReturn;
  165. TRACE_ERROR_EX(DBG_SS_APP, CreateMapError);
  166. TRACE_ERROR_EX(DBG_SS_APP, MapViewError);
  167. }