hashes.h 1.8 KB

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