f6aa930d0a0da2e0d58b489cc2fec5c371f3fd39
[supertux.git] / src / addon / md5.hpp
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 SUPERTUX_ADDON_MD5_HPP
45 #define SUPERTUX_ADDON_MD5_HPP
46
47 #include <stdio.h>
48 #include <fstream>
49 #include <iostream>
50 #include <stdint.h>
51 #include <string>
52
53 class MD5 {
54
55   public:
56     MD5(); 
57     MD5(uint8_t* string); /**< digest string, finalize */
58     MD5(std::istream& stream); /**< digest stream, finalize */
59     MD5(FILE *file); /**< digest file, close, finalize */
60     MD5(std::ifstream& stream); /**< digest stream, close, finalize */
61
62     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. */
63     void update(std::istream& stream);
64     void update(FILE *file);
65     void update(std::ifstream& stream);
66
67     uint8_t* raw_digest(); /**< digest as a 16-byte binary array */
68     std::string hex_digest(); /**< digest as a 33-byte ascii-hex string */
69     friend std::ostream& operator<<(std::ostream&, MD5 context);
70
71   private:
72     uint32_t state[4];
73     uint32_t count[2]; /**< number of _bits_, mod 2^64 */
74     uint8_t buffer[64]; /**< input buffer */
75     uint8_t digest[16];
76     bool finalized;
77
78     void init(); /**< called by all constructors */
79     void finalize(); /**< MD5 finalization. Ends an MD5 message-digest operation, writing the the message digest and zeroizing the context. */
80     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. */
81
82     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. */
83     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. */
84     static void memcpy(uint8_t* dest, uint8_t* src, uint32_t length);
85     static void memset(uint8_t* start, uint8_t val, uint32_t length);
86
87     static inline uint32_t rotate_left(uint32_t x, uint32_t n);
88     static inline uint32_t F(uint32_t x, uint32_t y, uint32_t z); //*< F, G, H and I are basic MD5 functions. */
89     static inline uint32_t G(uint32_t x, uint32_t y, uint32_t z);
90     static inline uint32_t H(uint32_t x, uint32_t y, uint32_t z);
91     static inline uint32_t I(uint32_t x, uint32_t y, uint32_t z);
92     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. */
93     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);
94     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);
95     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);
96
97 };
98
99 #endif /*SUPERTUX_ADDON_MD5_HPP*/