PHP脚本在一台Web服务器上运行,但无法在其他主Web服务器上运行

时间:2015-08-18 13:33:36

标签: php apache raspberry-pi

我目前不熟悉网络开发,并正在研究一种安全存储用户密码的方法。我偶然发现了一个关于密码散列和添加盐的页面。我下载了示例脚本和测试脚本(见下文),但我无法在家庭网络服务器(Rasberry pi Debian,Apache2 PHP5)上运行它。该脚本适用于由第三方托管服务提供商托管的实际网页。

为什么这个脚本不会在我自己的raspberry pi Web服务器上运行?任何帮助或想法将不胜感激。如果我忘记提及一些重要信息,请随时提出。

脚本运行直到" require_once(' passwordhash.php');"下面的所有东西都没有运行,所以第一个函数" create_hash()"甚至不起作用。

修改 Error_reporting给我这个:致命错误:在第46行的/var/www/hash.php中调用未定义的函数mcrypt_create_iv()

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)

2 个答案:

答案 0 :(得分: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

然后,您需要重新启动网络服务器。

答案 1 :(得分:0)

请确保您的覆盆子上的PHP版本与--with-mcrypt参数

一致

另见http://php.net/manual/en/mcrypt.installation.php

  

您需要使用--with-mcrypt [= DIR]参数编译PHP   启用此扩展程序。 DIR是mcrypt安装目录。确保   使用选项--disable-posix-threads编译libmcrypt。