hashes.h 2.2 KB

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