hashes.h 6.9 KB

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