国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - C/C++ - c++實現MD5算法實現代碼

c++實現MD5算法實現代碼

2021-01-10 12:19C++教程網 C/C++

用c++實現了md5算法。包含 md5.h 和md5.cpp 兩個文件。主要參考百度百科 “MD5” 原理,代碼中變量命名也是參考其中的公式,程序的使用說明在md5.h 文件的末尾注釋中

測試結果和百度百科測試例子一致。

實現過程中需要注意事項:最后把四個變量A B C D 鏈接成結果時 ,注意變量高低位的先后順序,具體參考 LinkResult()方法。

md5.h

?
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
#ifndef _MD5_H_
#define _MD5_H_
 
#include <iostream>
#include <string>
using namespace std;
 
class MD5
{
  public:
    typedef unsigned char uchar8; //make sure it is 8bit
    typedef char char8; //make sure it is 8bit
    MD5();
    
    void init();
 
    void UpdateMd5(const uchar8 input[], const int length);  
    void UpdateMd5(const char8 input[], const int length);  
    
    void Finalize();
    
    void ComputMd5(const uchar8 input[], const int length);
    void ComputMd5(const char8 input[], const int length);
    
    string GetMd5();
    
    void printMd5();
    
    
  private:
    typedef unsigned int uint32;    //make sure it is 32 bit;
    typedef unsigned long long uint64; //make sure it is 64 bit;
    uint32 A, B, C, D;
    const static int blockLen_ = 64;  // 512/8                
    //the remain after last updata (because md5 may be computed segment by segment)
    uchar8 remain_[blockLen_];         
    int remainNum_ ;     // the number of remain_, < 64
    uint64 totalInputBits_;
    uchar8 md5Result_[16];  //bit style md5 result,totally 128 bit
    char md5Result_hex_[33]; //hexadecimal style result; md5Result_hex_[32]='\0'
    bool isDone_;      // indicate the comput is finished;
          
    inline uint32 RotateLeft(const uint32 x, int n);
    inline uint32 F(const uint32 x, const uint32 y, const uint32 z);
    inline uint32 G(const uint32 x, const uint32 y, const uint32 z);
    inline uint32 H(const uint32 x, const uint32 y, const uint32 z);
    inline uint32 I(const uint32 x, const uint32 y, const uint32 z);
    inline void FF(uint32 &a, const uint32 b, const uint32 c, const uint32 d,
            const uint32 Mj, const int s, const uint32 ti);
    inline void GG(uint32 &a, const uint32 b, const uint32 c, const uint32 d,
            const uint32 Mj, const int s, const uint32 ti);
    inline void HH(uint32 &a, const uint32 b, const uint32 c, const uint32 d,
            const uint32 Mj, const int s, const uint32 ti);
    inline void II(uint32 &a, const uint32 b, const uint32 c, const uint32 d,
            const uint32 Mj, const int s, const uint32 ti);
            
 
    void UcharToUint(uint32 output[], const uchar8 input[], const unsigned int transLength);
    
    void FourRound(const uchar8 block[]); 
 
    void LinkResult();
          
};
 
/* user guide
  you can comput the md5 by using the funtion ComputMd5
  eg:
    MD5 m;
    MD5::char8 str[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    m.ComputMd5(str,sizeof(str) - 1); 
    m.printMd5();
 
  if you want to comput segment by segment,you can do as follow, and init() is suggested
  the begging,and Finalize() must call in the end:
    
    MD5 M;
    m.init();
    MD5::uchar8 str1[] = "ABCDEFGHIJKLMN";
    MD5::uchar8 str2[] = "OPQRSTUVWXYZabcdefghijk";
    MD5::uchar8 str3[] = "lmnopqrstuvwxyz";
    m.UpdateMd5(str1,sizeof(str1) - 1);
    m.UpdateMd5(str2,sizeof(str2) - 1);
    m.UpdateMd5(str3,sizeof(str3) - 1);
    m.Finalize();
    m.printMd5();
 
  if you want to comput the md5 of a file, you can use the interface of this program.
*/
 
#endif

