Commit | Line | Data |
---|---|---|
34a6a1d6 JM |
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 | #include <string> | |
37 | ||
38 | ||
39 | // a small class for calculating MD5 hashes of strings or byte arrays | |
40 | // it is not meant to be fast or secure | |
41 | // | |
42 | // usage: 1) feed it blocks of uchars with update() | |
43 | // 2) finalize() | |
44 | // 3) get hexdigest() string | |
45 | // or | |
46 | // MD5(std::string).hexdigest() | |
47 | // | |
48 | // assumes that char is 8 bit and int is 32 bit | |
49 | class MD5 | |
50 | { | |
51 | public: | |
52 | typedef unsigned int size_type; // must be 32bit | |
53 | ||
54 | MD5(); | |
55 | MD5(const std::string &text); | |
56 | void update(const unsigned char *buf, size_type length); | |
57 | void update(const char *buf, size_type length); | |
58 | MD5 &finalize(); | |
59 | std::string hexdigest() const; | |
60 | void bindigest(void *buf, int len) const; | |
61 | ||
62 | private: | |
63 | void init(); | |
64 | typedef unsigned char uint1; // 8bit | |
65 | typedef unsigned int uint4; // 32bit | |
66 | enum {blocksize = 64}; // VC6 won't eat a const static int here | |
67 | ||
68 | void transform(const uint1 block[blocksize]); | |
69 | static void decode(uint4 output[], const uint1 input[], size_type len); | |
70 | static void encode(uint1 output[], const uint4 input[], size_type len); | |
71 | ||
72 | bool finalized; | |
73 | uint1 buffer[blocksize]; // bytes that didn't fit in last 64 byte chunk | |
74 | uint4 count[2]; // 64bit counter for number of bits (lo, hi) | |
75 | uint4 state[4]; // digest so far | |
76 | uint1 digest[16]; // the result | |
77 | ||
78 | // low level logic operations | |
79 | static inline uint4 F(uint4 x, uint4 y, uint4 z); | |
80 | static inline uint4 G(uint4 x, uint4 y, uint4 z); | |
81 | static inline uint4 H(uint4 x, uint4 y, uint4 z); | |
82 | static inline uint4 I(uint4 x, uint4 y, uint4 z); | |
83 | static inline uint4 rotate_left(uint4 x, int n); | |
84 | static inline void FF(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac); | |
85 | static inline void GG(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac); | |
86 | static inline void HH(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac); | |
87 | static inline void II(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac); | |
88 | }; | |
89 | ||
90 | std::string md5(const std::string str); | |
91 | ||
92 | #endif |