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.

134 lines
2.8 KiB

  1. // NtRelHash.cpp : Mini hash for the NT code base builds
  2. // (c) 2002 Microsoft Corporation
  3. // [jorgeba] Jorge Peraza
  4. //
  5. #include "stdafx.h"
  6. #include "fastfilehash.h"
  7. using namespace ::std;
  8. __int32* getReleaseHash(TCHAR *sDir,TCHAR *sFiles, IFileHash* oHashGen);
  9. char* hashManifest(__int32 *piHash);
  10. //Entry point for tge application
  11. int __cdecl main(int argc, char* argv[])
  12. {
  13. CFastFileHash * oHashGen = new CFastFileHash();
  14. TCHAR sDir[MAX_PATH];
  15. //Check for the required arguments
  16. if(argc<2)
  17. {
  18. return 0;
  19. }
  20. //Covert the input to Unicode
  21. if(MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,argv[1],strlen(argv[1])+1,sDir,MAX_PATH-1)==0)
  22. {
  23. return 0;
  24. }
  25. //Generate the hash
  26. if(oHashGen!=NULL)
  27. {
  28. getReleaseHash(sDir,_T("nt*"),(IFileHash*) oHashGen);
  29. delete oHashGen;
  30. }
  31. return 0;
  32. }
  33. // Generate the release hash,
  34. __int32* getReleaseHash(TCHAR *sDir,TCHAR *sFiles, IFileHash* oHashGen)
  35. {
  36. //You'll see __int32 a lot, this is required to make this work with thw windows 64 platform
  37. HANDLE hSearch;
  38. WIN32_FIND_DATA FindFileData;
  39. TCHAR sFileName[MAX_PATH];
  40. TCHAR *sSearchStr = NULL;
  41. char* pcManifest = NULL;
  42. int iChars = 0;
  43. __int32 *piHash;
  44. __int32 piCombHash[5];
  45. if((sDir==NULL)||(sFiles==NULL)||(oHashGen==NULL))
  46. {
  47. return NULL;
  48. }
  49. //Generate the search string
  50. iChars = _tcslen(sDir);
  51. iChars += _tcslen(sFiles);
  52. sSearchStr = new TCHAR[iChars+1];
  53. if(sSearchStr==NULL)
  54. {
  55. return NULL;
  56. }
  57. _stprintf(sSearchStr,_T("%s%s"),sDir,sFiles);
  58. //Find the first file in the release directory
  59. hSearch = FindFirstFile(sSearchStr, &FindFileData );
  60. delete[] sSearchStr;
  61. memset(piCombHash,0,sizeof(__int32)*5);
  62. //Calculate the release hash
  63. do
  64. {
  65. if(!(FindFileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))
  66. {
  67. _stprintf(sFileName,_T("%s%s"),sDir,FindFileData.cFileName);
  68. piHash = oHashGen->getHash(sFileName);
  69. if(piHash!=NULL)
  70. {
  71. for(int iNdx = 0;iNdx < 5;iNdx++)
  72. {
  73. piCombHash[iNdx] = piCombHash[iNdx] ^ piHash[iNdx]; }
  74. delete[] piHash;
  75. }
  76. }
  77. }
  78. while(FindNextFile(hSearch,&FindFileData));
  79. //Generate the Manifest for the hash (Digital signature)
  80. pcManifest = hashManifest(piCombHash);
  81. cout << pcManifest;
  82. delete[] pcManifest;
  83. return NULL;
  84. }
  85. char* hashManifest(__int32 *piHash)
  86. {
  87. char* pcManifest = NULL;
  88. char cTemp;
  89. //Create the Manifest string
  90. pcManifest = new char[41];
  91. if(pcManifest==NULL)
  92. {
  93. return NULL;
  94. }
  95. for(int iNdx=0;iNdx<5;iNdx++)
  96. {
  97. for(int iNdj=0;iNdj<8;iNdj+=2)
  98. {
  99. memcpy(&cTemp,((char*)piHash+(iNdx*4)+(iNdj/2)),1);
  100. pcManifest[(iNdx*8)+iNdj] = 0x40 | ((cTemp>>4)&0x0f);
  101. pcManifest[(iNdx*8)+iNdj+1]= 0x40 | (cTemp&0x0f);
  102. }
  103. }
  104. pcManifest[40] = 0;
  105. return pcManifest;
  106. }