fastdo  0.5.12
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 uint1; // 8bit
54  typedef unsigned int uint4; // 32bit
55 
56  MD5();
57  MD5(const AnsiString& text);
58  void update(const unsigned char *buf, size_type length);
59  void update(const char *buf, size_type length);
60  MD5& finalize();
61 
62  String hexdigest() const;
63  void digestres( uint1 res[16] ) const;
64  String md5() const; // hexdigest()
65  friend std::ostream& operator<<(std::ostream&, MD5 &md5);
66 
67 private:
68  void init();
69  enum {blocksize = 64}; // VC6 won't eat a const static int here
70 
71  void transform(const uint1 block[blocksize]);
72  static void Decode(uint4 output[], const uint1 input[], size_type len);
73  static void Encode(uint1 output[], const uint4 input[], size_type len);
74 
75  bool finalized;
76  uint1 buffer[blocksize]; // bytes that didn't fit in last 64 byte chunk
77  uint4 count[2]; // 64bit counter for number of bits (lo, hi)
78  uint4 state[4]; // digest so far
79  uint1 digest[16]; // the result
80 
81  // low level logic operations
82  static inline uint4 F(uint4 x, uint4 y, uint4 z);
83  static inline uint4 G(uint4 x, uint4 y, uint4 z);
84  static inline uint4 H(uint4 x, uint4 y, uint4 z);
85  static inline uint4 I(uint4 x, uint4 y, uint4 z);
86  static inline uint4 RotateLeft(uint4 x, int n);
87  static inline void FF(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
88  static inline void GG(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
89  static inline void HH(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
90  static inline void II(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
91 };
92 
93 } // namespace winux
94 
95 #endif
#define WINUX_DLL
Definition: utilities.hpp:57
std::basic_string< char > AnsiString
Definition: utilities.hpp:165
std::ostream & operator<<(std::ostream &o, ConsoleAttrT< _VarType > const &tr)
Definition: console.hpp:134
unsigned char uint1
Definition: md5.hpp:53
unsigned int size_type
Definition: md5.hpp:52
unsigned int uint4
Definition: md5.hpp:54
std::basic_string< tchar > String
Definition: utilities.hpp:162
跨平台基础功能库
Definition: archives.hpp:7