首页 / 知识

关于php:加密密码

2023-04-11 13:55:00

关于php:加密密码

Encrypting Passwords

最快(最安全)的密码加密方式是什么(最好在PHP中),对于您选择的哪种方法,它都是可移植的吗?

换句话说,如果我以后将网站迁移到其他服务器,我的密码会继续有效吗?

我被告知,我现在使用的方法取决于服务器上安装的库的确切版本。


如果您为登录系统选择一种加密方法,那么速度不是您的朋友,杰夫和托马斯·帕塔塞克(Thomas Ptacek)经常碰到密码问题,结论是您应该使用最慢,最安全的加密方法负担得起。

From Thomas Ptacek's blog:
Speed is exactly what you dona€?t want in a password hash function.

Modern password schemes are attacked with incremental password crackers.

Incremental crackers dona€?t precalculate all possible cracked passwords. They consider each password hash individually, and they feed their dictionary through the password hash function the same way your PHP login page would. Rainbow table crackers like Ophcrack use space to attack passwords; incremental crackers like John the Ripper, Crack, and LC5 work with time: statistics and compute.

The password attack game is scored in time taken to crack password X. With rainbow tables, that time depends on how big your table needs to be and how fast you can search it. With incremental crackers, the time depends on how fast you can make the password hash function run.

The better you can optimize your password hash function, the faster your password hash function gets, the weaker your scheme is. MD5 and SHA1, even conventional block ciphers like DES, are designed to be fast. MD5, SHA1, and DES are weak password hashes. On modern CPUs, raw crypto building blocks like DES and MD5 can be bitsliced, vectorized, and parallelized to make password searches lightning fast. Game-over FPGA implementations cost only hundreds of dollars.


我和彼得在一起。开发人员似乎不了解密码。我们都选择(而且我也对此感到内))MD5或SHA1,因为它们速度很快。考虑一下(因为有人最近向我指出了这一点)没有任何意义。我们应该选择一个很慢的哈希算法。我的意思是,从规模上看,繁忙的网站会散列密码吗?每1/2分钟?谁在乎0.8秒与0.03秒服务器是否明智?但是这种额外的速度非常慢,无法阻止所有类型的常见蛮力攻击。

根据我的阅读,bcrypt是专为安全密码散列而设计的。它基于河豚,并且有很多实现。

对于PHP,请查看PHP Pass

对于使用.NET的任何人,请查看BCrypt.NET


应该指出,您不想加密密码,而希望对其进行哈希处理。

加密的密码可以解密,让别人看到密码。哈希是一种单向操作,因此用户的原始密码(通过密码)消失了。

关于应该选择哪种算法-使用当前接受的标准算法:

  • SHA-256

当您对用户密码进行哈希处理时,请确保也对它进行哈希处理。例如:

  • 密码:password1
  • salt:PasswordSaltDesignedForThisQuestion

将salt添加到用户密码:

1
String s = HashStringSHA256("password1PasswordSaltDesignedForThisQuestion");

无论您做什么,都不要编写自己的加密算法。这样做几乎可以保证(除非您是密码学家)该算法中将存在一个漏洞,使其难以破解。


考虑使用bcrypt,它已在laravel等许多现代框架中使用。


我并不一定要寻求最快的速度,而是要找到一个不错的平衡,为此代码开发的某些服务器运行速度相当慢,散列和存储密码的脚本需要5到6秒钟才能运行,并且我将其范围缩小到了散列(如果我对散列进行注释,它会在1-2秒内运行)。

它不一定是最安全的,我现在不为银行而设,但我当然不会将密码存储为纯文本。


在数据库中插入时使用此功能
Password_harsh($ password,PASSWORD_DEFAULT);
从数据库中进行选择时,您可以使用以下功能将要插入的密码与数据库中的密码进行比较
if(password_verify($ password,$ databasePassword)){

1
2
3
}else{
echo"password not correct";
}

这将以一种安全的格式加密密码


password_hash ( string $password , int $algo [, array $options ] )。 (PHP 5> = 5.5.0,PHP 7)

password_hash()使用强大的单向哈希算法创建新的密码哈希。 password_hash()crypt()兼容。因此,crypt()创建的密码哈希可以与password_hash()一起使用。


密码加密方法选择

最新内容

相关内容

热门文章

推荐文章

标签云

猜你喜欢