md5.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: md5.h,v 1.3 1999/01/18 06:20:08 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. There are two classes because computing a MD5 is not a continual
  12. operation unless 64 byte blocks are used. Also the summation requires an
  13. extra 18*4 bytes to operate.
  14. ##################################################################### */
  15. /*}}}*/
  16. #ifndef APTPKG_MD5_H
  17. #define APTPKG_MD5_H
  18. #ifdef __GNUG__
  19. #pragma interface "apt-pkg/md5.h"
  20. #endif
  21. #include <string>
  22. class MD5Summation;
  23. class MD5SumValue
  24. {
  25. friend MD5Summation;
  26. unsigned char Sum[4*4];
  27. public:
  28. // Accessors
  29. bool operator ==(const MD5SumValue &rhs) const;
  30. string Value() const;
  31. inline void Value(unsigned char S[16])
  32. {for (int I = 0; I != sizeof(Sum); I++) S[I] = Sum[I];};
  33. inline operator string() const {return Value();};
  34. bool Set(string Str);
  35. inline void Set(unsigned char S[16])
  36. {for (int I = 0; I != sizeof(Sum); I++) Sum[I] = S[I];};
  37. MD5SumValue(string Str);
  38. MD5SumValue();
  39. };
  40. class MD5Summation
  41. {
  42. unsigned char Buf[4*4];
  43. unsigned char Bytes[2*4];
  44. unsigned char In[16*4];
  45. bool Done;
  46. public:
  47. bool Add(const unsigned char *Data,unsigned long Size);
  48. inline bool Add(const char *Data) {return Add((unsigned char *)Data,strlen(Data));};
  49. bool AddFD(int Fd,unsigned long Size);
  50. inline bool Add(const unsigned char *Beg,const unsigned char *End)
  51. {return Add(Beg,End-Beg);};
  52. MD5SumValue Result();
  53. MD5Summation();
  54. };
  55. #endif