md5.h 2.1 KB

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