2015-08-18 206 views
0

我目前還不熟悉web開發,並且正在對存儲用戶密碼的安全方法進行一些研究。我偶然發現了一個關於密碼哈希和添加鹽的頁面。我下載了示例腳本和測試腳本(見下文),但是我無法在我的家庭網絡服務器上工作(rapberry pi Debian,Apache2 PHP5)。該腳本可以在由第三方託管服務提供商託管的我的實際網頁上工作。在一臺Web服務器上運行的PHP腳本,但無法在其他主Web服務器上運行

這個腳本怎麼會不會在我自己的樹莓派網絡服務器上運行?任何幫助或想法將不勝感激。如果我忘記提及一些重要信息,請隨時提問。

腳本運行到「require_once('passwordhash.php');」下面的所有內容都不會運行,所以第一個函數「create_hash()」甚至不起作用。

編輯: 的error_reporting給了我這樣的:致命錯誤:在/var/www/hash.php調用未定義功能mcrypt_create_iv()上線46

test.php的:

<?php 
require_once('PasswordHash.php'); 
$hash = create_hash("foobar"); 
$result = validate_password("foobar", $hash); 
if ($result) 
{ 
    echo "Good"; 
} 
else 
{ 
    echo "Bad"; 
} 
$result = validate_password("barfoo", $hash); 
if ($result) 
{ 
    echo "Bad"; 
} 
else 
{ 
    echo "Good"; 
} 
echo "\n"; 
echo $hash; 
?> 

PasswordHash.php:

<?php 

define("PBKDF2_HASH_ALGORITHM", "sha1"); 
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, 
     base64_decode($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, 
     base64_decode($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; 
} 

function pbkdf2($algorithm, $password, $salt, $count, $key_length, $raw_output = false) 
{ 
$algorithm = strtolower($algorithm); 
if(!in_array($algorithm, hash_algos(), true)) 
    trigger_error('PBKDF2 ERROR: Invalid hash algorithm.', E_USER_ERROR); 
if($count <= 0 || $key_length <= 0) 
    trigger_error('PBKDF2 ERROR: Invalid parameters.', E_USER_ERROR); 
if (function_exists("hash_pbkdf2")) { 
    // The output length is in NIBBLES (4-bits) if $raw_output is false! 
    if (!$raw_output) { 
     $key_length = $key_length * 2; 
    } 
    return hash_pbkdf2($algorithm, $password, $salt, $count, $key_length, $raw_output); 
} 
$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)); 
} 
?> 

(腳本學分去crackstation.net)

+1

「不工作」是不夠的診斷。啓用error_reporting查看任何內容,否則添加調試語句。 (不管怎樣,你最好切換到內置的['password_hash'](http://php.net/password_hash)/ ['password_verify'](http://php.net/password_verify)。) – mario

+0

您的Rasberry Pi可能不包含所有必需的庫。像@mario提到的那樣,請包含所有的調試信息。 – Tro

+0

@mario我添加了當我啓用error_reporting時收到的錯誤。謝謝你的提示。 – Joey040

回答

0

嘗試安裝Mcrypt for PHP。在終端

運行: sudo apt-get install php5-mcrypt

然後,您需要啓用mcrypt擴展,可以通過一些變種來實現: mv -i /etc/php5/conf.d/mcrypt.ini /etc/php5/mods-available/ php5enmod mcrypt

然後,您將需要重新啓動你的web服務器。

+0

在終端中運行此操作會導致以下情況:無法找到程序包php-mcrypt – Joey040

+0

對不起,我錯過了'5',現在再試一次。 – Tro

+0

謝謝,這個工程。你能解釋一下爲什麼我不得不添加這個軟件包才能正常工作嗎?當我在我自己的私人服務器上遇到其他問題時可能會很有用。 – Joey040

0

請確保在樹莓你的PHP版本是compied與--with-mcrypt的參數

參見http://php.net/manual/en/mcrypt.installation.php

You need to compile PHP with the --with-mcrypt[=DIR] parameter to enable this extension. DIR is the mcrypt install directory. Make sure you compile libmcrypt with the option --disable-posix-threads .

+0

您不需要編譯php安裝,因爲debian的apt-get允許您安裝包作爲一個補充到PHP。由於大多數人使用apt或yum這樣的經理,他們沒有選擇安裝時編譯的內容。 – Auris

相關問題