md5.cpp

?
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include"md5.h"
 
#include<iostream>
using namespace std;
 
const int S[4][4] = {7, 12, 17, 22,
           5, 9, 14, 20,
           4, 11, 16, 23,
           6, 10, 15, 21};
void MD5::init()
{
  A = 0x67452301;
  B = 0xefcdab89;
  C = 0x98badcfe;
  D = 0x10325476;
  remainNum_ = 0;
  remain_[0] = '\0';
  md5Result_hex_[0] = '\0';
  md5Result_[0] = '\0';
  totalInputBits_ = 0;
  isDone_ = false;
}
 
MD5::MD5()
{
  init();
}
 
inline MD5::uint32 MD5::RotateLeft(const uint32 x, int n)
{
  return (x << n) | (x >> (32-n));   
  // if x is signed, use: (x << n) | ((x & 0xFFFFFFFF) >> (32-n))
}
inline MD5::uint32 MD5::F(const uint32 x, const uint32 y, const uint32 z)
{
  return (x & y) | ((~x) & z);
}
inline MD5::uint32 MD5::G(const uint32 x, const uint32 y, const uint32 z)
{
  return (x & z) | (y & (~z));
}
inline MD5::uint32 MD5::H(const uint32 x, const uint32 y, const uint32 z)
{
  return x ^ y ^ z;
}
inline MD5::uint32 MD5::I(const uint32 x, const uint32 y, const uint32 z)
{
  return y ^ (x | (~z));
}
 
inline void MD5::FF(uint32 &a, const uint32 b, const uint32 c, const uint32 d,
          const uint32 Mj, const int s, const uint32 ti)
{
  a = b + RotateLeft(a + F(b, c, d) + Mj + ti, s);
}  
inline void MD5::GG(uint32 &a, const uint32 b, const uint32 c, const uint32 d,
          const uint32 Mj, const int s, const uint32 ti)
{
  a = b + RotateLeft(a + G(b, c, d) + Mj + ti, s);
}         
inline void MD5::HH(uint32 &a, const uint32 b, const uint32 c, const uint32 d,
          const uint32 Mj, const int s, const uint32 ti)
{
  a = b + RotateLeft(a + H(b, c, d) + Mj + ti, s);
}         
inline void MD5::II(uint32 &a, const uint32 b, const uint32 c, const uint32 d,
          const uint32 Mj, const int s, const uint32 ti)
{
  a = b + RotateLeft(a + I(b, c, d) + Mj + ti, s);
}    
 
// link A B C D to result(bit style result and hexadecimal style result)
void MD5::LinkResult()
{
  //bit style result
  for(int i = 0; i < 4; i++) //link A: low to high
  {
    md5Result_[i] = (A >> 8*i) & 0xff;
  }
  for(int i = 4; i<8; i++)  //link B: low to high
  {
    md5Result_[i] = (B >> 8*(i - 4)) & 0xff;
  }
  for(int i = 8; i<12; i++) //link C: low to high
  {
    md5Result_[i] = (C >> 8*(i - 8)) & 0xff;
  }
  for(int i = 12; i<16; i++) //link D: low to high
  {
    md5Result_[i] = (D >> 8*(i - 12)) & 0xff;
  }
 
  //change to hexadecimal style result
  // note: it is not the same as simply link hex(A) hex(B) hex(C) hex(D)
  for(int i = 0; i < 16; i++)
    sprintf(& md5Result_hex_[i*2], "%02x", md5Result_[i]);
  md5Result_hex_[32] = '\0';
 
}
 
//print the md5 by hex
void MD5::printMd5()
{
  if(!isDone_)
  {
    cout<< "Error: computation is not finished" <<endl;
    
  }
  else
    cout<< "MD5 Value: " << md5Result_hex_ <<endl;
}
 
//get the md5 value of hex style
string MD5::GetMd5()
{
  if(!isDone_)
  {
    cout<< "Error: computation is not finished" <<endl;
    exit(0);
  }
  string a((const char *)md5Result_hex_);
  return a;
}
 
