hashes.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: hashes.h,v 1.2 2001/03/11 05:30:20 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. #ifndef APTPKG_HASHES_H
  11. #define APTPKG_HASHES_H
  12. #include <apt-pkg/md5.h>
  13. #include <apt-pkg/sha1.h>
  14. #include <apt-pkg/sha2.h>
  15. #include <apt-pkg/macros.h>
  16. #include <cstring>
  17. #include <string>
  18. #ifndef APT_8_CLEANER_HEADERS
  19. using std::min;
  20. using std::vector;
  21. #endif
  22. #ifndef APT_10_CLEANER_HEADERS
  23. #include <apt-pkg/fileutl.h>
  24. #include <algorithm>
  25. #include <vector>
  26. #endif
  27. class FileFd;
  28. // helper class that contains hash function name
  29. // and hash
  30. class HashString
  31. {
  32. protected:
  33. std::string Type;
  34. std::string Hash;
  35. static const char * _SupportedHashes[10];
  36. // internal helper
  37. std::string GetHashForFile(std::string filename) const;
  38. public:
  39. HashString(std::string Type, std::string Hash);
  40. HashString(std::string StringedHashString); // init from str as "type:hash"
  41. HashString();
  42. // get hash type used
  43. std::string HashType() const { return Type; };
  44. std::string HashValue() const { return Hash; };
  45. APT_DEPRECATED std::string HashType() { return Type; };
  46. APT_DEPRECATED std::string HashValue() { return Hash; };
  47. // verify the given filename against the currently loaded hash
  48. bool VerifyFile(std::string filename) const;
  49. // generate a hash string from the given filename
  50. bool FromFile(std::string filename);
  51. // helper
  52. std::string toStr() const; // convert to str as "type:hash"
  53. bool empty() const;
  54. bool operator==(HashString const &other) const;
  55. bool operator!=(HashString const &other) const;
  56. // return the list of hashes we support
  57. static APT_CONST const char** SupportedHashes();
  58. };
  59. class HashStringList
  60. {
  61. public:
  62. /** find best hash if no specific one is requested
  63. *
  64. * @param type of the checksum to return, can be \b NULL
  65. * @return If type is \b NULL (or the empty string) it will
  66. * return the 'best' hash; otherwise the hash which was
  67. * specifically requested. If no hash is found \b NULL will be returned.
  68. */
  69. HashString const * find(char const * const type) const;
  70. HashString const * find(std::string const &type) const { return find(type.c_str()); }
  71. /** finds the filesize hash and returns it as number
  72. *
  73. * @return beware: if the size isn't known we return \b 0 here,
  74. * just like we would do for an empty file. If that is a problem
  75. * for you have to get the size manually out of the list.
  76. */
  77. unsigned long long FileSize() const;
  78. /** check if the given hash type is supported
  79. *
  80. * @param type to check
  81. * @return true if supported, otherwise false
  82. */
  83. static APT_PURE bool supported(char const * const type);
  84. /** add the given #HashString to the list
  85. *
  86. * @param hashString to add
  87. * @return true if the hash is added because it is supported and
  88. * not already a different hash of the same type included, otherwise false
  89. */
  90. bool push_back(const HashString &hashString);
  91. /** @return size of the list of HashStrings */
  92. size_t size() const { return list.size(); }
  93. /** take the 'best' hash and verify file with it
  94. *
  95. * @param filename to verify
  96. * @return true if the file matches the hashsum, otherwise false
  97. */
  98. bool VerifyFile(std::string filename) const;
  99. /** is the list empty ?
  100. *
  101. * @return \b true if the list is empty, otherwise \b false
  102. */
  103. bool empty() const { return list.empty(); }
  104. /** has the list at least one good entry
  105. *
  106. * similar to #empty, but handles forced hashes.
  107. *
  108. * @return if no hash is forced, same result as #empty,
  109. * if one is forced \b true if this has is available, \b false otherwise
  110. */
  111. bool usable() const;
  112. typedef std::vector<HashString>::const_iterator const_iterator;
  113. /** iterator to the first element */
  114. const_iterator begin() const { return list.begin(); }
  115. /** iterator to the end element */
  116. const_iterator end() const { return list.end(); }
  117. /** start fresh with a clear list */
  118. void clear() { list.clear(); }
  119. /** compare two HashStringList for similarity.
  120. *
  121. * Two lists are similar if at least one hashtype is in both lists
  122. * and the hashsum matches. All hashes are checked by default,
  123. * if one doesn't match false is returned regardless of how many
  124. * matched before. If a hash is forced, only this hash is compared,
  125. * all others are ignored.
  126. */
  127. bool operator==(HashStringList const &other) const;
  128. bool operator!=(HashStringList const &other) const;
  129. HashStringList() {}
  130. // simplifying API-compatibility constructors
  131. HashStringList(std::string const &hash) {
  132. if (hash.empty() == false)
  133. list.push_back(HashString(hash));
  134. }
  135. HashStringList(char const * const hash) {
  136. if (hash != NULL && hash[0] != '\0')
  137. list.push_back(HashString(hash));
  138. }
  139. private:
  140. std::vector<HashString> list;
  141. };
  142. class PrivateHashes;
  143. class Hashes
  144. {
  145. PrivateHashes *d;
  146. public:
  147. /* those will disappear in the future as it is hard to add new ones this way.
  148. * Use Add* to build the results and get them via GetHashStringList() instead */
  149. APT_DEPRECATED MD5Summation MD5;
  150. APT_DEPRECATED SHA1Summation SHA1;
  151. APT_DEPRECATED SHA256Summation SHA256;
  152. APT_DEPRECATED SHA512Summation SHA512;
  153. static const int UntilEOF = 0;
  154. bool Add(const unsigned char * const Data, unsigned long long const Size);
  155. APT_DEPRECATED bool Add(const unsigned char * const Data, unsigned long long const Size, unsigned int const Hashes);
  156. inline bool Add(const char * const Data)
  157. {return Add((unsigned char const * const)Data,strlen(Data));};
  158. inline bool Add(const unsigned char * const Beg,const unsigned char * const End)
  159. {return Add(Beg,End-Beg);};
  160. enum SupportedHashes { MD5SUM = (1 << 0), SHA1SUM = (1 << 1), SHA256SUM = (1 << 2),
  161. SHA512SUM = (1 << 3) };
  162. bool AddFD(int const Fd,unsigned long long Size = 0);
  163. APT_DEPRECATED bool AddFD(int const Fd,unsigned long long Size, unsigned int const Hashes);
  164. bool AddFD(FileFd &Fd,unsigned long long Size = 0);
  165. APT_DEPRECATED bool AddFD(FileFd &Fd,unsigned long long Size, unsigned int const Hashes);
  166. HashStringList GetHashStringList();
  167. APT_IGNORE_DEPRECATED_PUSH
  168. /** create a Hashes object to calculate all supported hashes
  169. *
  170. * If ALL is too much, you can limit which Hashes are calculated
  171. * with the following other constructors which mention explicitly
  172. * which hashes to generate. */
  173. Hashes();
  174. /** @param Hashes bitflag composed of #SupportedHashes */
  175. Hashes(unsigned int const Hashes);
  176. /** @param Hashes is a list of hashes */
  177. Hashes(HashStringList const &Hashes);
  178. virtual ~Hashes();
  179. APT_IGNORE_DEPRECATED_POP
  180. private:
  181. APT_HIDDEN APT_CONST inline unsigned int boolsToFlag(bool const addMD5, bool const addSHA1, bool const addSHA256, bool const addSHA512)
  182. {
  183. unsigned int Hashes = ~0;
  184. if (addMD5 == false) Hashes &= ~MD5SUM;
  185. if (addSHA1 == false) Hashes &= ~SHA1SUM;
  186. if (addSHA256 == false) Hashes &= ~SHA256SUM;
  187. if (addSHA512 == false) Hashes &= ~SHA512SUM;
  188. return Hashes;
  189. }
  190. public:
  191. APT_IGNORE_DEPRECATED_PUSH
  192. APT_DEPRECATED bool AddFD(int const Fd, unsigned long long Size, bool const addMD5,
  193. bool const addSHA1, bool const addSHA256, bool const addSHA512) {
  194. return AddFD(Fd, Size, boolsToFlag(addMD5, addSHA1, addSHA256, addSHA512));
  195. };
  196. APT_DEPRECATED bool AddFD(FileFd &Fd, unsigned long long Size, bool const addMD5,
  197. bool const addSHA1, bool const addSHA256, bool const addSHA512) {
  198. return AddFD(Fd, Size, boolsToFlag(addMD5, addSHA1, addSHA256, addSHA512));
  199. };
  200. APT_IGNORE_DEPRECATED_POP
  201. };
  202. #endif