http://http://diytec.web.fc2.com/mark2r2/

Dependencies:   EthernetNetIf NTPClient_NetServices mbed ConfigFile

Committer:
mark2r2
Date:
Tue Sep 20 12:46:26 2011 +0000
Revision:
0:08a4d61cd84c
V1.0

Who changed what in which revision?

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