hashes.cc 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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", "Checksum-FileSize", 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. else if (strcasecmp(Type.c_str(), "Checksum-FileSize") == 0)
  101. strprintf(fileHash, "%llu", Fd.FileSize());
  102. Fd.Close();
  103. return fileHash;
  104. }
  105. /*}}}*/
  106. const char** HashString::SupportedHashes() /*{{{*/
  107. {
  108. return _SupportedHashes;
  109. }
  110. /*}}}*/
  111. APT_PURE bool HashString::empty() const /*{{{*/
  112. {
  113. return (Type.empty() || Hash.empty());
  114. }
  115. /*}}}*/
  116. std::string HashString::toStr() const /*{{{*/
  117. {
  118. return Type + ":" + Hash;
  119. }
  120. /*}}}*/
  121. APT_PURE bool HashString::operator==(HashString const &other) const /*{{{*/
  122. {
  123. return (strcasecmp(Type.c_str(), other.Type.c_str()) == 0 && Hash == other.Hash);
  124. }
  125. APT_PURE bool HashString::operator!=(HashString const &other) const
  126. {
  127. return !(*this == other);
  128. }
  129. /*}}}*/
  130. bool HashStringList::usable() const /*{{{*/
  131. {
  132. if (empty() == true)
  133. return false;
  134. std::string const forcedType = _config->Find("Acquire::ForceHash", "");
  135. if (forcedType.empty() == true)
  136. {
  137. // FileSize alone isn't usable
  138. for (std::vector<HashString>::const_iterator hs = list.begin(); hs != list.end(); ++hs)
  139. if (hs->HashType() != "Checksum-FileSize")
  140. return true;
  141. return false;
  142. }
  143. return find(forcedType) != NULL;
  144. }
  145. /*}}}*/
  146. HashString const * HashStringList::find(char const * const type) const /*{{{*/
  147. {
  148. if (type == NULL || type[0] == '\0')
  149. {
  150. std::string const forcedType = _config->Find("Acquire::ForceHash", "");
  151. if (forcedType.empty() == false)
  152. return find(forcedType.c_str());
  153. for (char const * const * t = HashString::SupportedHashes(); *t != NULL; ++t)
  154. for (std::vector<HashString>::const_iterator hs = list.begin(); hs != list.end(); ++hs)
  155. if (strcasecmp(hs->HashType().c_str(), *t) == 0)
  156. return &*hs;
  157. return NULL;
  158. }
  159. for (std::vector<HashString>::const_iterator hs = list.begin(); hs != list.end(); ++hs)
  160. if (strcasecmp(hs->HashType().c_str(), type) == 0)
  161. return &*hs;
  162. return NULL;
  163. }
  164. /*}}}*/
  165. bool HashStringList::supported(char const * const type) /*{{{*/
  166. {
  167. for (char const * const * t = HashString::SupportedHashes(); *t != NULL; ++t)
  168. if (strcasecmp(*t, type) == 0)
  169. return true;
  170. return false;
  171. }
  172. /*}}}*/
  173. bool HashStringList::push_back(const HashString &hashString) /*{{{*/
  174. {
  175. if (hashString.HashType().empty() == true ||
  176. hashString.HashValue().empty() == true ||
  177. supported(hashString.HashType().c_str()) == false)
  178. return false;
  179. // ensure that each type is added only once
  180. HashString const * const hs = find(hashString.HashType().c_str());
  181. if (hs != NULL)
  182. return *hs == hashString;
  183. list.push_back(hashString);
  184. return true;
  185. }
  186. /*}}}*/
  187. bool HashStringList::VerifyFile(std::string filename) const /*{{{*/
  188. {
  189. if (list.empty() == true)
  190. return false;
  191. HashString const * const hs = find(NULL);
  192. if (hs == NULL || hs->VerifyFile(filename) == false)
  193. return false;
  194. HashString const * const hsf = find("Checksum-FileSize");
  195. if (hsf != NULL && hsf->VerifyFile(filename) == false)
  196. return false;
  197. return true;
  198. }
  199. /*}}}*/
  200. bool HashStringList::operator==(HashStringList const &other) const /*{{{*/
  201. {
  202. std::string const forcedType = _config->Find("Acquire::ForceHash", "");
  203. if (forcedType.empty() == false)
  204. {
  205. HashString const * const hs = find(forcedType);
  206. HashString const * const ohs = other.find(forcedType);
  207. if (hs == NULL || ohs == NULL)
  208. return false;
  209. return *hs == *ohs;
  210. }
  211. short matches = 0;
  212. for (const_iterator hs = begin(); hs != end(); ++hs)
  213. {
  214. HashString const * const ohs = other.find(hs->HashType());
  215. if (ohs == NULL)
  216. continue;
  217. if (*hs != *ohs)
  218. return false;
  219. ++matches;
  220. }
  221. if (matches == 0)
  222. return false;
  223. return true;
  224. }
  225. bool HashStringList::operator!=(HashStringList const &other) const
  226. {
  227. return !(*this == other);
  228. }
  229. /*}}}*/
  230. // PrivateHashes /*{{{*/
  231. class PrivateHashes {
  232. public:
  233. unsigned long long FileSize;
  234. PrivateHashes() : FileSize(0) {}
  235. };
  236. /*}}}*/
  237. // Hashes::Add* - Add the contents of data or FD /*{{{*/
  238. bool Hashes::Add(const unsigned char * const Data,unsigned long long const Size, unsigned int const Hashes)
  239. {
  240. bool Res = true;
  241. APT_IGNORE_DEPRECATED_PUSH
  242. if ((Hashes & MD5SUM) == MD5SUM)
  243. Res &= MD5.Add(Data, Size);
  244. if ((Hashes & SHA1SUM) == SHA1SUM)
  245. Res &= SHA1.Add(Data, Size);
  246. if ((Hashes & SHA256SUM) == SHA256SUM)
  247. Res &= SHA256.Add(Data, Size);
  248. if ((Hashes & SHA512SUM) == SHA512SUM)
  249. Res &= SHA512.Add(Data, Size);
  250. APT_IGNORE_DEPRECATED_POP
  251. d->FileSize += Size;
  252. return Res;
  253. }
  254. bool Hashes::AddFD(int const Fd,unsigned long long Size, unsigned int const Hashes)
  255. {
  256. unsigned char Buf[64*64];
  257. bool const ToEOF = (Size == UntilEOF);
  258. while (Size != 0 || ToEOF)
  259. {
  260. unsigned long long n = sizeof(Buf);
  261. if (!ToEOF) n = std::min(Size, n);
  262. ssize_t const Res = read(Fd,Buf,n);
  263. if (Res < 0 || (!ToEOF && Res != (ssize_t) n)) // error, or short read
  264. return false;
  265. if (ToEOF && Res == 0) // EOF
  266. break;
  267. Size -= Res;
  268. if (Add(Buf, Res, Hashes) == false)
  269. return false;
  270. }
  271. return true;
  272. }
  273. bool Hashes::AddFD(FileFd &Fd,unsigned long long Size, unsigned int const Hashes)
  274. {
  275. unsigned char Buf[64*64];
  276. bool const ToEOF = (Size == 0);
  277. while (Size != 0 || ToEOF)
  278. {
  279. unsigned long long n = sizeof(Buf);
  280. if (!ToEOF) n = std::min(Size, n);
  281. unsigned long long a = 0;
  282. if (Fd.Read(Buf, n, &a) == false) // error
  283. return false;
  284. if (ToEOF == false)
  285. {
  286. if (a != n) // short read
  287. return false;
  288. }
  289. else if (a == 0) // EOF
  290. break;
  291. Size -= a;
  292. if (Add(Buf, a, Hashes) == false)
  293. return false;
  294. }
  295. return true;
  296. }
  297. /*}}}*/
  298. HashStringList Hashes::GetHashStringList()
  299. {
  300. HashStringList hashes;
  301. APT_IGNORE_DEPRECATED_PUSH
  302. hashes.push_back(HashString("MD5Sum", MD5.Result().Value()));
  303. hashes.push_back(HashString("SHA1", SHA1.Result().Value()));
  304. hashes.push_back(HashString("SHA256", SHA256.Result().Value()));
  305. hashes.push_back(HashString("SHA512", SHA512.Result().Value()));
  306. APT_IGNORE_DEPRECATED_POP
  307. std::string SizeStr;
  308. strprintf(SizeStr, "%llu", d->FileSize);
  309. hashes.push_back(HashString("Checksum-FileSize", SizeStr));
  310. return hashes;
  311. }
  312. APT_IGNORE_DEPRECATED_PUSH
  313. Hashes::Hashes() { d = new PrivateHashes(); }
  314. Hashes::~Hashes() { delete d; }
  315. APT_IGNORE_DEPRECATED_POP