linux版本中间件

Md5.h 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #pragma once
  2. #ifndef MD5_H
  3. #define MD5_H
  4. #include <string>
  5. #include<string.h>
  6. #include <fstream>
  7. /* Type define */
  8. typedef unsigned char byte;
  9. typedef unsigned long ulong;
  10. /* MD5 declaration. */
  11. namespace md5space {
  12. class MD5 {
  13. public:
  14. MD5();
  15. MD5(const void *input, size_t length);
  16. MD5(const std::string &str);
  17. MD5(std::ifstream &in);
  18. void update(const void *input, size_t length);
  19. void update(const std::string &str);
  20. void update(std::ifstream &in);
  21. const byte* digest();
  22. std::string toString();
  23. void reset();
  24. std::string getMd5();
  25. private:
  26. void update(const byte *input, size_t length);
  27. void final();
  28. void transform(const byte block[64]);
  29. void encode(const ulong *input, byte *output, size_t length);
  30. void decode(const byte *input, ulong *output, size_t length);
  31. std::string bytesToHexString(const byte *input, size_t length);
  32. /* class uncopyable */
  33. MD5(const MD5&);
  34. MD5& operator=(const MD5&) = default;
  35. private:
  36. ulong _state[4]; /* state (ABCD) */
  37. ulong _count[2]; /* number of bits, modulo 2^64 (low-order word first) */
  38. byte _buffer[64]; /* input buffer */
  39. byte _digest[16]; /* message digest */
  40. bool _finished; /* calculate finished ? */
  41. static const byte PADDING[64]; /* padding for calculate */
  42. static const char HEX[16];
  43. static const size_t BUFFER_SIZE = 1024;
  44. };
  45. }
  46. #endif/*MD5_H*/