md5.h 1.2 KB

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