md5.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: md5.h,v 1.4 1999/10/24 06:53:12 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 "dsync/md5.h"
  22. #endif
  23. #include <string>
  24. #include <string.h>
  25. using namespace std;
  26. class MD5Summation;
  27. class MD5SumValue
  28. {
  29. friend class MD5Summation;
  30. unsigned char Sum[4*4];
  31. public:
  32. // Accessors
  33. bool operator ==(const MD5SumValue &rhs) const;
  34. string Value() const;
  35. inline void Value(unsigned char S[16])
  36. {for (int I = 0; I != sizeof(Sum); I++) S[I] = Sum[I];};
  37. inline operator string() const {return Value();};
  38. bool Set(string Str);
  39. inline void Set(unsigned char S[16])
  40. {for (int I = 0; I != sizeof(Sum); I++) Sum[I] = S[I];};
  41. MD5SumValue(string Str);
  42. MD5SumValue();
  43. };
  44. class MD5Summation
  45. {
  46. unsigned char Buf[4*4];
  47. unsigned char Bytes[2*4];
  48. unsigned char In[16*4];
  49. bool Done;
  50. public:
  51. bool Add(const unsigned char *Data,unsigned long Size);
  52. inline bool Add(const char *Data) {return Add((unsigned char *)Data,strlen(Data));};
  53. bool AddFD(int Fd,unsigned long Size);
  54. inline bool Add(const unsigned char *Beg,const unsigned char *End)
  55. {return Add(Beg,End-Beg);};
  56. MD5SumValue Result();
  57. MD5Summation();
  58. };
  59. #endif