都说md5不安全,貌似很多时候说是用sha1()进行加密更加安全,甚至还有用一种叫bcrypt的算法,各位phper平时都是怎么选择的呢?
回复内容:
都说md5不安全,貌似很多时候说是用sha1()进行加密更加安全,甚至还有用一种叫bcrypt的算法,各位phper平时都是怎么选择的呢?
题主你可以使用 md5
或者 sha1
进行初步处理,但为了更加安全,请你同时加上两个 salt
,一个静态 salt
,一个动态的 salt
。以 md5
为例:
假设通过 POST
传来的密码为 $_POST['password']
,在存入 DB
前先进行如下的操作:
<code>$password = hash('md5', $_POST['password'].$staticSalt.$dynamicSalt); </code>
为了保证动态 salt
的唯一性,可以这样操作:
<code>$dynamicSalt = hash('md5', microtime()); </code>
对于动态的 salt
可以与生成的密码一起保存在 DB
中,而静态 salt
则可以直接放在类文件中(例如定义为一个静态属性即可)。
首先谢谢题主采纳了我的答案,但是我之前的回答并不是最佳答案,之所以有此加密的想法源于自己所读的源码可能比较老,所以并没使用上较新版本的加密方法,例如 bcrypt
等。
此外,第二点,感谢评论中几位前辈的提点,已经明白设置静态 salt
的意义并不大,生成一个较长的动态 salt
已然可以解决问题。
LZ应该采用加盐HASH。
如何“腌制”密码呢?
=_,=
正确的格式应该是,用户password+动态的salt
动态的salt不能像2L所说的,使用microtime,因为时间在某些情况下不够随机,而且是可能被猜解的。
这里推荐一个我用的加盐HASH
$salt=base64_encode(mcrypt_create_iv(32,MCRYPT_DEV_RANDOM)); $password=sha1($register_password.$salt);
解释:
首先使用mcrypt,产生电脑随机生成的,专门用户加密的随机数函数。
第二步,把得到的随机数通过base64加密,使其变长并且不利于猜解。
第三步,把得出的盐拼接到密码的后面,再对其使用sha1进行哈希
再把password存入到用户的数据库。
PS:为何不用静态的salt?没有必要,使用一个动态随机足够长的盐足矣。
为何不用MD5?因为长度不够。
为何没有使用多次HASH?因为这样反而容易发生碰撞。
HASH好之后怎么使用“腌制”好的密码?
用户注册->提交密码->产生salt->腌制好的密码存入数据库->salt存入数据库。
用户登录->提交密码->调用salt接到提交密码的后面->进行HASH->调用之前注册腌制好的密码->对比HASH值是否和这个密码相同
最后推荐一篇文章,我是从中获益的,希望LZ也有所收获。
http://blog.jobbole.com/61872/
可以试试 phpass
对密码进行哈希相对安全的方法是使用bcrypt算法。开源的phpass库以一个易于使用的类来提供此功能。
这是我以前写的一篇关于密码加密的博文,欢迎参考。
http://www.wkii.org/save-user-password-use-bcrypt-or-pbkdf2.html
另外,请参考php5.5的内置函数
http://www.php.net/manual/zh/function.hash-pbkdf2.php
我写的pbkdf2与php5.5的函数输出结果一致。

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver CS6
Visual web development tools
