2012-10-13 49 views
5

我使用簡單md5($password);格式,但我想添加鹽這樣我怎麼能做到這一點?如何在用戶密碼中添加salt?

這裏是我的代碼:

if($success) 
    { 
     $data['firstname'] = $firstname; 
     $data['lastname'] = $lastname; 
     $data['username'] = $username; 
     $data['password'] = md5($password); 
     $data['email'] = $email; 


     $newUser = new User($data); 
     $newUser->save(true); 
     $Newuser->login($username, $password); 
     header("Location: welcome.php"); 

    } 
+3

閱讀:[你應該知道密碼安全性](http://crackstation.net/hashing-security.htm) – NullUserException

+0

謝謝我正在通過它 – deerox

+0

我想建議不要使用md5,而是更多現代哈希算法。 – Aleph

回答

7
$data['hashedpwd'] = md5($salt . $password); 

的更長,更復雜和獨特的每個用戶就可以使鹽就越難將是任何人都可以得到密碼(雖然這不是不可能的)。

一個簡單的(但差的鹽)是:$salt = '10';
更強的鹽是:$salt = '-45dfeHK/[email protected]/klF21-1_\/4JkUP/4';

鹽是用戶唯一的甚至更好。

正如一些評論所提到的MD5是一個古老而相對較差的散列algorythm,SHA-512或任何SHA-2家族會好得多的選擇。

更多細節見this salting question

+0

感謝它現在是完美的 – deerox

+0

使用MD5的不良業力。 – Sven

+0

好點,我只用它,因爲這個問題,我會添加到我的答案。 – SteB

0

下面是一個例子:(這裏使用您的密碼)

$pass = 'abcxyz123'; // password 
    $salt = '}#f4ga~g%7hjg4&j(7mk?/!bj30ab-wi=6^7-$^R9F|GK5J#E6WT;IO[JN'; // random string 

    $hash = md5($pass); 
    $hash_md5 = md5($salt . $pass); 

    // echo now 
    echo 'Original Password: ' . $pass . '<br><br>'; 
    echo 'Original Salt: ' . $salt . '<br><br>'; 
    echo 'MD5: ' . $hash . '<br><br>'; 
    echo 'MD5 with Salt: ' . $hash_md5 . '<br><br>'; 
+0

我會試一試這樣的感謝:) – deerox

1
$salt="Your text vatiable"; 
$data[passsord] = md5($salt . $password); 
2

其他人已經表明它是如何完成的,所以對一般的原則......

像已經通過註釋中的NullUserException鏈接,如果安全性很重要,則不應該通過向md5添加靜態鹽來實現。 Note however that you can store your salt as-is。靜鹽和md5都不被認爲是非常安全的方法。如果沒有那麼重要,它們就沒關係,但如果它確實如此,你就應該走另一條路。

About static salt:

常見的錯誤是使用相同的鹽在每個散列。鹽 被硬編碼到程序中,或者隨機生成一次。這是 無效,因爲如果兩個用戶具有相同的密碼,他們仍然會有 具有相同的散列。攻擊者仍然可以使用反向查找表 攻擊同時對每個散列運行字典攻擊。他們 只需要將salt應用於每個密碼猜測之前,他們就會散列 它。如果鹽硬編碼到一個受歡迎的產品,查找表 和彩虹表可建爲鹽,使之更容易被產品產生裂紋 哈希值。

一個新的隨機鹽必須在每次用戶創建一個帳戶 或改變其密碼時生成。

如果鹽是靜態的,攻擊者可以爲所有可能性生成一個彩虹表。用鹽對用戶來說是獨一無二的,這樣做毫無意義。

MD5是專爲計算速度,所以它的fundamentally not the way to go。另外,它已經有很多pre-created rainbow tables了,還有很多online cracking tools

鏈接的頁面也包含PHP source code,作爲如何正確完成此操作的示例。

<?php 
/* 
* Password hashing with PBKDF2. 
* Author: havoc AT defuse.ca 
* www: https://defuse.ca/php-pbkdf2.htm 
*/ 

// These constants may be changed without breaking existing hashes. 
define("PBKDF2_HASH_ALGORITHM", "sha256"); 
define("PBKDF2_ITERATIONS", 1000); 
define("PBKDF2_SALT_BYTES", 24); 
define("PBKDF2_HASH_BYTES", 24); 

define("HASH_SECTIONS", 4); 
define("HASH_ALGORITHM_INDEX", 0); 
define("HASH_ITERATION_INDEX", 1); 
define("HASH_SALT_INDEX", 2); 
define("HASH_PBKDF2_INDEX", 3); 

