hashes.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. public:
  32. HashString(std::string Type, std::string Hash);
  33. HashString(std::string StringedHashString); // init from str as "type:hash"
  34. HashString();
  35. // get hash type used
  36. std::string HashType() { return Type; };
  37. // verify the given filename against the currently loaded hash
  38. bool VerifyFile(std::string filename) const;
  39. // helper
  40. std::string toStr() const; // convert to str as "type:hash"
  41. bool empty() const;
  42. // return the list of hashes we support
  43. static const char** SupportedHashes();
  44. };
  45. class Hashes
  46. {
  47. public:
  48. MD5Summation MD5;
  49. SHA1Summation SHA1;
  50. SHA256Summation SHA256;
  51. SHA512Summation SHA512;
  52. inline bool Add(const unsigned char *Data,unsigned long long Size)
  53. {
  54. return MD5.Add(Data,Size) && SHA1.Add(Data,Size) && SHA256.Add(Data,Size) && SHA512.Add(Data,Size);
  55. };
  56. inline bool Add(const char *Data) {return Add((unsigned char *)Data,strlen(Data));};
  57. inline bool AddFD(int const Fd,unsigned long long Size = 0)
  58. { return AddFD(Fd, Size, true, true, true, true); };
  59. bool AddFD(int const Fd, unsigned long long Size, bool const addMD5,
  60. bool const addSHA1, bool const addSHA256, bool const addSHA512);
  61. inline bool AddFD(FileFd &Fd,unsigned long long Size = 0)
  62. { return AddFD(Fd, Size, true, true, true, true); };
  63. bool AddFD(FileFd &Fd, unsigned long long Size, bool const addMD5,
  64. bool const addSHA1, bool const addSHA256, bool const addSHA512);
  65. inline bool Add(const unsigned char *Beg,const unsigned char *End)
  66. {return Add(Beg,End-Beg);};
  67. };
  68. #endif