void MD5::UcharToUint(uint32 output[], const uchar8 input[], const unsigned int transLength)    
{
  for(int i = 0, j = 0; j < transLength; i++, j += 4)
  {
    output[i] = ((uint32)input[j]) | (((uint32)input[j+1]) << 8) |
        (((uint32)input[j+2]) << 16) | (((uint32)input[j+3]) << 24);
  }
}
 
// four round on a block of 512 bits;
void MD5::FourRound(const uchar8 block[])
{
  uint32 a = A, b = B, c = C, d = D;
  uint32 M[16];
  UcharToUint(M, block, blockLen_); //blockLen_ is a const int =64;
  
  //round 1
  FF (a, b, c, d, M[ 0], S[0][0], 0xd76aa478);
  FF (d, a, b, c, M[ 1], S[0][1], 0xe8c7b756);
  FF (c, d, a, b, M[ 2], S[0][2], 0x242070db);
  FF (b, c, d, a, M[ 3], S[0][3], 0xc1bdceee);
  FF (a, b, c, d, M[ 4], S[0][0], 0xf57c0faf);
  FF (d, a, b, c, M[ 5], S[0][1], 0x4787c62a);
  FF (c, d, a, b, M[ 6], S[0][2], 0xa8304613);
  FF (b, c, d, a, M[ 7], S[0][3], 0xfd469501);
  FF (a, b, c, d, M[ 8], S[0][0], 0x698098d8);
  FF (d, a, b, c, M[ 9], S[0][1], 0x8b44f7af);
  FF (c, d, a, b, M[10], S[0][2], 0xffff5bb1);
  FF (b, c, d, a, M[11], S[0][3], 0x895cd7be);
  FF (a, b, c, d, M[12], S[0][0], 0x6b901122);
  FF (d, a, b, c, M[13], S[0][1], 0xfd987193);
  FF (c, d, a, b, M[14], S[0][2], 0xa679438e);
  FF (b, c, d, a, M[15], S[0][3], 0x49b40821);
 
  // round 2
  GG (a, b, c, d, M[ 1], S[1][0], 0xf61e2562);
  GG (d, a, b, c, M[ 6], S[1][1], 0xc040b340);
  GG (c, d, a, b, M[11], S[1][2], 0x265e5a51);
  GG (b, c, d, a, M[ 0], S[1][3], 0xe9b6c7aa);
  GG (a, b, c, d, M[ 5], S[1][0], 0xd62f105d);
  GG (d, a, b, c, M[10], S[1][1], 0x2441453);
  GG (c, d, a, b, M[15], S[1][2], 0xd8a1e681);
  GG (b, c, d, a, M[ 4], S[1][3], 0xe7d3fbc8);
  GG (a, b, c, d, M[ 9], S[1][0], 0x21e1cde6);
  GG (d, a, b, c, M[14], S[1][1], 0xc33707d6);
  GG (c, d, a, b, M[ 3], S[1][2], 0xf4d50d87);
  GG (b, c, d, a, M[ 8], S[1][3], 0x455a14ed);
  GG (a, b, c, d, M[13], S[1][0], 0xa9e3e905);
  GG (d, a, b, c, M[ 2], S[1][1], 0xfcefa3f8);
  GG (c, d, a, b, M[ 7], S[1][2], 0x676f02d9);
  GG (b, c, d, a, M[12], S[1][3], 0x8d2a4c8a);
 
  //round 3
  HH (a, b, c, d, M[ 5], S[2][0], 0xfffa3942);
  HH (d, a, b, c, M[ 8], S[2][1], 0x8771f681);
  HH (c, d, a, b, M[11], S[2][2], 0x6d9d6122);
  HH (b, c, d, a, M[14], S[2][3], 0xfde5380c);
  HH (a, b, c, d, M[ 1], S[2][0], 0xa4beea44);
  HH (d, a, b, c, M[ 4], S[2][1], 0x4bdecfa9);
  HH (c, d, a, b, M[ 7], S[2][2], 0xf6bb4b60);
  HH (b, c, d, a, M[10], S[2][3], 0xbebfbc70);
  HH (a, b, c, d, M[13], S[2][0], 0x289b7ec6);
  HH (d, a, b, c, M[ 0], S[2][1], 0xeaa127fa);
  HH (c, d, a, b, M[ 3], S[2][2], 0xd4ef3085);
  HH (b, c, d, a, M[ 6], S[2][3], 0x4881d05);
  HH (a, b, c, d, M[ 9], S[2][0], 0xd9d4d039);
  HH (d, a, b, c, M[12], S[2][1], 0xe6db99e5);
  HH (c, d, a, b, M[15], S[2][2], 0x1fa27cf8);
  HH (b, c, d, a, M[ 2], S[2][3], 0xc4ac5665);
 
  //round 4
  II (a, b, c, d, M[ 0], S[3][0], 0xf4292244);
  II (d, a, b, c, M[ 7], S[3][1], 0x432aff97);
  II (c, d, a, b, M[14], S[3][2], 0xab9423a7);
  II (b, c, d, a, M[ 5], S[3][3], 0xfc93a039);
  II (a, b, c, d, M[12], S[3][0], 0x655b59c3);
  II (d, a, b, c, M[ 3], S[3][1], 0x8f0ccc92);
  II (c, d, a, b, M[10], S[3][2], 0xffeff47d);
  II (b, c, d, a, M[ 1], S[3][3], 0x85845dd1);
  II (a, b, c, d, M[ 8], S[3][0], 0x6fa87e4f);
  II (d, a, b, c, M[15], S[3][1], 0xfe2ce6e0);
  II (c, d, a, b, M[ 6], S[3][2], 0xa3014314);
  II (b, c, d, a, M[13], S[3][3], 0x4e0811a1);
  II (a, b, c, d, M[ 4], S[3][0], 0xf7537e82);
  II (d, a, b, c, M[11], S[3][1], 0xbd3af235);
  II (c, d, a, b, M[ 2], S[3][2], 0x2ad7d2bb);
  II (b, c, d, a, M[ 9], S[3][3], 0xeb86d391);
 
  A += a;
  B += b;
  C += c;
  D += d;
}
 
