简体   繁体   English

如何在 postgresql 中使用 bcrypt 重新散列我的用户密码,而不在 php 中迭代它们?

[英]How I can re-hash my user's a password using bcrypt in postgresql without iterating them in php?

In my database I use a weak hash, SHA1, for the password.在我的数据库中,我使用弱 hash,SHA1 作为密码。 I want to migrate them into a stronger hash (bcrypt).我想将它们迁移到更强大的 hash (bcrypt)。 Usually it is done via the following php script:通常通过以下 php 脚本完成:

$sql = "SELECT user_id,password from users";
/**
* @var $pdo database connection
*/
$statement = $pdo->prepare('UPDATE users SET password = :password WHERE user_id = :user_id");

foreach( $pdo->query($sql) as $user)
{
   $password  = password_hash($user['password'],PASSWORD_DEFAULT);
   $statement->bindParam(':password',$password);   
   $statement->bindParam(':user_id',$user['user_id']);
   $stmt->execute();
}

But on large datasets will take too long to update.但是在大型数据集上更新将花费太长时间。 Therefore do you know a faster way to hash all user passwords in postgresql with a postgresql-native password updating?因此,您是否知道使用 postgresql 本地密码更新 postgresql 中的所有用户密码 hash 的更快方法?

Also the code above will run in a migration script and it may cause some delays in deployment as well.此外,上面的代码将在迁移脚本中运行,它也可能导致部署延迟。

In order to update the passwords en marre run the following sql:为了更新密码,运行以下 sql:

UPDATE users SET password = crypt(users.password,gen_salt('bf')), double_hash=true;

Ensure password field is at least 72 characters long.确保密码字段长度至少为 72 个字符。 Upon sucessfull login rehash the provided password using password_hash .成功登录后,使用password_hash

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM