md5:又稱訊息摘要演算法,對於輸入的任意長度的訊息進行產生固定長度為128位的訊息摘要,由於md5的演算法是對資料進行有失真壓縮,也就是會無法直接通過分析演算法來反向通過md5值獲取原始壓縮資料,一般是用來資料校驗和避免密碼這類的隱私資料的明碼儲存。
演算法步驟:
1.資料填充:填充訊息使其長度對512取餘等於448,填充方法:先在訊息後面新增乙個1(按bit),然後用0來填充,對於記憶體結構來看,就是在訊息其後新增乙個0x80;新增的長度最少為1,最多為512.
2.新增長度:在填充後的結果後附上64位的訊息長度,如果訊息長度超過2^64,則只使用低64位資料。第一步加上第二步第二資料填充剛好是512的倍數
3.初始化變數:標準引數:a=0x67452301h,b=0xefcdab89h,c=0x98badcfeh,d=0x10325476h,在記憶體中顯示就是0123456789abcdeffedcba9876543210
4.資料處理:以512位分組為單位處理訊息,每個分組又會被分解成16個32位的陣列,首先定義4個輔助函式:
int f(int x, int y, int z)
int g(int x, int y, int z)
int h(int x, int y, int z)
int i(int x, int y, int z)
然後將16 個資料和abcd的副本值參與到f、g、h、i的四輪變換中,運算完後在一次加上變換前的初始值作為下乙個訊息分組的計算初始值,直到所有的訊息分組均運算完成。
f、g、h、i對應的運算函式如下:
結構一致:a、b、c、d:輸入的引數(a、b、c、d) k:訊息分組被分割後的16個32位陣列值 s:迴圈左移位數 i:加法常數的id
void ff(unsigned int* a, unsigned int b, unsigned int c, unsigned int d, unsigned int k, unsigned int s, unsigned int i)
void gg(unsigned int* a, unsigned int b, unsigned int c, unsigned int d, unsigned int k, unsigned int s, unsigned int i)
void hh(unsigned int* a, unsigned int b, unsigned int c, unsigned int d, unsigned int k, unsigned int s, unsigned int i)
void ii(unsigned int* a, unsigned int b, unsigned int c, unsigned int d, unsigned int k, unsigned int s, unsigned int i)
完整演算法如下(僅適合短字串,):
#include #define rol(data,n) ((data >> (32 - n)) | (data << n))
#define big_endian_switch_little_endian(x) x << 24 | x >> 24 | ((x << 8) & 0xff0000) | ((x >> 8) & 0xff00)
int f(int x, int y, int z)
int g(int x, int y, int z)
int h(int x, int y, int z)
int i(int x, int y, int z)
void ff(unsigned int* a, unsigned int b, unsigned int c, unsigned int d, unsigned int k, unsigned int s, unsigned int i)
void gg(unsigned int* a, unsigned int b, unsigned int c, unsigned int d, unsigned int k, unsigned int s, unsigned int i)
void hh(unsigned int* a, unsigned int b, unsigned int c, unsigned int d, unsigned int k, unsigned int s, unsigned int i)
void ii(unsigned int* a, unsigned int b, unsigned int c, unsigned int d, unsigned int k, unsigned int s, unsigned int i)
int main()
; unsigned int ivalue[16] = ;
char code = "zfy1996";
unsigned int len = strlen(code);
unsigned int len1 = strlen(code)*8;
memcpy(cvalue, code, strlen(code));
cvalue[strlen(code)] = 0x80;
memcpy(ivalue, cvalue, 64);
ivalue[14] = strlen(code) * 8;
//引數初始化
unsigned int a = 0x67452301, b = 0xefcdab89, c = 0x98badcfe, d = 0x10325476;
unsigned int a = a, b = b, c = c, d = d;
unsigned int s[64] = ;
unsigned int k[64] = ;
typedef void(*func)(unsigned int*, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int);
func fun = ;
for (int i = 0; i < 4; i++)
}a += a;
b += b;
c += c;
d += d;
//小端轉大端
printf("%x%x%x%x\n",
big_endian_switch_little_endian(a),
big_endian_switch_little_endian(b),
big_endian_switch_little_endian(c),
big_endian_switch_little_endian(d));
}
Hash MD5校驗工具
hash md5是一款小巧好用的雜湊計算器,也是一款md5校驗工具。支援檔案拖放,速度很快,可以計算檔案的 md5 sha1 crc32 的值。hash md5校驗工具截圖1 截圖2hash md5校驗工具在論壇上 軟體發布時經常用,是為了保證檔案的正確性,防止一些人盜用程式,加些木馬或者篡改版權,...
單向雜湊函式,HASH演算法
md2 md4 message digest algorithm,影響了md5,sha 1等演算法 md5 已被攻破,美國已經不推薦使用。md6 在改進中,因速度問題沒有被sha 3採用。sha 0 被淘汰 sha 1 secure hash algorithm 發現sha 1使用最廣泛,但理論上可...
單向雜湊演算法運算速度實測
測試環境 centos 6.4 x86 64位 vmware虛擬機器 1g ram 物理主機cpu i7 3770 3.4ghz 測試 使用openssl的hash庫 include include include include include include using namespace st...