hashes.cc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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/md5.h>
  16. #include <apt-pkg/sha1.h>
  17. #include <apt-pkg/sha2.h>
  18. #include <stddef.h>
  19. #include <algorithm>
  20. #include <unistd.h>
  21. #include <string>
  22. #include <iostream>
  23. /*}}}*/
  24. const char * HashString::_SupportedHashes[] =
  25. {
  26. "SHA512", "SHA256", "SHA1", "MD5Sum", NULL
  27. };
  28. HashString::HashString()
  29. {
  30. }
  31. HashString::HashString(std::string Type, std::string Hash) : Type(Type), Hash(Hash)
  32. {
  33. }
  34. HashString::HashString(std::string StringedHash) /*{{{*/
  35. {
  36. if (StringedHash.find(":") == std::string::npos)
  37. {
  38. // legacy: md5sum without "MD5Sum:" prefix
  39. if (StringedHash.size() == 32)
  40. {
  41. Type = "MD5Sum";
  42. Hash = StringedHash;
  43. }
  44. if(_config->FindB("Debug::Hashes",false) == true)
  45. std::clog << "HashString(string): invalid StringedHash " << StringedHash << std::endl;
  46. return;
  47. }
  48. std::string::size_type pos = StringedHash.find(":");
  49. Type = StringedHash.substr(0,pos);
  50. Hash = StringedHash.substr(pos+1, StringedHash.size() - pos);
  51. if(_config->FindB("Debug::Hashes",false) == true)
  52. std::clog << "HashString(string): " << Type << " : " << Hash << std::endl;
  53. }
  54. /*}}}*/
  55. bool HashString::VerifyFile(std::string filename) const /*{{{*/
  56. {
  57. std::string fileHash = GetHashForFile(filename);
  58. if(_config->FindB("Debug::Hashes",false) == true)
  59. std::clog << "HashString::VerifyFile: got: " << fileHash << " expected: " << toStr() << std::endl;
  60. return (fileHash == Hash);
  61. }
  62. /*}}}*/
  63. bool HashString::FromFile(std::string filename) /*{{{*/
  64. {
  65. // pick the strongest hash
  66. if (Type == "")
  67. Type = _SupportedHashes[0];
  68. Hash = GetHashForFile(filename);
  69. return true;
  70. }
  71. /*}}}*/
  72. std::string HashString::GetHashForFile(std::string filename) const /*{{{*/
  73. {
  74. std::string fileHash;
  75. FileFd Fd(filename, FileFd::ReadOnly);
  76. if(strcasecmp(Type.c_str(), "MD5Sum") == 0)
  77. {
  78. MD5Summation MD5;
  79. MD5.AddFD(Fd);
  80. fileHash = (std::string)MD5.Result();
  81. }
  82. else if (strcasecmp(Type.c_str(), "SHA1") == 0)
  83. {
  84. SHA1Summation SHA1;
  85. SHA1.AddFD(Fd);
  86. fileHash = (std::string)SHA1.Result();
  87. }
  88. else if (strcasecmp(Type.c_str(), "SHA256") == 0)
  89. {
  90. SHA256Summation SHA256;
  91. SHA256.AddFD(Fd);
  92. fileHash = (std::string)SHA256.Result();
  93. }
  94. else if (strcasecmp(Type.c_str(), "SHA512") == 0)
  95. {
  96. SHA512Summation SHA512;
  97. SHA512.AddFD(Fd);
  98. fileHash = (std::string)SHA512.Result();
  99. }
  100. Fd.Close();
  101. return fileHash;
  102. }
  103. /*}}}*/
  104. const char** HashString::SupportedHashes() /*{{{*/
  105. {
  106. return _SupportedHashes;
  107. }
  108. /*}}}*/
  109. APT_PURE bool HashString::empty() const /*{{{*/
  110. {
  111. return (Type.empty() || Hash.empty());
  112. }
  113. /*}}}*/
  114. std::string HashString::toStr() const /*{{{*/
  115. {
  116. return Type + ":" + Hash;
  117. }
  118. /*}}}*/
  119. APT_PURE bool HashString::operator==(HashString const &other) const /*{{{*/
  120. {
  121. return (strcasecmp(Type.c_str(), other.Type.c_str()) == 0 && Hash == other.Hash);
  122. }
  123. APT_PURE bool HashString::operator!=(HashString const &other) const
  124. {
  125. return !(*this == other);
  126. }
  127. /*}}}*/
  128. bool HashStringList::usable() const /*{{{*/
  129. {
  130. if (empty() == true)
  131. return false;
  132. std::string const forcedType = _config->Find("Acquire::ForceHash", "");
  133. if (forcedType.empty() == true)
  134. return true;
  135. return find(forcedType) != NULL;
  136. }
  137. /*}}}*/
  138. HashString const * HashStringList::find(char const * const type) const /*{{{*/
  139. {
  140. if (type == NULL || type[0] == '\0')
  141. {
  142. std::string const forcedType = _config->Find("Acquire::ForceHash", "");
  143. if (forcedType.empty() == false)
  144. return find(forcedType.c_str());
  145. for (char const * const * t = HashString::SupportedHashes(); *t != NULL; ++t)
  146. for (std::vector<HashString>::const_iterator hs = list.begin(); hs != list.end(); ++hs)
  147. if (strcasecmp(hs->HashType().c_str(), *t) == 0)
  148. return &*hs;
  149. return NULL;
  150. }
  151. for (std::vector<HashString>::const_iterator hs = list.begin(); hs != list.end(); ++hs)
  152. if (strcasecmp(hs->HashType().c_str(), type) == 0)
  153. return &*hs;
  154. return NULL;
  155. }
  156. /*}}}*/
  157. bool HashStringList::supported(char const * const type) /*{{{*/
  158. {
  159. for (char const * const * t = HashString::SupportedHashes(); *t != NULL; ++t)
  160. if (strcasecmp(*t, type) == 0)
  161. return true;
  162. return false;
  163. }
  164. /*}}}*/
  165. bool HashStringList::push_back(const HashString &hashString) /*{{{*/
  166. {
  167. if (hashString.HashType().empty() == true ||
  168. hashString.HashValue().empty() == true ||
  169. supported(hashString.HashType().c_str()) == false)
  170. return false;
  171. // ensure that each type is added only once
  172. HashString const * const hs = find(hashString.HashType().c_str());
  173. if (hs != NULL)
  174. return *hs == hashString;
  175. list.push_back(hashString);
  176. return true;
  177. }
  178. /*}}}*/
  179. bool HashStringList::VerifyFile(std::string filename) const /*{{{*/
  180. {
  181. if (list.empty() == true)
  182. return false;
  183. HashString const * const hs = find(NULL);
  184. if (hs == NULL || hs->VerifyFile(filename) == false)
  185. return false;
  186. return true;
  187. }
  188. /*}}}*/
  189. bool HashStringList::operator==(HashStringList const &other) const /*{{{*/
  190. {
  191. std::string const forcedType = _config->Find("Acquire::ForceHash", "");
  192. if (forcedType.empty() == false)
  193. {
  194. HashString const * const hs = other.find(forcedType);
  195. HashString const * const ohs = other.find(forcedType);
  196. if (hs == NULL || ohs == NULL)
  197. return false;
  198. return hs == ohs;
  199. }
  200. short matches = 0;
  201. for (const_iterator hs = begin(); hs != end(); ++hs)
  202. {
  203. HashString const * const ohs = other.find(hs->HashType());
  204. if (ohs == NULL)
  205. continue;
  206. if (*hs != *ohs)
  207. return false;
  208. ++matches;
  209. }
  210. if (matches == 0)
  211. return false;
  212. return true;
  213. }
  214. bool HashStringList::operator!=(HashStringList const &other) const
  215. {
  216. return !(*this == other);
  217. }
  218. /*}}}*/
  219. // Hashes::Add* - Add the contents of data or FD /*{{{*/
  220. bool Hashes::Add(const unsigned char * const Data,unsigned long long const Size, unsigned int const Hashes)
  221. {
  222. bool Res = true;
  223. #if __GNUC__ >= 4
  224. #pragma GCC diagnostic push
  225. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  226. #endif
  227. if ((Hashes & MD5SUM) == MD5SUM)
  228. Res &= MD5.Add(Data, Size);
  229. if ((Hashes & SHA1SUM) == SHA1SUM)
  230. Res &= SHA1.Add(Data, Size);
  231. if ((Hashes & SHA256SUM) == SHA256SUM)
  232. Res &= SHA256.Add(Data, Size);
  233. if ((Hashes & SHA512SUM) == SHA512SUM)
  234. Res &= SHA512.Add(Data, Size);
  235. #if __GNUC__ >= 4
  236. #pragma GCC diagnostic pop
  237. #endif
  238. return Res;
  239. }
  240. bool Hashes::AddFD(int const Fd,unsigned long long Size, unsigned int const Hashes)
  241. {
  242. unsigned char Buf[64*64];
  243. bool const ToEOF = (Size == UntilEOF);
  244. while (Size != 0 || ToEOF)
  245. {
  246. unsigned long long n = sizeof(Buf);
  247. if (!ToEOF) n = std::min(Size, n);
  248. ssize_t const Res = read(Fd,Buf,n);
  249. if (Res < 0 || (!ToEOF && Res != (ssize_t) n)) // error, or short read
  250. return false;
  251. if (ToEOF && Res == 0) // EOF
  252. break;
  253. Size -= Res;
  254. if (Add(Buf, Res, Hashes) == false)
  255. return false;
  256. }
  257. return true;
  258. }
  259. bool Hashes::AddFD(FileFd &Fd,unsigned long long Size, unsigned int const Hashes)
  260. {
  261. unsigned char Buf[64*64];
  262. bool const ToEOF = (Size == 0);
  263. while (Size != 0 || ToEOF)
  264. {
  265. unsigned long long n = sizeof(Buf);
  266. if (!ToEOF) n = std::min(Size, n);
  267. unsigned long long a = 0;
  268. if (Fd.Read(Buf, n, &a) == false) // error
  269. return false;
  270. if (ToEOF == false)
  271. {
  272. if (a != n) // short read
  273. return false;
  274. }
  275. else if (a == 0) // EOF
  276. break;
  277. Size -= a;
  278. if (Add(Buf, a, Hashes) == false)
  279. return false;
  280. }
  281. return true;
  282. }
  283. /*}}}*/
  284. HashStringList Hashes::GetHashStringList()
  285. {
  286. HashStringList hashes;
  287. #if __GNUC__ >= 4
  288. #pragma GCC diagnostic push
  289. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  290. #endif
  291. hashes.push_back(HashString("MD5Sum", MD5.Result().Value()));
  292. hashes.push_back(HashString("SHA1", SHA1.Result().Value()));
  293. hashes.push_back(HashString("SHA256", SHA256.Result().Value()));
  294. hashes.push_back(HashString("SHA512", SHA512.Result().Value()));
  295. #if __GNUC__ >= 4
  296. #pragma GCC diagnostic pop
  297. #endif
  298. return hashes;
  299. }
  300. #if __GNUC__ >= 4
  301. #pragma GCC diagnostic push
  302. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  303. #pragma GCC diagnostic ignored "-Wsuggest-attribute=const"
  304. #endif
  305. Hashes::Hashes() {}
  306. Hashes::~Hashes() {}
  307. #if __GNUC__ >= 4
  308. #pragma GCC diagnostic pop
  309. #endif