search
Homephp教程php手册密码散列安全,密码散

密码散列安全,密码散

Jun 13, 2016 am 08:38 AM
hashgenerallyHashSafetypasswordoftranslate

密码散列安全,密码散

  hash,一般翻译做“散列”,也有直接音译为“哈希”的,就是把任意长度的输入(又叫做预映射),通过散列算法,变换成固定长度的输出,该输出就是散列值。

  通过对密码进行散列处理,然后再保存到数据库中,这样就使得攻击者无法直接获取原始密码,同时还可以保证你的应用可以对原始密码进行相同的散列处理,然后比对散列结果。

  但是,密码散列只能保护密码不会被从数据库中直接窃取,无法保证注入到应用中的恶意代码拦截到原始密码。

  MD5、sha1,、sha256这样的散列算法是面向快速、高效进行散列处理而设计的。现代化计算机可以快速的“反转”上述散列算法的散列值,不建议在密码散列中使用这些算法。

  当进行密码散列处理的时候,有两个必须考虑的因素:计算量以及“盐”。散列算法的计算量越大,暴力破解所需的时间就越长。

  php5.5提供一个原生密码散列API ,它提供一种安全的方式来完成密码散列和验证。password_hash()函数会随机生成“盐”。

  php5.3及后续版本中,还可以使用crypt()函数,它支持多种散列算法。php提供了对应的原生实现,所有在使用此函数的时候,你需要保证所选的散列算法是你的系统所支持的。当对密码进行散列处理的时候,建议采用Blowfish算法,相比MD5或者sha1,这个算法提供了更高的计算量,同时还有良好的伸缩性。 

crypt(),采用Blowfish算法,如下:

<span>if</span> (CRYPT_BLOWFISH == 1<span>) {
    </span><span>echo</span> 'Blowfish: ' . <span>crypt</span>('rasmuslerdorf', '$2a$07$usesomesillystringforsalt$') . "\n"<span>;
}</span>

输出:

Blowfish: $2a$07<span>$usesomesillystringfore2uDLvp1Ii2e</span>./U9C8sBjqp8I90dH6hi

string crypt ( string $str [, string $salt ] )

Blowfish 算法使用如下盐值:"$2a$",一个两位 cost 参数,"$" 以及 64 位由 "./0-9A-Za-z" 中的字符组合而成的字符串。在盐值中使用此范围之外的字符将导致 crypt() 返回一个空字符串。两位 cost 参数是循环次数以 2 为底的对数,它的范围是 04-31,超出这个范围将导致 crypt() 失败。 PHP 5.3.7 之前只支持 "$2a$" 作为盐值的前缀,PHP 5.3.7 开始引入了新的前缀来修正一个在Blowfish实现上的安全风险。总而言之,开发者如果仅针对 PHP 5.3.7及之后版本进行开发,那应该使用 "$2y$" 而非 "$2a$"。

 

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.