hashes.h 1.9 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. 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. SHA512Summation SHA512;
  49. inline bool Add(const unsigned char *Data,unsigned long Size)
  50. {
  51. return MD5.Add(Data,Size) && SHA1.Add(Data,Size) && SHA256.Add(Data,Size) && SHA512.Add(Data,Size);
  52. };
  53. inline bool Add(const char *Data) {return Add((unsigned char *)Data,strlen(Data));};
  54. bool AddFD(int Fd,unsigned long Size);
  55. inline bool Add(const unsigned char *Beg,const unsigned char *End)
  56. {return Add(Beg,End-Beg);};
  57. };
  58. #endif