2015-08-18 58 views
-1

我想複製下面的代碼在PHP中有一個基於Web的帳戶創建腳本,但我真的不明白在md5哈希之前發生了什麼。任何人都可以解釋這個C密碼哈希算法?

這是代碼:

reg_seconds = (unsigned) regtime/3600L; 
ch = strlen (&password[0]); 
_itoa (reg_seconds, &config_data[0], 10); 
//Throw some salt in the game ;) 
sprintf (&password[ch], "_%s_salt", &config_data[0]); 
//printf ("New password = %s\n", password); 
MDString (&password[0], &MDBuffer[0]); 
for (ch=0;ch<16;ch++) 
    sprintf (&md5password[ch*2], "%02x", (unsigned char) MDBuffer[ch]); 
md5password[32] = 0; 

當使用密碼「密碼」和一個regtime「399969」我知道「9a5c041c5b37febc90ad3dc66ec62c83」的哈希密碼

誰能解釋一下到底正在發生以及最終的字符串是什麼被散列?

+1

你有什麼問題一步理解? – Martin

+0

'ch = strlen(&password [0]);' - 爲什麼? – errikos

+0

這是一個很好的例子,我們應該如何**不**散列密碼。我知道這不是您的代碼,但請注意,這些密碼不受保護,只會被模糊處理。 – martinstoeckli

回答

1

好讓我們看一行行,假設密碼= 「密碼」 和regtime = 399969

reg_seconds = (unsigned) regtime/3600L; 
=> reg_seconds = 111 NB incoherent with the name, isn't is regtime % 3600L 
ch = strlen (&password[0]); 
=> ch = 8 
_itoa (reg_seconds, &config_data[0], 10); 
=> config_data = "111" 
//Throw some salt in the game ;) 
sprintf (&password[ch], "_%s_salt", &config_data[0]); 
=> password = "password_111_salt" 
//printf ("New password = %s\n", password); Why did not you uncomment this ? 
MDString (&password[0], &MDBuffer[0]); 
MDBuffer receives the binary hash 
for (ch=0;ch<16;ch++) 
    sprintf (&md5password[ch*2], "%02x", (unsigned char) MDBuffer[ch]); 
md5password[32] = 0; 
md5password receives the hex encoded hash : "033f7d591eda915e708571edd255b511" 

Oups它不是期望的散列!

因爲399969是 regtime但reg_seconds在上面的代碼......所以

password = "password_399969_salt" 
md5password = "9a5c041c5b37febc90ad3dc66ec62c83" 
+0

完美,謝謝!這不是我的代碼,我只是想了解它。看到最後一個字符串使它很容易理解:) – Kida

相關問題