2010-02-10 101 views
4

首先,我看到要使用CRYPT_BLOWFISH,我需要用$ 2a $開頭的16個字符的鹽。但php.net documentation for crypt()表示某些系統不支持CRYPT_BLOWFISH。這種情況多久?(PHP)如何在CRYPT_BLOWFISH中使用crypt()?

接下來,他們對文檔的例子,我看到我用的crypt()如下:

<?php 
$password = crypt('mypassword'); // let the salt be automatically generated 

/* You should pass the entire results of crypt() as the salt for comparing a 
    password, to avoid problems when different hashing algorithms are used. (As 
    it says above, standard DES-based password hashing uses a 2-character salt, 
    but MD5-based hashing uses 12.) */ 
if (crypt($user_input, $password) == $password) { 
    echo "Password verified!"; 
} 
?> 

爲了使用crypt_blowfish的,我會需要修改的唯一的事情是第一線,使它是這樣的;

crypt('mypassword', '$2a$07$usesomesillystringforsalt$') 

然後其餘的行都很好嗎?

回答

5

對於5.3.0之前的PHP,crypt()使用了OS提供的lib。如果您使用的是早期版本,那麼您需要檢查操作系統文檔以查看它是否受支持(檢查CRYPT_BLOWFISH常量的值) - 如果不是,則該算法在PHP的mcrypt()擴展中實現。

你從文檔報價似乎沒有多大意義的例子:

$stored_password=fetch_password($user); 

    if (crypt($_REQUEST['password'],$stored_password)===$stored_password) { 
     // note that crypt automatically extracts the salt and alogrithm type 
     // from $stored_password 
     .... 

你只需要在創建密碼時指定的前綴($ 2A $)。

HTH

C.

+0

是的,但只有我離開的問題是:當我創建的密碼,我使用的crypt( '輸入mypassword', '$ 2A $ $ 07 usesomesillystringforsalt $'),對不對?我的鹽實際上是一個隨機生成的16個字符串? – sepiroth 2010-02-10 11:23:19