hashes.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: hashes.cc,v 1.1 2001/03/06 07:15:29 jgg Exp $
  4. /* ######################################################################
  5. Hashes - Simple wrapper around the hash functions
  6. This is just used to make building the methods simpler, this is the
  7. only interface required..
  8. ##################################################################### */
  9. /*}}}*/
  10. // Include Files /*{{{*/
  11. #include <config.h>
  12. #include <apt-pkg/hashes.h>
  13. #include <apt-pkg/fileutl.h>
  14. #include <apt-pkg/configuration.h>
  15. #include <apt-pkg/macros.h>
  16. #include <unistd.h>
  17. #include <string>
  18. #include <iostream>
  19. /*}}}*/
  20. const char* HashString::_SupportedHashes[] =
  21. {
  22. "SHA512", "SHA256", "SHA1", "MD5Sum", NULL
  23. };
  24. HashString::HashString()
  25. {
  26. }
  27. HashString::HashString(std::string Type, std::string Hash) : Type(Type), Hash(Hash)
  28. {
  29. }
  30. HashString::HashString(std::string StringedHash) /*{{{*/
  31. {
  32. // legacy: md5sum without "MD5Sum:" prefix
  33. if (StringedHash.find(":") == std::string::npos && StringedHash.size() == 32)
  34. {
  35. Type = "MD5Sum";
  36. Hash = StringedHash;
  37. return;
  38. }
  39. std::string::size_type pos = StringedHash.find(":");
  40. Type = StringedHash.substr(0,pos);
  41. Hash = StringedHash.substr(pos+1, StringedHash.size() - pos);
  42. if(_config->FindB("Debug::Hashes",false) == true)
  43. std::clog << "HashString(string): " << Type << " : " << Hash << std::endl;
  44. }
  45. /*}}}*/
  46. bool HashString::VerifyFile(std::string filename) const /*{{{*/
  47. {
  48. std::string fileHash = GetHashForFile(filename);
  49. if(_config->FindB("Debug::Hashes",false) == true)
  50. std::clog << "HashString::VerifyFile: got: " << fileHash << " expected: " << toStr() << std::endl;
  51. return (fileHash == Hash);
  52. }
  53. /*}}}*/
  54. bool HashString::FromFile(std::string filename) /*{{{*/
  55. {
  56. // pick the strongest hash
  57. if (Type == "")
  58. Type = _SupportedHashes[0];
  59. Hash = GetHashForFile(filename);
  60. return true;
  61. }
  62. /*}}}*/
  63. std::string HashString::GetHashForFile(std::string filename) const /*{{{*/
  64. {
  65. std::string fileHash;
  66. FileFd Fd(filename, FileFd::ReadOnly);
  67. if(Type == "MD5Sum")
  68. {
  69. MD5Summation MD5;
  70. MD5.AddFD(Fd);
  71. fileHash = (std::string)MD5.Result();
  72. }
  73. else if (Type == "SHA1")
  74. {
  75. SHA1Summation SHA1;
  76. SHA1.AddFD(Fd);
  77. fileHash = (std::string)SHA1.Result();
  78. }
  79. else if (Type == "SHA256")
  80. {
  81. SHA256Summation SHA256;
  82. SHA256.AddFD(Fd);
  83. fileHash = (std::string)SHA256.Result();
  84. }
  85. else if (Type == "SHA512")
  86. {
  87. SHA512Summation SHA512;
  88. SHA512.AddFD(Fd);
  89. fileHash = (std::string)SHA512.Result();
  90. }
  91. Fd.Close();
  92. return fileHash;
  93. }
  94. /*}}}*/
  95. const char** HashString::SupportedHashes()
  96. {
  97. return _SupportedHashes;
  98. }
  99. bool HashString::empty() const
  100. {
  101. return (Type.empty() || Hash.empty());
  102. }
  103. std::string HashString::toStr() const
  104. {
  105. return Type + std::string(":") + Hash;
  106. }
  107. // Hashes::AddFD - Add the contents of the FD /*{{{*/
  108. // ---------------------------------------------------------------------
  109. /* */
  110. bool Hashes::AddFD(int const Fd,unsigned long long Size, bool const addMD5,
  111. bool const addSHA1, bool const addSHA256, bool const addSHA512)
  112. {
  113. unsigned char Buf[64*64];
  114. ssize_t Res = 0;
  115. int ToEOF = (Size == 0);
  116. while (Size != 0 || ToEOF)
  117. {
  118. unsigned long long n = sizeof(Buf);
  119. if (!ToEOF) n = std::min(Size, n);
  120. Res = read(Fd,Buf,n);
  121. if (Res < 0 || (!ToEOF && Res != (ssize_t) n)) // error, or short read
  122. return false;
  123. if (ToEOF && Res == 0) // EOF
  124. break;
  125. Size -= Res;
  126. if (addMD5 == true)
  127. MD5.Add(Buf,Res);
  128. if (addSHA1 == true)
  129. SHA1.Add(Buf,Res);
  130. if (addSHA256 == true)
  131. SHA256.Add(Buf,Res);
  132. if (addSHA512 == true)
  133. SHA512.Add(Buf,Res);
  134. }
  135. return true;
  136. }
  137. bool Hashes::AddFD(FileFd &Fd,unsigned long long Size, bool const addMD5,
  138. bool const addSHA1, bool const addSHA256, bool const addSHA512)
  139. {
  140. unsigned char Buf[64*64];
  141. bool const ToEOF = (Size == 0);
  142. while (Size != 0 || ToEOF)
  143. {
  144. unsigned long long n = sizeof(Buf);
  145. if (!ToEOF) n = std::min(Size, n);
  146. unsigned long long a = 0;
  147. if (Fd.Read(Buf, n, &a) == false) // error
  148. return false;
  149. if (ToEOF == false)
  150. {
  151. if (a != n) // short read
  152. return false;
  153. }
  154. else if (a == 0) // EOF
  155. break;
  156. Size -= a;
  157. if (addMD5 == true)
  158. MD5.Add(Buf, a);
  159. if (addSHA1 == true)
  160. SHA1.Add(Buf, a);
  161. if (addSHA256 == true)
  162. SHA256.Add(Buf, a);
  163. if (addSHA512 == true)
  164. SHA512.Add(Buf, a);
  165. }
  166. return true;
  167. }
  168. /*}}}*/