参考的说明链接:
MD5算法步骤详解
http://blog.sina.com.cn/s/blog_6fe0eb1901014cpl.html
MD5算法的C++实现
http://www.cnblogs.com/flying_bat/archive/2007/09/25/905133.html
看完网上文档后,主要的问题是,对于输入的信息你要怎么进行补位这些操作啊?因为自己是渣渣,而且又没有深究,所以就姑且用vector来实现了,代码也不够优,
还是有机会就再来优化吧!
直接上代码:
头文件 :myMd5.h
#ifndef MYMD5_H
#define MYMD5_H
#include <vector>
#include <string>
typedef unsigned char byte;
typedef unsigned long ulong;
using std::string;
using std::vector;
class MD5
{
public:
MD5();
void input(string str);
void printMD5();
void calc();
private:
void initialize();
void pad();
void transform();
void getAns();
private:
string s;
ulong ans[4];
ulong A, B, C, D;
vector<byte> in;
vector<ulong> app;
};
#endif
实现:myMd5.cpp
#include "myMd5.h"
#include <iostream>
using namespace std;
/********* F, G, H and I are basic MD5 non-linear functions. *********/
#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
#define H(x, y, z) ((x) ^ (y) ^ (z))
#define I(x, y, z) ((y) ^ ((x) | (~z)))
/********* ROTATE_LEFT rotates x left n bits. *********/
#define ROTATE_LEFT(x, n) (((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.
********