1/* MD5
2Slightly modified version for the purposes of bs::framework and Banshee Engine by Marko Pintera.
3
4converted to C++ class by Frank Thilo (thilo@unix-ag.org)
5for bzflag (http://www.bzflag.org)
6
7based on:
8
9md5.h and md5.c
10reference implementation of RFC 1321
11
12Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
13rights reserved.
14
15License to copy and use this software is granted provided that it
16is identified as the "RSA Data Security, Inc. MD5 Message-Digest
17Algorithm" in all material mentioning or referencing this software
18or this function.
19
20License is also granted to make and use derivative works provided
21that such works are identified as "derived from the RSA Data
22Security, Inc. MD5 Message-Digest Algorithm" in all material
23mentioning or referencing the derived work.
24
25RSA Data Security, Inc. makes no representations concerning either
26the merchantability of this software or the suitability of this
27software for any particular purpose. It is provided "as is"
28without express or implied warranty of any kind.
29
30These notices must be retained in any copies of any part of this
31documentation and/or software.
32
33*/
34
35#ifndef BZF_MD5_H
36#define BZF_MD5_H
37
38#if !defined(_CRT_SECURE_NO_WARNINGS)
39#define _CRT_SECURE_NO_WARNINGS
40#endif
41
42#include <cstring>
43#include <iostream>
44
45// a small class for calculating MD5 hashes of strings or byte arrays
46// it is not meant to be fast or secure
47//
48// usage: 1) feed it blocks of uchars with update()
49// 2) finalize()
50// 3) get hexdigest() string
51// or
52// MD5(std::string).hexdigest()
53//
54// assumes that char is 8 bit and int is 32 bit
55class MD5
56{
57public:
58 typedef unsigned int size_type; // must be 32bit
59
60 MD5();
61 MD5(const std::string& text);
62 void update(const unsigned char *buf, size_type length);
63 void update(const char *buf, size_type length);
64 MD5& finalize();
65 std::string hexdigest() const;
66 void decdigest(unsigned char* buf, size_type length);
67
68private:
69 void init();
70 typedef unsigned char uint1; // 8bit
71 typedef unsigned int uint4; // 32bit
72 enum { blocksize = 64 }; // VC6 won't eat a const static int here
73
74 void transform(const uint1 block[blocksize]);
75 static void decode(uint4 output[], const uint1 input[], size_type len);
76 static void encode(uint1 output[], const uint4 input[], size_type len);
77
78 bool finalized;
79 uint1 buffer[blocksize]; // bytes that didn't fit in last 64 byte chunk
80 uint4 count[2]; // 64bit counter for number of bits (lo, hi)
81 uint4 state[4]; // digest so far
82 uint1 digest[16]; // the result
83
84 // low level logic operations
85 static inline uint4 F(uint4 x, uint4 y, uint4 z);
86 static inline uint4 G(uint4 x, uint4 y, uint4 z);
87 static inline uint4 H(uint4 x, uint4 y, uint4 z);
88 static inline uint4 I(uint4 x, uint4 y, uint4 z);
89 static inline uint4 rotate_left(uint4 x, int n);
90 static inline void FF(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
91 static inline void GG(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
92 static inline void HH(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
93 static inline void II(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
94};
95
96std::string md5(const std::string str);
97
98#endif