hashes.h 2.6 KB

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