Slightly modified MD5 lib. Allows access to raw binary instead of hexadecimal ascii.
md5.h@0:56e31f50f1e0, 2011-06-19 (annotated)
- Committer:
- Remco
- Date:
- Sun Jun 19 16:26:54 2011 +0000
- Revision:
- 0:56e31f50f1e0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Remco | 0:56e31f50f1e0 | 1 | /* MD5 |
Remco | 0:56e31f50f1e0 | 2 | converted to C++ class by Frank Thilo (thilo@unix-ag.org) |
Remco | 0:56e31f50f1e0 | 3 | for bzflag (http://www.bzflag.org) |
Remco | 0:56e31f50f1e0 | 4 | |
Remco | 0:56e31f50f1e0 | 5 | based on: |
Remco | 0:56e31f50f1e0 | 6 | |
Remco | 0:56e31f50f1e0 | 7 | md5.h and md5.c |
Remco | 0:56e31f50f1e0 | 8 | reference implementation of RFC 1321 |
Remco | 0:56e31f50f1e0 | 9 | |
Remco | 0:56e31f50f1e0 | 10 | Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All |
Remco | 0:56e31f50f1e0 | 11 | rights reserved. |
Remco | 0:56e31f50f1e0 | 12 | |
Remco | 0:56e31f50f1e0 | 13 | License to copy and use this software is granted provided that it |
Remco | 0:56e31f50f1e0 | 14 | is identified as the "RSA Data Security, Inc. MD5 Message-Digest |
Remco | 0:56e31f50f1e0 | 15 | Algorithm" in all material mentioning or referencing this software |
Remco | 0:56e31f50f1e0 | 16 | or this function. |
Remco | 0:56e31f50f1e0 | 17 | |
Remco | 0:56e31f50f1e0 | 18 | License is also granted to make and use derivative works provided |
Remco | 0:56e31f50f1e0 | 19 | that such works are identified as "derived from the RSA Data |
Remco | 0:56e31f50f1e0 | 20 | Security, Inc. MD5 Message-Digest Algorithm" in all material |
Remco | 0:56e31f50f1e0 | 21 | mentioning or referencing the derived work. |
Remco | 0:56e31f50f1e0 | 22 | |
Remco | 0:56e31f50f1e0 | 23 | RSA Data Security, Inc. makes no representations concerning either |
Remco | 0:56e31f50f1e0 | 24 | the merchantability of this software or the suitability of this |
Remco | 0:56e31f50f1e0 | 25 | software for any particular purpose. It is provided "as is" |
Remco | 0:56e31f50f1e0 | 26 | without express or implied warranty of any kind. |
Remco | 0:56e31f50f1e0 | 27 | |
Remco | 0:56e31f50f1e0 | 28 | These notices must be retained in any copies of any part of this |
Remco | 0:56e31f50f1e0 | 29 | documentation and/or software. |
Remco | 0:56e31f50f1e0 | 30 | |
Remco | 0:56e31f50f1e0 | 31 | */ |
Remco | 0:56e31f50f1e0 | 32 | |
Remco | 0:56e31f50f1e0 | 33 | #ifndef BZF_MD5_H |
Remco | 0:56e31f50f1e0 | 34 | #define BZF_MD5_H |
Remco | 0:56e31f50f1e0 | 35 | |
Remco | 0:56e31f50f1e0 | 36 | #include <string> |
Remco | 0:56e31f50f1e0 | 37 | |
Remco | 0:56e31f50f1e0 | 38 | |
Remco | 0:56e31f50f1e0 | 39 | /** a small class for calculating MD5 hashes of strings or byte arrays |
Remco | 0:56e31f50f1e0 | 40 | it is not meant to be fast or secure |
Remco | 0:56e31f50f1e0 | 41 | |
Remco | 0:56e31f50f1e0 | 42 | usage: 1) feed it blocks of uchars with update() |
Remco | 0:56e31f50f1e0 | 43 | 2) finalize() |
Remco | 0:56e31f50f1e0 | 44 | 3) get hexdigest() string |
Remco | 0:56e31f50f1e0 | 45 | or |
Remco | 0:56e31f50f1e0 | 46 | MD5(std::string).hexdigest() |
Remco | 0:56e31f50f1e0 | 47 | |
Remco | 0:56e31f50f1e0 | 48 | assumes that char is 8 bit and int is 32 bit |
Remco | 0:56e31f50f1e0 | 49 | */ |
Remco | 0:56e31f50f1e0 | 50 | class MD5 |
Remco | 0:56e31f50f1e0 | 51 | { |
Remco | 0:56e31f50f1e0 | 52 | public: |
Remco | 0:56e31f50f1e0 | 53 | typedef unsigned int size_type; // must be 32bit |
Remco | 0:56e31f50f1e0 | 54 | |
Remco | 0:56e31f50f1e0 | 55 | MD5(); |
Remco | 0:56e31f50f1e0 | 56 | /** |
Remco | 0:56e31f50f1e0 | 57 | take string, hash it and finalize |
Remco | 0:56e31f50f1e0 | 58 | @param text the string to hash |
Remco | 0:56e31f50f1e0 | 59 | */ |
Remco | 0:56e31f50f1e0 | 60 | MD5(const std::string& text); |
Remco | 0:56e31f50f1e0 | 61 | /** |
Remco | 0:56e31f50f1e0 | 62 | add text to hash |
Remco | 0:56e31f50f1e0 | 63 | @param buf the text to add to the hash |
Remco | 0:56e31f50f1e0 | 64 | @param text length |
Remco | 0:56e31f50f1e0 | 65 | */ |
Remco | 0:56e31f50f1e0 | 66 | void update(const unsigned char *buf, size_type length); |
Remco | 0:56e31f50f1e0 | 67 | /** |
Remco | 0:56e31f50f1e0 | 68 | add text to hash |
Remco | 0:56e31f50f1e0 | 69 | @param buf the text to add to the hash |
Remco | 0:56e31f50f1e0 | 70 | @param text length |
Remco | 0:56e31f50f1e0 | 71 | */ |
Remco | 0:56e31f50f1e0 | 72 | void update(const char *buf, size_type length); |
Remco | 0:56e31f50f1e0 | 73 | /** |
Remco | 0:56e31f50f1e0 | 74 | calculate the final hash value |
Remco | 0:56e31f50f1e0 | 75 | */ |
Remco | 0:56e31f50f1e0 | 76 | MD5& finalize(); |
Remco | 0:56e31f50f1e0 | 77 | /** |
Remco | 0:56e31f50f1e0 | 78 | @return the hash as hex string |
Remco | 0:56e31f50f1e0 | 79 | */ |
Remco | 0:56e31f50f1e0 | 80 | std::string hexdigest() const; |
Remco | 0:56e31f50f1e0 | 81 | |
Remco | 0:56e31f50f1e0 | 82 | void init(); |
Remco | 0:56e31f50f1e0 | 83 | |
Remco | 0:56e31f50f1e0 | 84 | /** |
Remco | 0:56e31f50f1e0 | 85 | @return the hash as binary data, an array of 16 bytes. |
Remco | 0:56e31f50f1e0 | 86 | */ |
Remco | 0:56e31f50f1e0 | 87 | const unsigned char* rawdigest() const { return digest; } |
Remco | 0:56e31f50f1e0 | 88 | |
Remco | 0:56e31f50f1e0 | 89 | private: |
Remco | 0:56e31f50f1e0 | 90 | typedef unsigned char uint1; // 8bit |
Remco | 0:56e31f50f1e0 | 91 | typedef unsigned int uint4; // 32bit |
Remco | 0:56e31f50f1e0 | 92 | enum {blocksize = 64}; // VC6 won't eat a const static int here |
Remco | 0:56e31f50f1e0 | 93 | |
Remco | 0:56e31f50f1e0 | 94 | void transform(const uint1 block[blocksize]); |
Remco | 0:56e31f50f1e0 | 95 | static void decode(uint4 output[], const uint1 input[], size_type len); |
Remco | 0:56e31f50f1e0 | 96 | static void encode(uint1 output[], const uint4 input[], size_type len); |
Remco | 0:56e31f50f1e0 | 97 | |
Remco | 0:56e31f50f1e0 | 98 | bool finalized; |
Remco | 0:56e31f50f1e0 | 99 | uint1 buffer[blocksize]; // bytes that didn't fit in last 64 byte chunk |
Remco | 0:56e31f50f1e0 | 100 | uint4 count[2]; // 64bit counter for number of bits (lo, hi) |
Remco | 0:56e31f50f1e0 | 101 | uint4 state[4]; // digest so far |
Remco | 0:56e31f50f1e0 | 102 | uint1 digest[16]; // the result |
Remco | 0:56e31f50f1e0 | 103 | |
Remco | 0:56e31f50f1e0 | 104 | // low level logic operations |
Remco | 0:56e31f50f1e0 | 105 | static inline uint4 F(uint4 x, uint4 y, uint4 z); |
Remco | 0:56e31f50f1e0 | 106 | static inline uint4 G(uint4 x, uint4 y, uint4 z); |
Remco | 0:56e31f50f1e0 | 107 | static inline uint4 H(uint4 x, uint4 y, uint4 z); |
Remco | 0:56e31f50f1e0 | 108 | static inline uint4 I(uint4 x, uint4 y, uint4 z); |
Remco | 0:56e31f50f1e0 | 109 | static inline uint4 rotate_left(uint4 x, int n); |
Remco | 0:56e31f50f1e0 | 110 | static inline void FF(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac); |
Remco | 0:56e31f50f1e0 | 111 | static inline void GG(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac); |
Remco | 0:56e31f50f1e0 | 112 | static inline void HH(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac); |
Remco | 0:56e31f50f1e0 | 113 | static inline void II(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac); |
Remco | 0:56e31f50f1e0 | 114 | }; |
Remco | 0:56e31f50f1e0 | 115 | |
Remco | 0:56e31f50f1e0 | 116 | std::string md5(const std::string str); |
Remco | 0:56e31f50f1e0 | 117 | |
Remco | 0:56e31f50f1e0 | 118 | #endif |