简体   繁体   English

如何重新哈希存储在我的数据库中的密码? (PHP)

[英]How to ReHash a password stored into my Database ? (PHP)

I have some passwords encrypted in my database and I would like to find a way to display them. 我的数据库中已加密了一些密码,我想找到一种显示密码的方法。 Here is how they are saved into my mysql database: 这是它们如何保存到我的mysql数据库中的方法:

function generateHash($plainText, $salt = null){

                if ($salt === null)
  {
   $salt = substr(md5(uniqid(rand(), true)), 0, 25);
  }
  else
  {
   $salt = substr($salt, 0, 25);
  }

  return $salt . sha1($salt . $plainText);
 }

        $secure_pass = generateHash($this->clean_password);

Then $secure_pass is saved into my database. 然后$ secure_pass保存到我的数据库中。

Anyone would have an idea ?? 任何人都会有一个主意?

Thank you very much ;) 非常感谢你 ;)

You must never display a password. 您绝不能显示密码。
You must never display a password. 您绝不能显示密码。
You must never display a password. 您绝不能显示密码。

The entire point of hashing a password is to make this impossible. 散列密码的全部目的是使此操作变得不可能。

Since you're using a somewhat insecure hash, it's a little bit less impossible, but you still can't do it. 由于您使用的是某种不安全的哈希,因此不太可能,但仍然无法做到。

You should be use SHA512 instead of SHA1 to make this more impossible. 您应该使用SHA512而不是SHA1来使此操作变得更不可能。

The point of a cryptographical hash is that it's neigh-impossible to reverse the operation. 密码哈希的要点是它不可能逆向操作。 So basically the answer here is no, you cannot. 因此,基本上,这里的答案是否定的,你不能。

there are an infinite number of values that will produce any single hash, as such there is no way to reproduce with certainty the original password that was given. 有无数个会产生任何哈希值的值,因此无法确定地复制给出的原始密码。

you can however display working passwords by one of the following methods: 但是,您可以通过以下方法之一显示工作密码:

Cavet: Obviously, if security is important to you, you should do none of the above. Cavet:显然,如果安全性对您很重要,则您不应执行上述任何一项操作。 if you want to allow users who forgot their password I suggest you read about password resetting. 如果您想允许忘记密码的用户,建议您阅读有关密码重置的信息。

You can't reverse a hash function sooo... you left with two options: 你不能反向推散列函数...留下了两个选择:

1 Force on user to insert new password or... 1强制用户插入新密码或...

2 Update the hash as users login to your system again (You can force to kick the cookie and sessions that allow user to login without retyping their password). 2在用户再次登录到系统时更新哈希(您可以强制执行cookie和允许用户登录而无需重新输入密码的会话)。 This solution will allow your users to log in with the old hash and at the same time you will update the old hash to new one. 该解决方案将允许您的用户使用旧哈希来登录,同时您将旧哈希更新为新哈希。 Next time your user will log in, the script will use new version of hash to login the user. 下次您的用户登录时,脚本将使用新版本的hash来登录用户。

In this example I have used md5 as a hash I want to update to BCRYPT with cost = 12 but feel free to change it to what ever you need. 在此示例中,我将md5用作哈希,我想使用cost = 12更新到BCRYPT,但可以随时将其更改为所需的值。 Change from BCRYPT cost=10 to BCRYPT cost = 12 would also work or any other combination. 从BCRYPT成本= 10更改为BCRYPT成本= 12也可以,也可以是任何其他组合。 Consider this example: 考虑以下示例:

$passwordFromDatabase = "0d107d09f5bbe40cade3de5c71e9e9b7"; // md5  hash of "letmein"
$passwordFromForm = $_POST['password']; // $_POST['password'] == "letmein"

if(password_needs_rehash($passwordFromDatabase, PASSWORD_BCRYPT, ["cost" => 12]) && md5($passwordFromForm) === $passwordFromDatabase){
    // generate new password
    $newPasswordHash = password_hash($passwordFromForm, PASSWORD_BCRYPT, ["cost" => 12]);
    // update hash from databse - replace old hash $passwordFromDatabase with new hash $newPasswordHash
    // after update login user
    if(password_veryfi($passwordFromForm, $newPasswordHash)){
        // user has loged in successfuly and hash was updated
        // redirect to user area
    }else{
        // ups something went wrong Exception
    }
}else{
    if($password_veryfi($passwordFromForm, $passwordFromDatabase)){
        // user password hash from database is already BCRYPTed no need to rehash
        // user has loged in successfuly
        // redirect to user area
    }else{
        // wrong password
        // no access granted - stay where you are
    }
}

I prefer the second option :). 我更喜欢第二种选择:)。 Make your own choice. 自行选择。 If you pick the second option and choose not to kick the cookie and session that allow user to login without providing the password, its ok too... The change will happen overtime. 如果您选择第二个选项,并且选择不踢出允许用户在不提供密码的情况下登录的cookie和会话,那也可以...更改将随着时间的流逝而发生。 And no one will even notice the change. 而且没有人会注意到这一变化。

You've run them through SHA1 - the original passwords are destroyed and unrecoverable by any practical means. 您已经通过SHA1运行了它们-原始密码已被破坏,并且无法通过任何实际方法恢复。 You COULD try to find another string that produces the same SHA1, but that'd be more effort than it's worth. 您可以尝试查找另一个产生相同SHA1的字符串,但这要花很多功夫。

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

相关问题 如何使用md5重新哈希存储在数据库中的密码? - How can I rehash my stored passwords which is stored in Database using md5? 如何在PHP 5.5中使用password_needs_rehash函数 - How to use the password_needs_rehash function in PHP 5.5 laravel 4 sentry 2如何更改密码和rehash - laravel 4 sentry 2 how to change password and rehash 如何让我的PHP文件正确地从数据库中调用密码? - How to make my PHP files call the password from the database properly? 如何解密存储在drupal到wordpress数据库中的密码? - How to decrypt the password stored in drupal to wordpress database? 我的登录功能无法匹配数据库中存储的用户名和密码 - My login function is unable to match username and password stored in the database PHP如何保护数据库中的密码 - PHP how to secure password in database 考虑到我的图像链接存储在MySQL数据库中,如何通过php显示存储在文件夹中的图像 - How to display through php an image stored in a folder considering that my image's link is stored in MySQL database 如何使用PHP更改数据库中的SALT密码? - How to change a SALT password in a database using PHP? 我如何以数据形式存储在我的sql数据库中的最后一个条目作为php / html中的输出 - How to i take data form last entry stored in my sql database as output in php/html
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM