分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
转自:http://cooker.iteye.com/blog/969196
- //MD5.h
- #ifndef BZF_MD5_H
- #define BZF_MD5_H
- #include <string>
- #include <iostream>
- // a small class for calculating MD5 hashes of strings or byte arrays
- // it is not meant to be fast or secure
- //
- // usage: 1) feed it blocks of uchars with update()
- // 2) finalize()
- // 3) get hexdigest() string
- // or
- // MD5(std::string).hexdigest()
- //
- // assumes that char is 8 bit and int is 32 bit
- class MD5
- {
- public:
- typedef unsigned int size_type; // must be 32bit
- MD5();
- MD5(const std::string& text);
- void update(const unsigned char *buf, size_type length);
- void update(const char *buf, size_type length);
- MD5& finalize();
- std::string hexdigest() const;
- friend std::ostream& operator<<(std::ostream&, MD5 md5);
- private:
- void init();
- typedef unsigned char uint1; // 8bit
- typedef unsigned int uint4; // 32bit
- enum {blocksize = 64}; // VC6 won't eat a const static int here
- void transform(const uint1 block[blocksize]);
- static void decode(uint4 output[], const uint1 input[], size_type len);
- static void encode(uint1 output[], const uint4 input[], size_type len);
- bool finalized;
- uint1 buffer[blocksize]; // bytes that didn't fit in last 64 byte chunk
- uint4 count[2]; // 64bit counter for number of bits (lo, hi)
- uint4 state[4]; // digest so far
- uint1 digest[16]; // the result
- // low level logic operations
- static inline uint4 F(uint4 x, uint4 y, uint4 z);
- static inline uint4 G(uint4 x, uint4 y, uint4 z);
- static inline uint4 H(uint4 x, uint4 y, uint4 z);
- static inline uint4 I(uint4 x, uint4 y, uint4 z);
- static inline uint4 rotate_left(uint4 x, int n);
- static inline void FF(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
- static inline void GG(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
- static inline void HH(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
- static inline void II(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
- };
- std::string md5(const std::string str);
- #endif
- //MD5.cpp
- /* MD5
- converted to C++ class by Frank Thilo (thilo@unix-ag.org)
- for bzflag (http://www.bzflag.org)
- based on:
- md5.h and md5.c
- reference implemantion of RFC 1321
- Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
- rights reserved.
- License to copy and use this software is granted provided that it
- is identified as the "RSA Data Security, Inc. MD5 Message-Digest
- Algorithm" in all material mentioning or referencing this software
- or this function.
- License is also granted to make and use derivative works provided
- that such works are identified as "derived from the RSA Data
- Security, Inc. MD5 Message-Digest Algorithm" in all material
- mentioning or referencing the derived work.
- RSA Data Security, Inc. makes no representations concerning either
- the merchantability of this software or the suitability of this
- software for any particular purpose. It is provided "as is"
- without express or implied warranty of any kind.
- These notices must be retained in any copies of any part of this
- documentation and/or software.
- */
- /* interface header */
- #include "md5.h"
- /* system implementation headers */
- #include <stdio.h>
- // Constants for MD5Transform routine.
- #define S11 7
- #define S12 12
- #define S13 17
- #define S14 22
- #define S21 5
- #define S22 9
- #define S23 14
- #define S24 20
- #define S31 4
- #define S32 11
- #define S33 16
- #define S34 23
- #define S41 6
- #define S42 10
- #define S43 15
- #define S44 21
- ///
- // F, G, H and I are basic MD5 functions.
- inline MD5::uint4 MD5::F(uint4 x, uint4 y, uint4 z) {
- return x&y | ~x&z;
- }
- inline MD5::uint4 MD5::G(uint4 x, uint4 y, uint4 z) {
- return x&z | y&~z;
- }
- inline MD5::uint4 MD5::H(uint4 x, uint4 y, uint4 z) {
- return x^y^z;
- }
- inline MD5::uint4 MD5::I(uint4 x, uint4 y, uint4 z) {
- return y ^ (x | ~z);
- }
- // rotate_left rotates x left n bits.
- inline MD5::uint4 MD5::rotate_left(uint4 x, int n) {
- return (x << n) | (x >> (32-n));
- }
- // FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
- // Rotation is separate from addition to prevent recomputation.
- inline void MD5::FF(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac) {
- a = rotate_left(a+ F(b,c,d) + x + ac, s) + b;
- }
- inline void MD5::GG(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac) {
- a = rotate_left(a + G(b,c,d) + x + ac, s) + b;
- }
- inline void MD5::HH(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac) {
- a = rotate_left(a + H(b,c,d) + x + ac, s) + b;
- }
- inline void MD5::II(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac) {
- a = rotate_left(a + I(b,c,d) + x + ac, s) + b;
- }
- //
- // default ctor, just initailize
- MD5::MD5()
- {
- init();
- }
- //
- // nifty shortcut ctor, compute MD5 for string and finalize it right away
- MD5::MD5(const std::string &text)
- {
- init();
- update(text.c_str(), text.length());
- finalize();
- }
- //
- void MD5::init()
- {
- finalized=false;
- count[0] = 0;
- count[1] = 0;
- // load magic initialization constants.
- state[0] = 0x67452301;
- state[1] = 0xefcdab89;
- state[2] = 0x98badcfe;
- state[3] = 0x10325476;
- }
- //
- // decodes input (unsigned char) into output (uint4). Assumes len is a multiple of 4.
- void MD5::decode(uint4 output[], const uint1 input[], size_type len)
- {
- for (unsigned int i = 0, j = 0; j < len; i++, j += 4)
- output[i] = ((uint4)input[j]) | (((uint4)input[j+1]) << 8) |
- (((uint4)input[j+2]) << 16) | (((uint4)input[j+3]) << 24);
- }
- //
- // encodes input (uint4) into output (unsigned char). Assumes len is
- // a multiple of 4.
- void MD5::encode(uint1 output[], const uint4 input[], size_type len)
- {
- for (size_type i = 0, j = 0; j < len; i++, j += 4) {
- output[j] = input[i] & 0xff;
- output[j+1] = (input[i] >> 8) & 0xff;
- output[j+2] = (input[i] >> 16) & 0xff;
- output[j+3] = (input[i] >> 24) & 0xff;
- }
- }
- //
- // apply MD5 algo on a block
- void MD5::transform(const uint1 block[blocksize])
- {
- uint4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
- decode (x, block, blocksize);
- /* Round 1 */
- FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
- FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
- FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
- FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
- FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
- FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
- FF (c, d, a, b, x[ 6], S13, 0xa8304613);