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