hashes.h 1.8 KB

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