PHP 脚本 运行 在一台网络服务器上运行,但无法在其他家庭网络服务器上 运行
PHP script running on one web server, but fails to run on other home web server
我目前是 Web 开发的新手,正在研究一种安全的用户密码存储方式。我偶然发现了一个关于密码散列和加盐的页面。我下载了示例脚本和测试脚本(见下文),但无法在我的家庭 Web 服务器(raspberry pi Debian、Apache2 PHP5)上运行。该脚本确实适用于由第 3 方托管提供商托管的我的实际网页。
为什么这个脚本不会 运行 在我自己的 raspberry pi 网络服务器上?任何帮助或想法将不胜感激。如果我忘记提及一些重要信息,请随时询问。
脚本 运行 直到 "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)
尝试为 PHP 安装 Mcrypt。
运行 在终端中:
sudo apt-get install php5-mcrypt
然后您需要启用 mcrypt 扩展,这可以通过以下变体来完成:
mv -i /etc/php5/conf.d/mcrypt.ini /etc/php5/mods-available/
php5enmod mcrypt
然后您需要重新启动网络服务器。
请确保您的PHP树莓派版本与--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 .
我目前是 Web 开发的新手,正在研究一种安全的用户密码存储方式。我偶然发现了一个关于密码散列和加盐的页面。我下载了示例脚本和测试脚本(见下文),但无法在我的家庭 Web 服务器(raspberry pi Debian、Apache2 PHP5)上运行。该脚本确实适用于由第 3 方托管提供商托管的我的实际网页。
为什么这个脚本不会 运行 在我自己的 raspberry pi 网络服务器上?任何帮助或想法将不胜感激。如果我忘记提及一些重要信息,请随时询问。
脚本 运行 直到 "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)
尝试为 PHP 安装 Mcrypt。
运行 在终端中:
sudo apt-get install php5-mcrypt
然后您需要启用 mcrypt 扩展,这可以通过以下变体来完成:
mv -i /etc/php5/conf.d/mcrypt.ini /etc/php5/mods-available/
php5enmod mcrypt
然后您需要重新启动网络服务器。
请确保您的PHP树莓派版本与--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 .