md5.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. using std::string;
  25. class MD5Summation;
  26. class MD5SumValue
  27. {
  28. friend class MD5Summation;
  29. unsigned char Sum[4*4];
  30. public:
  31. // Accessors
  32. bool operator ==(const MD5SumValue &rhs) const;
  33. string Value() const;
  34. inline void Value(unsigned char S[16])
  35. {for (int I = 0; I != sizeof(Sum); I++) S[I] = Sum[I];};
  36. inline operator string() const {return Value();};
  37. bool Set(string Str);
  38. inline void Set(unsigned char S[16])
  39. {for (int I = 0; I != sizeof(Sum); I++) Sum[I] = S[I];};
  40. MD5SumValue(string Str);
  41. MD5SumValue();
  42. };
  43. class MD5Summation
  44. {
  45. unsigned char Buf[4*4];
  46. unsigned char Bytes[2*4];
  47. unsigned char In[16*4];
  48. bool Done;
  49. public:
  50. bool Add(const unsigned char *Data,unsigned long Size);
  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. MD5SumValue Result();
  56. MD5Summation();
  57. };
  58. #endif