代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C/C++
】
md5算法的C++实现
作者:
欣蔚
/ 发布于
2016/7/25
/
857
/* * md5.h * * Created on: 2016年7月22日 * Author: sun2019 */ #ifndef MD5_H_ #define MD5_H_ #include <assert.h> #include <math.h> #include <iostream> #include <stdint.h> #include <string> using namespace std; class MD5 { public: MD5(); ~MD5(); string calc(const char *str); string calc(const char *buf, int32_t len); protected: MD5(const MD5 &); MD5& operator=(const MD5 &); private: uint32_t T[64]; static const uint32_t CV[4]; static const uint32_t R[64]; void prepare(const char *buf, int32_t len, char *&outbuf, int32_t &outlen); inline uint32_t rotate_left(uint32_t num, uint32_t n) const; string _gen_md5_str(uint32_t a, uint32_t b, uint32_t c, uint32_t d) const; inline uint32_t _reverse(const uint32_t n) const; inline uint32_t F(uint32_t x, uint32_t y, uint32_t z) const; inline uint32_t G(uint32_t x, uint32_t y, uint32_t z) const; inline uint32_t H(uint32_t x, uint32_t y, uint32_t z) const; inline uint32_t I(uint32_t x, uint32_t y, uint32_t z) const; #if 0 inline int32_t FF(int32_t a, int32_t b, int32_t c, int32_t d, int32_t m, int32_t s, int32_t t); inline int32_t GG(int32_t a, int32_t b, int32_t c, int32_t d, int32_t m, int32_t s, int32_t t); inline int32_t HH(int32_t a, int32_t b, int32_t c, int32_t d, int32_t m, int32_t s, int32_t t); inline int32_t II(int32_t a, int32_t b, int32_t c, int32_t d, int32_t m, int32_t s, int32_t t); #endif }; #endif /* MD5_H_ */ 2. [代码][C/C++]代码 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 /* * md5.cpp * * Created on: 2016年7月22日 * Author: sun2019 */ #include "md5.h" #include <assert.h> #include <math.h> #include<stdio.h> #include <iostream> #include <string.h> #include <stdint.h> #include <string> const uint32_t MD5::CV[4] = { 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476 }; const uint32_t MD5::R[64] = { 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21 }; MD5::MD5() { //initialize T[] for (int i = 1; i <= 64; ++i) { long long ti = pow(2, 32) * sin(i); if (ti < 0) { ti = 0 - ti; } T[i - 1] = ti; } } MD5::~MD5() { } string MD5::calc(const char *str) { return calc(str, strlen(str)); } string MD5::calc(const char *buf, const int32_t len) { char *outbuf = NULL; int32_t outlen = 0; prepare(buf, len, outbuf, outlen); assert(outlen > 0 && outlen % 64 == 0); uint32_t A = CV[0]; uint32_t B = CV[1]; uint32_t C = CV[2]; uint32_t D = CV[3]; for (int32_t i = 0; i < outlen / 64; ++i) { uint32_t X[16] = {0}; for (int32_t j = 0; j < 16; ++j) { X[j] = *(uint32_t*)(outbuf + (64 * i + 4 * j)); } uint32_t a = A; uint32_t b = B; uint32_t c = C; uint32_t d = D; for (int i = 0; i < 64; ++i) { int32_t g = 0; if (i >= 0 && i <= 15) { g = i; a = (uint32_t)(b + rotate_left(a + F(b, c, d) + X[g] + T[i], R[i])); } else if (i >= 16 && i <= 31) { g = (i * 5 + 1) % 16; a = (uint32_t)(b + rotate_left(a + G(b, c, d) + X[g] + T[i], R[i])); } else if (i >= 32 && i <= 47) { g = (i * 3 + 5) % 16; a = (uint32_t)(b + rotate_left(a + H(b, c, d) + X[g] + T[i], R[i])); } else if (i >= 48 && i <= 63) { g = (i * 7) % 16; a = (uint32_t)(b + rotate_left(a + I(b, c, d) + X[g] + T[i], R[i])); } //loop swap uint32_t temp = d; d = c; c = b; b = a; a = temp; } A = A + a; B = B + b; C = C + c; D = D + d; } return _gen_md5_str(A, B, C, D); } inline uint32_t MD5::rotate_left(uint32_t num, uint32_t n) const { return (num << n) | (num >> (32 - n % 32)); } string MD5::_gen_md5_str(uint32_t a, uint32_t b, uint32_t c, uint32_t d) const { char res[33] = {0}; sprintf(res, "%08x%08x%08x%08x", _reverse(a), _reverse(b), _reverse(c), _reverse(d)); return res; } inline uint32_t MD5::_reverse(const uint32_t n) const { return ((n & 0x000000ff) << 24) | ((n & 0x0000ff00) << 8) | ((n & 0x00ff0000) >> 8) | ((n & 0xff000000) >> 24); } void MD5::prepare(const char *buf, int32_t len, char *&outbuf, int32_t &outlen) { int32_t x = len * 8 % 512; int32_t bit_to_fill = (x < 448) ? (448 - x) : (512 - x + 448); char c1 = 1, c2 = 0; c1 = c1 << 7; if (bit_to_fill % 8 == 0) { int32_t byte_to_fill = bit_to_fill / 8; int32_t new_len = len + byte_to_fill + 8; char *p = new (nothrow) char[new_len]; assert(p); char *q = p; memcpy(q, buf, len); q += len; for (int i = 0; i < byte_to_fill; ++i) { if (i == 0) { memcpy(q, &c1, 1); } else { memcpy(q, &c2, 1); } ++q; } int64_t length = len * 8; memcpy(q, &length, 8); outbuf = p; outlen = new_len; } assert(bit_to_fill % 8 == 0); } inline uint32_t MD5::F(uint32_t x, uint32_t y, uint32_t z) const { return (x & y) | ((~x) & z); } inline uint32_t MD5::G(uint32_t x, uint32_t y, uint32_t z) const { return (x & z) | (y & (~z)); } inline uint32_t MD5::H(uint32_t x, uint32_t y, uint32_t z) const { return x ^ y ^ z; } inline uint32_t MD5::I(uint32_t x, uint32_t y, uint32_t z) const { return y ^ (x | (~z)); } #if 0 inline int32_t MD5::FF(int32_t a, int32_t b, int32_t c, int32_t d, int32_t m, int32_t s, int32_t t) { } inline int32_t MD5::GG(int32_t a, int32_t b, int32_t c, int32_t d, int32_t m, int32_t s, int32_t t) { } inline int32_t MD5::HH(int32_t a, int32_t b, int32_t c, int32_t d, int32_t m, int32_t s, int32_t t) { } inline int32_t MD5::II(int32_t a, int32_t b, int32_t c, int32_t d, int32_t m, int32_t s, int32_t t) { } #endif /* * test.cpp * * Created on: 2016年7月22日 * Author: sun2019 */ #include "lib/md5.h" #include<stdio.h> int main() { MD5 md5; string code; code = md5.calc("md5压缩算法"); cout << "md5压缩算法:md5 = " << code << endl; return 0; }
试试其它关键字
md5算法
同语言下
.
C分鱼问题
.
链表
.
最大连续和
.
编码字符串
.
libiconv字符编码处理及判断字符串是否为utf8
.
一组数中两两二元组,差最大有几对,差最小呢?(数组
.
通过管道获取一个进程的执行状态
.
多关键字排序
.
字符串字典序排序
.
3元一次方程(牛顿迭代法求方程的根)
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
欣蔚
贡献的其它代码
(
15
)
.
查看一个关键字的前后N行
.
Mysql查询某字段值重复的数据
.
查询某个字段信息,由多个id逗号隔开拼装成
.
java 日期比较得到分钟的差数
.
获取Linux系统内存情况
.
,显示/删除文件目录下的所有文件及空目录,同时可以
.
生成PDF中插入图片
.
md5算法的C++实现
.
在线更新
.
Asp.net 获取服务器信息
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3