md5.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // MD5.h
  2. #ifndef BZF_MD5_H
  3. #define BZF_MD5_H
  4. #include <iostream>
  5. #include <string>
  6. #include "base/buffer.h"
  7. namespace ylib
  8. {
  9. class MD5
  10. {
  11. public:
  12. typedef unsigned int size_type; // must be 32bit
  13. MD5();
  14. MD5(const ylib::buffer& value);
  15. void update(const unsigned char* buf, size_type length);
  16. void update(const char* buf, size_type length);
  17. MD5& finalize();
  18. std::string hexdigest() const;
  19. friend std::ostream& operator<<(std::ostream&, MD5 md5);
  20. private:
  21. void init();
  22. typedef unsigned char uint1; // 8bit
  23. typedef unsigned int uint4; // 32bit
  24. enum { blocksize = 64 }; // VC6 won't eat a const static int here
  25. void transform(const uint1 block[blocksize]);
  26. static void decode(uint4 output[], const uint1 input[], size_type len);
  27. static void encode(uint1 output[], const uint4 input[], size_type len);
  28. bool finalized;
  29. uint1 buffer[blocksize]; // bytes that didn't fit in last 64 byte chunk
  30. uint4 count[2]; // 64bit counter for number of bits (lo, hi)
  31. uint4 state[4]; // digest so far
  32. uint1 digest[16]; // the result
  33. // low level logic operations
  34. static inline uint4 F(uint4 x, uint4 y, uint4 z);
  35. static inline uint4 G(uint4 x, uint4 y, uint4 z);
  36. static inline uint4 H(uint4 x, uint4 y, uint4 z);
  37. static inline uint4 I(uint4 x, uint4 y, uint4 z);
  38. static inline uint4 rotate_left(uint4 x, int n);
  39. static inline void FF(uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
  40. static inline void GG(uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
  41. static inline void HH(uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
  42. static inline void II(uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
  43. };
  44. }
  45. #endif