1 | // |
2 | // MD5 message-digest algorithm |
3 | // Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All rights reserved. |
4 | // |
5 | // C++/object oriented translation and modification: |
6 | // Copyright (C) 1995 Mordechai T. Abzug |
7 | // |
8 | // Further adaptations for SuperTux: |
9 | // Copyright (C) 2008 Christoph Sommer <christoph.sommer@2008.expires.deltadevelopment.de> |
10 | // |
11 | // This translation/modification is provided "as is," without express or |
12 | // implied warranty of any kind. |
13 | // |
14 | // The translators/modifiers do not claim: |
15 | // (1) that MD5 will do what you think it does; |
16 | // (2) that this translation/ modification is accurate; or |
17 | // (3) that this software is "merchantible." |
18 | // |
19 | // based on: |
20 | // |
21 | // MD5.H - header file for MD5C.C |
22 | // MDDRIVER.C - test driver for MD2, MD4 and MD5 |
23 | // Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All rights reserved. |
24 | // |
25 | // License to copy and use this software is granted provided that it |
26 | // is identified as the "RSA Data Security, Inc. MD5 Message-Digest |
27 | // Algorithm" in all material mentioning or referencing this software |
28 | // or this function. |
29 | // |
30 | // License is also granted to make and use derivative works provided |
31 | // that such works are identified as "derived from the RSA Data |
32 | // Security, Inc. MD5 Message-Digest Algorithm" in all material |
33 | // mentioning or referencing the derived work. |
34 | // |
35 | // RSA Data Security, Inc. makes no representations concerning either |
36 | // the merchantability of this software or the suitability of this |
37 | // software for any particular purpose. It is provided "as is" |
38 | // without express or implied warranty of any kind. |
39 | // |
40 | // These notices must be retained in any copies of any part of this |
41 | // documentation and/or software. |
42 | // |
43 | |
44 | #ifndef HEADER_SUPERTUX_ADDON_MD5_HPP |
45 | #define |
46 | |
47 | #include <fstream> |
48 | #include <stdint.h> |
49 | |
50 | class MD5 |
51 | { |
52 | public: |
53 | MD5(); |
54 | MD5(uint8_t* string); /**< digest string, finalize */ |
55 | MD5(std::istream& stream); /**< digest stream, finalize */ |
56 | MD5(FILE *file); /**< digest file, close, finalize */ |
57 | MD5(std::ifstream& stream); /**< digest stream, close, finalize */ |
58 | |
59 | void update(uint8_t* input, unsigned int input_length); /**< MD5 block update operation. Continues an MD5 message-digest operation, processing another message block, and updating the context. */ |
60 | void update(std::istream& stream); |
61 | void update(FILE *file); |
62 | void update(std::ifstream& stream); |
63 | |
64 | uint8_t* raw_digest(); /**< digest as a 16-byte binary array */ |
65 | std::string hex_digest(); /**< digest as a 33-byte ascii-hex string */ |
66 | friend std::ostream& operator<<(std::ostream&, MD5 context); |
67 | |
68 | private: |
69 | uint32_t state[4]; |
70 | uint32_t count[2]; /**< number of _bits_, mod 2^64 */ |
71 | uint8_t buffer[64]; /**< input buffer */ |
72 | uint8_t digest[16]; |
73 | bool finalized; |
74 | |
75 | void init(); /**< called by all constructors */ |
76 | void finalize(); /**< MD5 finalization. Ends an MD5 message-digest operation, writing the the message digest and zeroizing the context. */ |
77 | void transform(uint8_t* buffer); /**< MD5 basic transformation. Transforms state based on block. Does the real update work. Note that length is implied to be 64. */ |
78 | |
79 | static void encode(uint8_t* dest, uint32_t* src, uint32_t length); /**< Encodes input (uint32_t) into output (uint8_t). Assumes len is a multiple of 4. */ |
80 | static void decode(uint32_t* dest, uint8_t* src, uint32_t length); /**< Decodes input (uint8_t) into output (uint32_t). Assumes len is a multiple of 4. */ |
81 | static void memcpy(uint8_t* dest, uint8_t* src, uint32_t length); |
82 | static void memset(uint8_t* start, uint8_t val, uint32_t length); |
83 | |
84 | static inline uint32_t rotate_left(uint32_t x, uint32_t n); |
85 | static inline uint32_t F(uint32_t x, uint32_t y, uint32_t z); //*< F, G, H and I are basic MD5 functions. */ |
86 | static inline uint32_t G(uint32_t x, uint32_t y, uint32_t z); |
87 | static inline uint32_t H(uint32_t x, uint32_t y, uint32_t z); |
88 | static inline uint32_t I(uint32_t x, uint32_t y, uint32_t z); |
89 | static inline void FF(uint32_t& a, uint32_t b, uint32_t c, uint32_t d, uint32_t x, uint32_t s, uint32_t ac); /**< FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. Rotation is separate from addition to prevent recomputation. */ |
90 | static inline void GG(uint32_t& a, uint32_t b, uint32_t c, uint32_t d, uint32_t x, uint32_t s, uint32_t ac); |
91 | static inline void HH(uint32_t& a, uint32_t b, uint32_t c, uint32_t d, uint32_t x, uint32_t s, uint32_t ac); |
92 | static inline void II(uint32_t& a, uint32_t b, uint32_t c, uint32_t d, uint32_t x, uint32_t s, uint32_t ac); |
93 | |
94 | }; |
95 | |
96 | #endif |
97 | |
98 | /* EOF */ |
99 | |