// update md5,must consider the remain_.
void MD5::UpdateMd5(const uchar8 input[], const int length)
  isDone_ = false;
  totalInputBits_ += (length << 3);
  
  int start = blockLen_ - remainNum_; //blockLen_ = 64
  //copy a part of input to remain_ so it can form a block(size=64)
 
  if(start <= length)
  {
    // can form a block,then do FourRound to this block
      memcpy(&remain_[remainNum_], input, start) ;
      FourRound(remain_);
 
      int i;
      for(i = start; i <= length - blockLen_; i += blockLen_)
      {
        FourRound(&input[i]);
      }
      remainNum_ = length - i;
      memcpy(remain_, &input[i], remainNum_);
  }  
  else
  {
    // can not form a block, function return;
    memcpy(&remain_[remainNum_], input, length);
    remainNum_ += length;
  
  
}
 
void MD5::UpdateMd5(const char8 input[], const int length)
{
  UpdateMd5((const uchar8 *)input, length);
}
 
// padding with 100000... to remain_ and add the 64bit original size of input
void MD5::Finalize()
{
  if(isDone_ == true)
    return;
    
  uchar8 padding[64] = {
  0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  };
 
  int temp = 56 - remainNum_; //56 = 448/8
  if(temp > 0)
  {
    UpdateMd5(padding, temp);
    totalInputBits_ -= (temp << 3);
  }
  else if(temp < 0)
  {
    UpdateMd5(padding, 64 + temp);
    totalInputBits_ -= ((64 + temp) << 3);
  }
 
  // trans totalInputBits_ to uchar8 (64bits)
  uchar8 Bits[8];
  for(int i = 0; i < 8; i++)
  {
    Bits[i] = (totalInputBits_ >> 8*i) & 0xff;
  }
  
  UpdateMd5(Bits, 8); // add the number of original input (the last 64bits)
  
  LinkResult();
  isDone_ = true;
}
 
