hashes.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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() { return Type; };
  44. std::string HashType() const { return Type; };
  45. std::string HashValue() const { return Hash; };
  46. // verify the given filename against the currently loaded hash
  47. bool VerifyFile(std::string filename) const;
  48. // generate a hash string from the given filename
  49. bool FromFile(std::string filename);
  50. // helper
  51. std::string toStr() const; // convert to str as "type:hash"
  52. bool empty() const;
  53. bool operator==(HashString const &other) const;
  54. bool operator!=(HashString const &other) const;
  55. // return the list of hashes we support
  56. static APT_CONST const char** SupportedHashes();
  57. };
  58. class HashStringList
  59. {
  60. public:
  61. /** find best hash if no specific one is requested
  62. *
  63. * @param type of the checksum to return, can be \b NULL
  64. * @return If type is \b NULL (or the empty string) it will
  65. * return the 'best' hash; otherwise the hash which was
  66. * specifically requested. If no hash is found \b NULL will be returned.
  67. */
  68. HashString const * find(char const * const type) const;
  69. HashString const * find(std::string const &type) const { return find(type.c_str()); }
  70. /** check if the given hash type is supported
  71. *
  72. * @param type to check
  73. * @return true if supported, otherwise false
  74. */
  75. static APT_PURE bool supported(char const * const type);
  76. /** add the given #HashString to the list
  77. *
  78. * @param hashString to add
  79. * @return true if the hash is added because it is supported and
  80. * not already a different hash of the same type included, otherwise false
  81. */
  82. bool push_back(const HashString &hashString);
  83. /** @return size of the list of HashStrings */
  84. size_t size() const { return list.size(); }
  85. /** take the 'best' hash and verify file with it
  86. *
  87. * @param filename to verify
  88. * @return true if the file matches the hashsum, otherwise false
  89. */
  90. bool VerifyFile(std::string filename) const;
  91. /** is the list empty ?
  92. *
  93. * @return \b true if the list is empty, otherwise \b false
  94. */
  95. bool empty() const { return list.empty(); }
  96. typedef std::vector<HashString>::const_iterator const_iterator;
  97. /** iterator to the first element */
  98. const_iterator begin() const { return list.begin(); }
  99. /** iterator to the end element */
  100. const_iterator end() const { return list.end(); }
  101. /** start fresh with a clear list */
  102. void clear() { list.clear(); }
  103. /** compare two HashStringList for similarity.
  104. *
  105. * Two lists are similar if at least one hashtype is in both lists
  106. * and the hashsum matches. All hashes are checked, if one doesn't
  107. * match false is returned regardless of how many matched before.
  108. */
  109. bool operator==(HashStringList const &other) const;
  110. bool operator!=(HashStringList const &other) const;
  111. HashStringList() {}
  112. // simplifying API-compatibility constructors
  113. HashStringList(std::string const &hash) {
  114. if (hash.empty() == false)
  115. list.push_back(HashString(hash));
  116. }
  117. HashStringList(char const * const hash) {
  118. if (hash != NULL && hash[0] != '\0')
  119. list.push_back(HashString(hash));
  120. }
  121. private:
  122. std::vector<HashString> list;
  123. };
  124. class Hashes
  125. {
  126. public:
  127. MD5Summation MD5;
  128. SHA1Summation SHA1;
  129. SHA256Summation SHA256;
  130. SHA512Summation SHA512;
  131. static const int UntilEOF = 0;
  132. inline bool Add(const unsigned char *Data,unsigned long long Size)
  133. {
  134. return MD5.Add(Data,Size) && SHA1.Add(Data,Size) && SHA256.Add(Data,Size) && SHA512.Add(Data,Size);
  135. };
  136. inline bool Add(const char *Data) {return Add((unsigned char const *)Data,strlen(Data));};
  137. inline bool AddFD(int const Fd,unsigned long long Size = 0)
  138. { return AddFD(Fd, Size, true, true, true, true); };
  139. bool AddFD(int const Fd, unsigned long long Size, bool const addMD5,
  140. bool const addSHA1, bool const addSHA256, bool const addSHA512);
  141. inline bool AddFD(FileFd &Fd,unsigned long long Size = 0)
  142. { return AddFD(Fd, Size, true, true, true, true); };
  143. bool AddFD(FileFd &Fd, unsigned long long Size, bool const addMD5,
  144. bool const addSHA1, bool const addSHA256, bool const addSHA512);
  145. inline bool Add(const unsigned char *Beg,const unsigned char *End)
  146. {return Add(Beg,End-Beg);};
  147. };
  148. #endif