我创建了我的第一个类,但我在将对象转换回字符串方面遇到了问题。
class Cryption
{
var $data;
var $salt;
function __construct($data, $salt)
{
$this->data = $data;
$this->salt = $salt;
}
function sha512()
{
$sodium = 'Na';
return hash_hmac("sha512", $this->data . $this->salt, $sodium);
}
function encrypt()
{
$salt = substr(sha512(($this->key), 'brownies'), 0, 30);
return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $salt, $this->data, MCRYPT_MODE_CBC, md5($salt)));
}
当我使用它时:
$password = new Cryption(mysql_real_escape_string(trim($_POST['password'])), 'pepper');
$password->sha512();
这里显示“PHP Catchable fatal error: Object of class Cryption could not be converted to string(PHP可捕获致命错误:无法将Cryption类的对象转换为字符串)”。
我不知道如何将它转换回字符串。请有经验的开发者帮帮我吗?
谢谢。
编辑:
<?php
require("config.php");
include("includes/cryption/cryption.php");
$username = mysql_real_escape_string(trim($_POST['username']));
$password = new Cryption(mysql_real_escape_string(trim($_POST['password'])), 'pepper'); //use a different salt next time such as a special salt for each user
$password->sha512();
$result = mysql_query("SELECT * FROM `administrators` WHERE username='$username' and password='$password'");
$row = mysql_fetch_row($result);
$count = mysql_num_rows($result);
if ($count == 1) {
if (isset($_POST['remember'])) {
session_start();
$_SESSION['user'] = array(
'id' => $row[0],
'username' => $row[1],
'password' => $row[2]
);
$userid = new Cryption($_SESSION['user']['id'], 'kkfishing');
$session = new Cryption($_SESSION['user']['username'], 'kkfishing');
$validated = new Cryption($_SESSION['user']['password'], 'kkfishing');
setcookie("uniqueid", $userid->encrypt(), time() + 60 * 60 * 24 * 100, "/"); //100 days
setcookie("kksessionid", $session->encrypt(), time() + 60 * 60 * 24 * 100, "/");
setcookie("kkuserid", $validated->encrypt(), time() + 60 * 60 * 24 * 100, "/");//disguised cookie name
}
session_start();
$_SESSION['authenticated'] = $row[0];
echo '1'; //true
exit;
}
else
{
echo '0'; //false
exit;
}
?>