hashes.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 <cstring>
  16. #include <string>
  17. #ifndef APT_8_CLEANER_HEADERS
  18. using std::min;
  19. using std::vector;
  20. #endif
  21. #ifndef APT_10_CLEANER_HEADERS
  22. #include <apt-pkg/fileutl.h>
  23. #include <algorithm>
  24. #include <vector>
  25. #endif
  26. class FileFd;
  27. // helper class that contains hash function name
  28. // and hash
  29. class HashString
  30. {
  31. protected:
  32. std::string Type;
  33. std::string Hash;
  34. static const char* _SupportedHashes[10];
  35. // internal helper
  36. std::string GetHashForFile(std::string filename) const;
  37. public:
  38. HashString(std::string Type, std::string Hash);
  39. HashString(std::string StringedHashString); // init from str as "type:hash"
  40. HashString();
  41. // get hash type used
  42. std::string HashType() { return Type; };
  43. // verify the given filename against the currently loaded hash
  44. bool VerifyFile(std::string filename) const;
  45. // generate a hash string from the given filename
  46. bool FromFile(std::string filename);
  47. // helper
  48. std::string toStr() const; // convert to str as "type:hash"
  49. bool empty() const;
  50. // return the list of hashes we support
  51. static APT_CONST const char** SupportedHashes();
  52. };
  53. class Hashes
  54. {
  55. public:
  56. MD5Summation MD5;
  57. SHA1Summation SHA1;
  58. SHA256Summation SHA256;
  59. SHA512Summation SHA512;
  60. static const int UntilEOF = 0;
  61. inline bool Add(const unsigned char *Data,unsigned long long Size)
  62. {
  63. return MD5.Add(Data,Size) && SHA1.Add(Data,Size) && SHA256.Add(Data,Size) && SHA512.Add(Data,Size);
  64. };
  65. inline bool Add(const char *Data) {return Add((unsigned char const *)Data,strlen(Data));};
  66. inline bool AddFD(int const Fd,unsigned long long Size = 0)
  67. { return AddFD(Fd, Size, true, true, true, true); };
  68. bool AddFD(int const Fd, unsigned long long Size, bool const addMD5,
  69. bool const addSHA1, bool const addSHA256, bool const addSHA512);
  70. inline bool AddFD(FileFd &Fd,unsigned long long Size = 0)
  71. { return AddFD(Fd, Size, true, true, true, true); };
  72. bool AddFD(FileFd &Fd, unsigned long long Size, bool const addMD5,
  73. bool const addSHA1, bool const addSHA256, bool const addSHA512);
  74. inline bool Add(const unsigned char *Beg,const unsigned char *End)
  75. {return Add(Beg,End-Beg);};
  76. };
  77. #endif