function create_hash($password) 
{ 
    // format: algorithm:iterations:salt:hash 
    $salt = base64_encode(mcrypt_create_iv(PBKDF2_SALT_BYTES, MCRYPT_DEV_URANDOM)); 
    return PBKDF2_HASH_ALGORITHM . ":" . PBKDF2_ITERATIONS . ":" . $salt . ":" . 
     base64_encode(pbkdf2(
      PBKDF2_HASH_ALGORITHM, 
      $password, 
      $salt, 
      PBKDF2_ITERATIONS, 
      PBKDF2_HASH_BYTES, 
      true 
     )); 
} 

function validate_password($password, $good_hash) 
{ 
    $params = explode(":", $good_hash); 
    if(count($params) < HASH_SECTIONS) 
     return false; 
    $pbkdf2 = base64_decode($params[HASH_PBKDF2_INDEX]); 
    return slow_equals(
     $pbkdf2, 
     pbkdf2(
      $params[HASH_ALGORITHM_INDEX], 
      $password, 
      $params[HASH_SALT_INDEX], 
      (int)$params[HASH_ITERATION_INDEX], 
      strlen($pbkdf2), 
      true 
     ) 
    ); 
} 

// Compares two strings $a and $b in length-constant time. 
function slow_equals($a, $b) 
{ 
    $diff = strlen($a)^strlen($b); 
    for($i = 0; $i < strlen($a) && $i < strlen($b); $i++) 
    { 
     $diff |= ord($a[$i])^ord($b[$i]); 
    } 
    return $diff === 0; 
} 

/* 
* PBKDF2 key derivation function as defined by RSA's PKCS #5: https://www.ietf.org/rfc/rfc2898.txt 
* $algorithm - The hash algorithm to use. Recommended: SHA256 
* $password - The password. 
* $salt - A salt that is unique to the password. 
* $count - Iteration count. Higher is better, but slower. Recommended: At least 1000. 
* $key_length - The length of the derived key in bytes. 
* $raw_output - If true, the key is returned in raw binary format. Hex encoded otherwise. 
* Returns: A $key_length-byte key derived from the password and salt. 
* 
* Test vectors can be found here: https://www.ietf.org/rfc/rfc6070.txt 
* 
* This implementation of PBKDF2 was originally created by https://defuse.ca 
* With improvements by http://www.variations-of-shadow.com 
*/ 
function pbkdf2($algorithm, $password, $salt, $count, $key_length, $raw_output = false) 
{ 
    $algorithm = strtolower($algorithm); 
    if(!in_array($algorithm, hash_algos(), true)) 
     die('PBKDF2 ERROR: Invalid hash algorithm.'); 
    if($count <= 0 || $key_length <= 0) 
     die('PBKDF2 ERROR: Invalid parameters.'); 

    $hash_length = strlen(hash($algorithm, "", true)); 
    $block_count = ceil($key_length/$hash_length); 

    $output = ""; 
    for($i = 1; $i <= $block_count; $i++) { 
     // $i encoded as 4 bytes, big endian. 
     $last = $salt . pack("N", $i); 
     // first iteration 
     $last = $xorsum = hash_hmac($algorithm, $last, $password, true); 
     // perform the other $count - 1 iterations 
     for ($j = 1; $j < $count; $j++) { 
      $xorsum ^= ($last = hash_hmac($algorithm, $last, $password, true)); 
     } 
     $output .= $xorsum; 
    } 

    if($raw_output) 
     return substr($output, 0, $key_length); 
    else 
     return bin2hex(substr($output, 0, $key_length)); 
} 
+0

這真的很有幫助評論非常感謝 – deerox

+0

我會使用bcrypt over PBKDF2在PHP – CodesInChaos

2

PHP 5.5將帶來一個非常容易使用的密碼哈希API,它目前正在重新實現能與PHP開始5.3.7版本馬上使用。

我可以複製&粘貼的解釋,但看到自己:https://github.com/ircmaxell/password_compat

總之,它是這樣的:

/* Create the hash */ 
$hash = password_hash($password, PASSWORD_BCRYPT, array('cost' => 10)); 

/* Store the hash... then user comes back */ 
if (password_verify($password, $hash)) { 
    /* Valid */ 
} else { 
    /* Invalid */ 
} 

/* 
* If the security level has to be increased to new $options or a new $algorithm, 
* silent rehashing is supported 
*/ 
if (password_verify($password, $hash)) { 
    if (password_needs_rehash($hash, $algorithm, $options)) { 
     $hash = password_hash($password, $algorithm, $options); 
     /* Store new hash in db */ 
    } 
} 

哈希可能只要255個字符(不與bcrypt,但接下來可能會發生),請相應地規劃存儲。數據庫應該使用VARCHAR(255)。

相關問題