md5.h 2.0 KB

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