hashes.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. unsigned int CalcHashes;
  235. PrivateHashes(unsigned int const CalcHashes) : FileSize(0), CalcHashes(CalcHashes) {}
  236. };
  237. /*}}}*/
  238. // Hashes::Add* - Add the contents of data or FD /*{{{*/
  239. bool Hashes::Add(const unsigned char * const Data, unsigned long long const Size)
  240. {
  241. bool Res = true;
  242. APT_IGNORE_DEPRECATED_PUSH
  243. if ((d->CalcHashes & MD5SUM) == MD5SUM)
  244. Res &= MD5.Add(Data, Size);
  245. if ((d->CalcHashes & SHA1SUM) == SHA1SUM)
  246. Res &= SHA1.Add(Data, Size);
  247. if ((d->CalcHashes & SHA256SUM) == SHA256SUM)
  248. Res &= SHA256.Add(Data, Size);
  249. if ((d->CalcHashes & SHA512SUM) == SHA512SUM)
  250. Res &= SHA512.Add(Data, Size);
  251. APT_IGNORE_DEPRECATED_POP
  252. d->FileSize += Size;
  253. return Res;
  254. }
  255. bool Hashes::Add(const unsigned char * const Data, unsigned long long const Size, unsigned int const Hashes)
  256. {
  257. d->CalcHashes = Hashes;
  258. return Add(Data, Size);
  259. }
  260. bool Hashes::AddFD(int const Fd,unsigned long long Size)
  261. {
  262. unsigned char Buf[64*64];
  263. bool const ToEOF = (Size == UntilEOF);
  264. while (Size != 0 || ToEOF)
  265. {
  266. unsigned long long n = sizeof(Buf);
  267. if (!ToEOF) n = std::min(Size, n);
  268. ssize_t const Res = read(Fd,Buf,n);
  269. if (Res < 0 || (!ToEOF && Res != (ssize_t) n)) // error, or short read
  270. return false;
  271. if (ToEOF && Res == 0) // EOF
  272. break;
  273. Size -= Res;
  274. if (Add(Buf, Res) == false)
  275. return false;
  276. }
  277. return true;
  278. }
  279. bool Hashes::AddFD(int const Fd,unsigned long long Size, unsigned int const Hashes)
  280. {
  281. d->CalcHashes = Hashes;
  282. return AddFD(Fd, Size);
  283. }
  284. bool Hashes::AddFD(FileFd &Fd,unsigned long long Size)
  285. {
  286. unsigned char Buf[64*64];
  287. bool const ToEOF = (Size == 0);
  288. while (Size != 0 || ToEOF)
  289. {
  290. unsigned long long n = sizeof(Buf);
  291. if (!ToEOF) n = std::min(Size, n);
  292. unsigned long long a = 0;
  293. if (Fd.Read(Buf, n, &a) == false) // error
  294. return false;
  295. if (ToEOF == false)
  296. {
  297. if (a != n) // short read
  298. return false;
  299. }
  300. else if (a == 0) // EOF
  301. break;
  302. Size -= a;
  303. if (Add(Buf, a) == false)
  304. return false;
  305. }
  306. return true;
  307. }
  308. bool Hashes::AddFD(FileFd &Fd,unsigned long long Size, unsigned int const Hashes)
  309. {
  310. d->CalcHashes = Hashes;
  311. return AddFD(Fd, Size);
  312. }
  313. /*}}}*/
  314. HashStringList Hashes::GetHashStringList()
  315. {
  316. HashStringList hashes;
  317. APT_IGNORE_DEPRECATED_PUSH
  318. if ((d->CalcHashes & MD5SUM) == MD5SUM)
  319. hashes.push_back(HashString("MD5Sum", MD5.Result().Value()));
  320. if ((d->CalcHashes & SHA1SUM) == SHA1SUM)
  321. hashes.push_back(HashString("SHA1", SHA1.Result().Value()));
  322. if ((d->CalcHashes & SHA256SUM) == SHA256SUM)
  323. hashes.push_back(HashString("SHA256", SHA256.Result().Value()));
  324. if ((d->CalcHashes & SHA512SUM) == SHA512SUM)
  325. hashes.push_back(HashString("SHA512", SHA512.Result().Value()));
  326. APT_IGNORE_DEPRECATED_POP
  327. std::string SizeStr;
  328. strprintf(SizeStr, "%llu", d->FileSize);
  329. hashes.push_back(HashString("Checksum-FileSize", SizeStr));
  330. return hashes;
  331. }
  332. APT_IGNORE_DEPRECATED_PUSH
  333. Hashes::Hashes() { d = new PrivateHashes(~0); }
  334. Hashes::Hashes(unsigned int const Hashes) { d = new PrivateHashes(Hashes); }
  335. Hashes::Hashes(HashStringList const &Hashes) {
  336. unsigned int calcHashes = Hashes.usable() ? 0 : ~0;
  337. if (Hashes.find("MD5Sum") != NULL)
  338. calcHashes |= MD5SUM;
  339. if (Hashes.find("SHA1") != NULL)
  340. calcHashes |= SHA1SUM;
  341. if (Hashes.find("SHA256") != NULL)
  342. calcHashes |= SHA256SUM;
  343. if (Hashes.find("SHA512") != NULL)
  344. calcHashes |= SHA512SUM;
  345. d = new PrivateHashes(calcHashes);
  346. }
  347. Hashes::~Hashes() { delete d; }
  348. APT_IGNORE_DEPRECATED_POP