md5.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: md5.h,v 1.6 2001/05/07 05:06:52 jgg Exp $
  4. /* ######################################################################
  5. MD5SumValue - Storage for a MD5Sum
  6. MD5Summation - MD5 Message Digest Algorithm.
  7. This is a C++ interface to a set of MD5Sum functions. The class can
  8. store a MD5Sum in 16 bytes of memory.
  9. A MD5Sum is used to generate a (hopefully) unique 16 byte number for a
  10. block of data. This can be used to gaurd against corruption of a file.
  11. MD5 should not be used for tamper protection, use SHA or something more
  12. secure.
  13. There are two classes because computing a MD5 is not a continual
  14. operation unless 64 byte blocks are used. Also the summation requires an
  15. extra 18*4 bytes to operate.
  16. ##################################################################### */
  17. /*}}}*/
  18. #ifndef APTPKG_MD5_H
  19. #define APTPKG_MD5_H
  20. #ifdef __GNUG__
  21. #pragma interface "apt-pkg/md5.h"
  22. #endif
  23. #include <string>
  24. #include <algorithm>
  25. using std::string;
  26. using std::min;
  27. class MD5Summation;
  28. class MD5SumValue
  29. {
  30. friend class MD5Summation;
  31. unsigned char Sum[4*4];
  32. public:
  33. // Accessors
  34. bool operator ==(const MD5SumValue &rhs) const;
  35. string Value() const;
  36. inline void Value(unsigned char S[16])
  37. {for (int I = 0; I != sizeof(Sum); I++) S[I] = Sum[I];};
  38. inline operator string() const {return Value();};
  39. bool Set(string Str);
  40. inline void Set(unsigned char S[16])
  41. {for (int I = 0; I != sizeof(Sum); I++) Sum[I] = S[I];};
  42. MD5SumValue(string Str);
  43. MD5SumValue();
  44. };
  45. class MD5Summation
  46. {
  47. uint32_t Buf[4];
  48. unsigned char Bytes[2*4];
  49. unsigned char In[16*4];
  50. bool Done;
  51. public:
  52. bool Add(const unsigned char *Data,unsigned long Size);
  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. MD5SumValue Result();
  58. MD5Summation();
  59. };
  60. #endif