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