最有效的方法来更改密码的散列类型(md5到sha1) [英] Most efficient way to change the hash type of a password (md5 to sha1)

查看:173
本文介绍了最有效的方法来更改密码的散列类型(md5到sha1)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个系统使用MD5来散列来自我的用户的密码并将其存储到我的数据库中。
现在,我正在改用另一个使用SHA1的系统(以及一个独特的系统SALT,而不是用户唯一的)来对密码进行哈希处理。

我设法将用户的旧MD5密码转换为我的新SHA1密码?

您无法将 md5 sha 但是当用户<时,用户只会使用密码登录所以你可以修改你的脚本来自动更新

  //用户还没有被授权
$ auth = false;
$ updated = false;

//从您的登录表单
$ user = $ _POST ['user'];
$ pass = $ _POST ['pass'];

//检查用户名是否有更新密码
$ udated = false; //不更新

//我希望你总是这样做
$ password = $ updated? md5($ pass):sha1($ pass);

//执行认证
//从数据库中选择
//检查数据
//设置认证
$ auth = true;

//然后如果($ auth == true&&!$ updated)chage密码
{
$ newpassword = sha1($ pass);
//连接到DB
//更新密码
//在DB
$ udated = true中设置状态更新;
}

//如果($ auth == true&&!$ updated){
$ newpassword = password_hash($ password,PASSWORD_BCRYPT );
//连接到DB
//更新密码
//在DB
$ updated = true中设置状态更新;
}

我使用 password_hash 有一个更好的方法,因为它使用 BCRYPT 这是一个更好的散列算法。 查看更多关于password_compat的信息


I have a system using MD5 to hash passwords from my users and store it into my database. Now, I'm changing to another system that uses SHA1 (and a unique system SALT, not user-unique) to hash the passwords.

How do I manage to get the user old MD5 password turned into my new SHA1 password with PHP?

解决方案

You can not convert md5 to sha but really your users only use password when they are about to login so you can modify your script a little to do the update automatically

// The user is not authticated yet
$auth = false;
$updated = false;

// From your Login form
$user = $_POST['user'];
$pass = $_POST['pass'];

// Check If the username has update password
$udated = false; // not update

// I gues you always do this
$password = $updated ? md5($pass) : sha1($pass);

// Do the autentication
// Slect from Database
// Check the data
// Set auth
$auth = true;

// Then chage the password
if ($auth == true && !$updated) {
    $newpassword = sha1($pass);
    // Connect to DB
    // Update the Password
    // Set Status to Updated in DB
    $udated = true;
}

// Better Approch
if ($auth == true && !$updated) {
    $newpassword = password_hash($password, PASSWORD_BCRYPT);
    // Connect to DB
    // Update the Password
    // Set Status to Updated in DB
    $updated = true;
}

I used password_hash has a better approach because it uses BCRYPT which is a better hash algorithm. See more information on password_compat

这篇关于最有效的方法来更改密码的散列类型(md5到sha1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