MD5.hpp
1 //
2 // Created by xiong on 2022/6/9.
3 //
4 
5 #ifndef EMX_MD5_HPP
6 #define EMX_MD5_HPP
7 
8 #include <string>
9 
10 namespace Emx {
15  class MD5 {
16  public:
17  typedef unsigned int size_type;// must be 32bit
18 
20  MD5();
21 
23  MD5(const std::string &text);
24 
30  void update(const unsigned char *buf, size_type length);
31 
37  void update(const char *buf, size_type length);
38 
41 
43  std::string hexdigest() const;
44 
45  friend std::ostream &operator<<(std::ostream &, MD5 md5);
46 
47  private:
48  void init();
49 
50  typedef unsigned char uint1;// 8bit
51  typedef unsigned int uint4; // 32bit
52  enum { blocksize = 64 }; // VC6 won't eat a const static int here
53 
54  void transform(const uint1 block[blocksize]);
55 
56  static void decode(uint4 output[], const uint1 input[], size_type len);
57 
58  static void encode(uint1 output[], const uint4 input[], size_type len);
59 
60  bool finalized;
61  uint1 buffer[blocksize];// bytes that didn't fit in last 64 byte chunk
62  uint4 count[2]; // 64bit counter for number of bits (lo, hi)
63  uint4 state[4]; // digest so far
64  uint1 digest[16]; // the result
65 
66  // low level logic operations
67  static inline uint4 F(uint4 x, uint4 y, uint4 z);
68 
69  static inline uint4 G(uint4 x, uint4 y, uint4 z);
70 
71  static inline uint4 H(uint4 x, uint4 y, uint4 z);
72 
73  static inline uint4 I(uint4 x, uint4 y, uint4 z);
74 
75  static inline uint4 rotate_left(uint4 x, int n);
76 
77  static inline void FF(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
78 
79  static inline void GG(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
80 
81  static inline void HH(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
82 
83  static inline void II(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
84  };
85  class FileMd5 {
86  public:
87  static std::string Get(const char *path);
88  };
89 
91 }// namespace Emx
92 
93 #endif//EMX_MD5_HPP
Definition: MD5.hpp:85
static std::string Get(const char *path)
计算MD5
Definition: MD5.hpp:15
MD5 & finalize()
生成最终MD5
friend std::ostream & operator<<(std::ostream &, MD5 md5)
std::string hexdigest() const
MD5二进制数据转字符串输出
MD5(const std::string &text)
使用特定数据初始化MD5上下文
unsigned int size_type
Definition: MD5.hpp:17
void update(const char *buf, size_type length)
使用新的数据更新MD5
void update(const unsigned char *buf, size_type length)
使用新的数据更新MD5
MD5()
初始化MD5上下文
Definition: EmxGpio.hpp:10