// comput the md5 based on input, (just this one input)
void MD5::ComputMd5(const uchar8 input[], const int length)
{
  init();
  UpdateMd5(input, length);
  Finalize();
}
 
void MD5::ComputMd5(const char8 input[], const int length)
{
  ComputMd5((const uchar8 *)input, length);
}

延伸 · 閱讀

精彩推薦
  • C/C++學習C++編程的必備軟件

    學習C++編程的必備軟件

    本文給大家分享的是作者在學習使用C++進行編程的時候所用到的一些常用的軟件,這里推薦給大家...

    謝恩銘10102021-05-08
  • C/C++C語言中炫酷的文件操作實例詳解

    C語言中炫酷的文件操作實例詳解

    內存中的數據都是暫時的,當程序結束時,它們都將丟失,為了永久性的保存大量的數據,C語言提供了對文件的操作,這篇文章主要給大家介紹了關于C語言中文件...

    針眼_6702022-01-24
  • C/C++C語言實現電腦關機程序

    C語言實現電腦關機程序

    這篇文章主要為大家詳細介紹了C語言實現電腦關機程序,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    xiaocaidayong8482021-08-20
  • C/C++詳解c語言中的 strcpy和strncpy字符串函數使用

    詳解c語言中的 strcpy和strncpy字符串函數使用

    strcpy 和strcnpy函數是字符串復制函數。接下來通過本文給大家介紹c語言中的strcpy和strncpy字符串函數使用,感興趣的朋友跟隨小編要求看看吧...

    spring-go5642021-07-02
  • C/C++深入理解goto語句的替代實現方式分析

    深入理解goto語句的替代實現方式分析

    本篇文章是對goto語句的替代實現方式進行了詳細的分析介紹,需要的朋友參考下...

    C語言教程網7342020-12-03
  • C/C++C++之重載 重定義與重寫用法詳解

    C++之重載 重定義與重寫用法詳解

    這篇文章主要介紹了C++之重載 重定義與重寫用法詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下...

    青山的青6062022-01-04
  • C/C++C/C++經典實例之模擬計算器示例代碼

    C/C++經典實例之模擬計算器示例代碼

    最近在看到的一個需求,本以為比較簡單,但花了不少時間,所以下面這篇文章主要給大家介紹了關于C/C++經典實例之模擬計算器的相關資料,文中通過示...

    jia150610152021-06-07
  • C/C++c++ 單線程實現同時監聽多個端口

    c++ 單線程實現同時監聽多個端口

    這篇文章主要介紹了c++ 單線程實現同時監聽多個端口的方法,幫助大家更好的理解和學習使用c++,感興趣的朋友可以了解下...

    源之緣11542021-10-27
主站蜘蛛池模板: 欧美日韩成人在线 | 一级一片免费看 | 成人免费小视频 | 国产欧美精品一区二区三区 | 秋霞特色aa大片 | 欧美成人一区二区 | 国产视频网 | 亚洲久草视频 | 国产精品18久久久久久久久久久久 | 久久国产精品一区二区 | 亚洲天堂中文字幕 | 日韩在线免费观看视频 | 久久久久久久久久久久一区二区 | 中文字幕在线观看一区二区 | 国产精品1 | 欧美成人免费 | 日韩免费视频 | 亚洲一区 日韩精品 中文字幕 | 午夜视频在线观看免费视频 | 红杏首页 | 在线永久免费观看黄网站 | 在线欧美| 午夜色电影 | 一区二区中文字幕 | 中文字幕亚洲一区二区va在线 | 中文字幕在线观看av | 久久99视频 | 久久久久久av | 免费福利网站 | 亚洲国产精品一区二区三区 | 日本久久综合 | 亚洲一区二区三区四区五区中文 | 爱爱免费视频 | 国产精品成人国产乱一区 | 欧美在线视频网站 | 影音先锋中文字幕在线观看 | 爆操av | 亚洲欧美精品 | 中文字幕亚洲欧美日韩在线不卡 | 91国内外精品自在线播放 | 中文字幕第一页在线 |