hashes.h 2.1 KB

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