SHA512 哈希 MYSQL/PHP

SHA512 Hashing MYSQL/PHP

很抱歉这个简单的问题,但我仍在学习php/mysql

我正在尝试在注册时对密码进行哈希处理,这很有效,但是一旦哈希处理完成,用户就无法使用他们的密码登录。下面是我的变量的一小段

  $password = hash('sha512',$_POST['password']); 
  $confirmPassword = hash('sha512',$_POST['confirmPassword']);

->因此,密码与确认密码匹配,然后使用 Javascript 和 PHP 进行比较,如果匹配则将数据插入数据库。那么为什么不允许用户登录呢?

目前在我的数据库中,我的密码列设置为 CHAR(128) 是否有帮助?

您的密码栏的大小太small.Do一个简单的:

echo sizeof(hash('sha512',$_POST['confirmPassword']));

并获得正确大小的数据库列。

使用单个未加盐的 SHA512 以这种方式散列密码是不安全的。 PHP 提供专用函数 password_hash() and password_verify() 来正确完成这项工作:

// Hash a new password for storing in the database.
// The function automatically generates a cryptographically safe salt.
$hashToStoreInDb = password_hash($_POST['password'], PASSWORD_DEFAULT);

// Check if the hash of the entered login password, matches the stored hash.
// The salt and the cost factor will be extracted from $existingHashFromDb.
$isPasswordCorrect = password_verify($_POST['password'], $existingHashFromDb);

要检查密码和它的确认是否相等,你应该直接比较它们(明文),你计算哈希之前。

建议使用 varchar(255) 类型的数据库字段来存储此哈希,以备将来使用。