fastdo  0.6.8
md5.hpp
浏览该文件的文档.
1 /* MD5
2  converted to C++ class by Frank Thilo (thilo@unix-ag.org)
3  for bzflag (http://www.bzflag.org)
4 
5  based on:
6 
7  md5.h and md5.c
8  reference implementation of RFC 1321
9 
10  Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
11 rights reserved.
12 
13 License to copy and use this software is granted provided that it
14 is identified as the "RSA Data Security, Inc. MD5 Message-Digest
15 Algorithm" in all material mentioning or referencing this software
16 or this function.
17 
18 License is also granted to make and use derivative works provided
19 that such works are identified as "derived from the RSA Data
20 Security, Inc. MD5 Message-Digest Algorithm" in all material
21 mentioning or referencing the derived work.
22 
23 RSA Data Security, Inc. makes no representations concerning either
24 the merchantability of this software or the suitability of this
25 software for any particular purpose. It is provided "as is"
26 without express or implied warranty of any kind.
27 
28 These notices must be retained in any copies of any part of this
29 documentation and/or software.
30 
31 */
32 
33 #ifndef BZF_MD5_H
34 #define BZF_MD5_H
35 
36 namespace winux
37 {
38 // a small class for calculating MD5 hashes of strings or byte arrays
39 // it is not meant to be fast or secure
40 //
41 // usage: 1) feed it blocks of uchars with update()
42 // 2) finalize()
43 // 3) get hexdigest() string
44 // or
45 // MD5(AnsiString).hexdigest()
46 //
47 // assumes that char is 8 bit and int is 32 bit
48 
50 {
51 public:
52  typedef unsigned int size_type; // must be 32bit
53  typedef unsigned char uint8; // 8bit
54  typedef unsigned int uint32; // 32bit
55 
56  MD5();
57  MD5(const AnsiString& content);
58  void update(const unsigned char buf[], size_type length);
59  void update(const char buf[], size_type length);
60  MD5& finalize();
61 
62  AnsiString hexdigest() const;
63  Buffer digestres() const;
64  AnsiString toString() const;
65  friend std::ostream& operator<<(std::ostream& out, MD5& md5);
66 
67 private:
68  void init();
69  enum {blocksize = 64};
70 
71  void transform(const uint8 block[blocksize]);
72  static void Decode(uint32 output[], const uint8 input[], size_type len);
73  static void Encode(uint8 output[], const uint32 input[], size_type len);
74 
75  bool finalized;
76  uint8 buffer[blocksize];
77  uint32 count[2];
78  uint32 state[4];
79  uint8 digest[16];
80 
81  // low level logic operations
82  static inline uint32 F(uint32 x, uint32 y, uint32 z);
83  static inline uint32 G(uint32 x, uint32 y, uint32 z);
84  static inline uint32 H(uint32 x, uint32 y, uint32 z);
85  static inline uint32 I(uint32 x, uint32 y, uint32 z);
86  static inline uint32 RotateLeft(uint32 x, int n);
87  static inline void FF(uint32 &a, uint32 b, uint32 c, uint32 d, uint32 x, uint32 s, uint32 ac);
88  static inline void GG(uint32 &a, uint32 b, uint32 c, uint32 d, uint32 x, uint32 s, uint32 ac);
89  static inline void HH(uint32 &a, uint32 b, uint32 c, uint32 d, uint32 x, uint32 s, uint32 ac);
90  static inline void II(uint32 &a, uint32 b, uint32 c, uint32 d, uint32 x, uint32 s, uint32 ac);
91 };
92 
93 } // namespace winux
94 
95 #endif
XString< char > AnsiString
Definition: utilities.hpp:212
#define WINUX_DLL
Definition: utilities.hpp:60
std::ostream & operator<<(std::ostream &o, ConsoleAttrT< _VarType > const &tr)
Definition: console.hpp:137
unsigned int uint32
Definition: md5.hpp:54
缓冲区,表示内存中一块二进制数据(利用malloc/realloc进行内存分配)
Definition: utilities.hpp:528
unsigned int size_type
Definition: md5.hpp:52
跨平台基础功能库
Definition: archives.hpp:7
unsigned char uint8
Definition: md5.hpp:53