hashes.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. /** sets the filesize hash
  79. *
  80. * @param Size of the file
  81. * @return @see #push_back
  82. */
  83. bool FileSize(unsigned long long const Size);
  84. /** check if the given hash type is supported
  85. *
  86. * @param type to check
  87. * @return true if supported, otherwise false
  88. */
  89. static APT_PURE bool supported(char const * const type);
  90. /** add the given #HashString to the list
  91. *
  92. * @param hashString to add
  93. * @return true if the hash is added because it is supported and
  94. * not already a different hash of the same type included, otherwise false
  95. */
  96. bool push_back(const HashString &hashString);
  97. /** @return size of the list of HashStrings */
  98. size_t size() const { return list.size(); }
  99. /** take the 'best' hash and verify file with it
  100. *
  101. * @param filename to verify
  102. * @return true if the file matches the hashsum, otherwise false
  103. */
  104. bool VerifyFile(std::string filename) const;
  105. /** is the list empty ?
  106. *
  107. * @return \b true if the list is empty, otherwise \b false
  108. */
  109. bool empty() const { return list.empty(); }
  110. /** has the list at least one good entry
  111. *
  112. * similar to #empty, but handles forced hashes.
  113. *
  114. * @return if no hash is forced, same result as #empty,
  115. * if one is forced \b true if this has is available, \b false otherwise
  116. */
  117. bool usable() const;
  118. typedef std::vector<HashString>::const_iterator const_iterator;
  119. /** iterator to the first element */
  120. const_iterator begin() const { return list.begin(); }
  121. /** iterator to the end element */
  122. const_iterator end() const { return list.end(); }
  123. /** start fresh with a clear list */
  124. void clear() { list.clear(); }
  125. /** compare two HashStringList for similarity.
  126. *
  127. * Two lists are similar if at least one hashtype is in both lists
  128. * and the hashsum matches. All hashes are checked by default,
  129. * if one doesn't match false is returned regardless of how many
  130. * matched before. If a hash is forced, only this hash is compared,
  131. * all others are ignored.
  132. */
  133. bool operator==(HashStringList const &other) const;
  134. bool operator!=(HashStringList const &other) const;
  135. HashStringList() {}
  136. // simplifying API-compatibility constructors
  137. HashStringList(std::string const &hash) {
  138. if (hash.empty() == false)
  139. list.push_back(HashString(hash));
  140. }
  141. HashStringList(char const * const hash) {
  142. if (hash != NULL && hash[0] != '\0')
  143. list.push_back(HashString(hash));
  144. }
  145. private:
  146. std::vector<HashString> list;
  147. };
  148. class PrivateHashes;
  149. class Hashes
  150. {
  151. PrivateHashes * const d;
  152. public:
  153. /* those will disappear in the future as it is hard to add new ones this way.
  154. * Use Add* to build the results and get them via GetHashStringList() instead */
  155. APT_DEPRECATED MD5Summation MD5;
  156. APT_DEPRECATED SHA1Summation SHA1;
  157. APT_DEPRECATED SHA256Summation SHA256;
  158. APT_DEPRECATED SHA512Summation SHA512;
  159. static const int UntilEOF = 0;
  160. bool Add(const unsigned char * const Data, unsigned long long const Size);
  161. APT_DEPRECATED bool Add(const unsigned char * const Data, unsigned long long const Size, unsigned int const Hashes);
  162. inline bool Add(const char * const Data)
  163. {return Add((unsigned char const * const)Data,strlen(Data));};
  164. inline bool Add(const unsigned char * const Beg,const unsigned char * const End)
  165. {return Add(Beg,End-Beg);};
  166. enum SupportedHashes { MD5SUM = (1 << 0), SHA1SUM = (1 << 1), SHA256SUM = (1 << 2),
  167. SHA512SUM = (1 << 3) };
  168. bool AddFD(int const Fd,unsigned long long Size = 0);
  169. APT_DEPRECATED bool AddFD(int const Fd,unsigned long long Size, unsigned int const Hashes);
  170. bool AddFD(FileFd &Fd,unsigned long long Size = 0);
  171. APT_DEPRECATED bool AddFD(FileFd &Fd,unsigned long long Size, unsigned int const Hashes);
  172. HashStringList GetHashStringList();
  173. APT_IGNORE_DEPRECATED_PUSH
  174. /** create a Hashes object to calculate all supported hashes
  175. *
  176. * If ALL is too much, you can limit which Hashes are calculated
  177. * with the following other constructors which mention explicitly
  178. * which hashes to generate. */
  179. Hashes();
  180. /** @param Hashes bitflag composed of #SupportedHashes */
  181. Hashes(unsigned int const Hashes);
  182. /** @param Hashes is a list of hashes */
  183. Hashes(HashStringList const &Hashes);
  184. virtual ~Hashes();
  185. APT_IGNORE_DEPRECATED_POP
  186. private:
  187. APT_HIDDEN APT_CONST inline unsigned int boolsToFlag(bool const addMD5, bool const addSHA1, bool const addSHA256, bool const addSHA512)
  188. {
  189. unsigned int Hashes = ~0;
  190. if (addMD5 == false) Hashes &= ~MD5SUM;
  191. if (addSHA1 == false) Hashes &= ~SHA1SUM;
  192. if (addSHA256 == false) Hashes &= ~SHA256SUM;
  193. if (addSHA512 == false) Hashes &= ~SHA512SUM;
  194. return Hashes;
  195. }
  196. public:
  197. APT_IGNORE_DEPRECATED_PUSH
  198. APT_DEPRECATED bool AddFD(int const Fd, unsigned long long Size, bool const addMD5,
  199. bool const addSHA1, bool const addSHA256, bool const addSHA512) {
  200. return AddFD(Fd, Size, boolsToFlag(addMD5, addSHA1, addSHA256, addSHA512));
  201. };
  202. APT_DEPRECATED bool AddFD(FileFd &Fd, unsigned long long Size, bool const addMD5,
  203. bool const addSHA1, bool const addSHA256, bool const addSHA512) {
  204. return AddFD(Fd, Size, boolsToFlag(addMD5, addSHA1, addSHA256, addSHA512));
  205. };
  206. APT_IGNORE_DEPRECATED_POP
  207. };
  208. #endif