How to use php crypt function
php crypt函数用于返回使用DES、Blowfish或MD5算法加密的字符串,其语法是crypt(str,salt),参数str必需,指规定要编码的字符串。
php crypt函数怎么用?
定义和用法
crypt() 函数返回使用 DES、Blowfish 或 MD5 算法加密的字符串。
在不同的操作系统上,该函数的行为不同,某些操作系统支持一种以上的算法类型。在安装时,PHP 会检查什么算法可用以及使用什么算法。
确切的算法依赖于 salt 参数的格式和长度。salt 可以通过增加由一个特定字符串与一个特定的加密方法生成的字符串的数量来使得加密更安全。
这里有一些和 crypt() 函数一起使用的常量。这些常量值是在安装时由 PHP 设置的。
常量:
● [CRYPT_SALT_LENGTH] - 默认的加密长度。使用标准的 DES 加密,长度为 2
● [CRYPT_STD_DES] - 标准的基于 DES 加密有 2 个字符的 salt,来自字母表 "./0-9A-Za-z"。在 salt 中使用无效的字符将引发函数失败。
● [CRYPT_EXT_DES] - 扩展的基于 DES 加密有 9 个字符的 salt,由 1 个下划线,后边跟 4 个字节的迭代次数和 4 个字节的 salt 组成。这些被编码为可打印字符,每个字符 6 位,最低有效字符优先。值 0 到 63 被编码为 "./0-9A-Za-z"。在 salt 中使用无效的字符将引发函数失败。
● [CRYPT_MD5] - MD5 加密有 12 个字符的 salt,以 $1$ 开始。
● [CRYPT_BLOWFISH] - Blowfish 加密有一个以 $2a$、$2x$ 或 $2y$ 开始的 salt,一个两位数的 cost 参数 "$",以及来自字母表 "./0-9A-Za-z" 中的 22 个字符。使用字母表以外的字符将引发函数返回一个长度为 0 的字符串。"$" 参数是以 2 为底的基于 Blowfish 散列算法的迭代次数的对数,必须在 04-31 范围内。在该范围以外的值将引发函数失败。
● [CRYPT_SHA_256] - SHA-256 加密有 16 个字符的 salt,以 $5$ 开始。如果 salt 字符串以 "rounds=
● [CRYPT_SHA_512] - SHA-512 加密有 16 个字符的 salt,以 $6$ 开始。 如果 salt 字符串以 "rounds=
在该函数支持多种算法的系统上,上面的常量如果支持则设置为 "1",否则设置为 "0"。
注释:没有相应的解密函数。crypt() 函数使用一种单向算法。
语法
crypt(str,salt)
参数
str 必需。规定要编码的字符串。
salt 可选。用于增加被编码字符数目的字符串,以使编码更加安全。如果未提供 salt 参数,则每次调用该函数时会随机生成一个。
返回值: 返回加密字符串,如果失败则返回一个小于 13 个字符并保证不同于 salt 的字符串。
PHP 版本: 4+
更新日志:
在 PHP 5.3.7 中,新增了 $2x$ 和 $2y$ Blowfish 模式,用来处理潜在的高位攻击。
在 PHP 5.3.2 中,新增了常量 SHA-256 和 SHA-512。
自 PHP 5.3.2 起,Blowfish 在无效的循环将返回 "failure" 字符串("*0" 或 "*1"),而不是后退到 DES。
自 PHP 5.3.0 起,PHP 自带 MD5 加密实现、标准 DES 实现、扩展 DES 实现以及 Blowfish 算法。如果系统不支持上述的算法,将使用 PHP 自带的算法实现。
实例 1
<?php $hashed_password = crypt('mypassword'); // 自动生成盐值 /* 你应当使用 crypt() 得到的完整结果作为盐值进行密码校验,以此来避免使用不同散列算法导致的问题。(如上所述,基于标准 DES 算法的密码散列使用 2 字符盐值,但是基于 MD5 算法的散列使用 12 个字符盐值。)*/ if (hash_equals($hashed_password, crypt($user_input, $hashed_password))) { echo "Password verified!"; } ?>
实例 2
利用 htpasswd 进行 crypt() 加密:
<?php // 设置密码 $password = 'mypassword'; // 获取散列值,使用自动盐值 $hash = crypt($password); ?>
实例
在本实例,我们以不同散列类型使用:
<?php if (CRYPT_STD_DES == 1) { echo 'Standard DES: ' . crypt('rasmuslerdorf', 'rl') . "\n"; } if (CRYPT_EXT_DES == 1) { echo 'Extended DES: ' . crypt('rasmuslerdorf', '_J9..rasm') . "\n"; } if (CRYPT_MD5 == 1) { echo 'MD5: ' . crypt('rasmuslerdorf', '$1$rasmusle$') . "\n"; } if (CRYPT_BLOWFISH == 1) { echo 'Blowfish: ' . crypt('rasmuslerdorf', '$2a$07$usesomesillystringforsalt$') . "\n"; } if (CRYPT_SHA256 == 1) { echo 'SHA-256: ' . crypt('rasmuslerdorf', '$5$rounds=5000$usesomesillystringforsalt$') . "\n"; } if (CRYPT_SHA512 == 1) { echo 'SHA-512: ' . crypt('rasmuslerdorf', '$6$rounds=5000$usesomesillystringforsalt$') . "\n"; } ?>
上面的代码输出如下(取决于操作系统):
Standard DES: rl.3StKT.4T8M Extended DES: _J9..rasmBYk8r9AiWNc MD5: $1$rasmusle$rISCgZzpwk3UhDidwXvin0 Blowfish: $2a$07$usesomesillystringfore2uDLvp1Ii2e./U9C8sBjqp8I90dH6hi SHA-256: $5$rounds=5000$usesomesillystri$KqJWpanXZHKq2BOB43TSaYhEWsQ1Lr5QNyPCDH/Tp.6 SHA-512: $6$rounds=5000$usesomesillystri$D4IrlXatmP7rx3P3InaxBeoomnAihCKRVQP22JZ6EY47Wc6BkroIuUUBOov1i.S5KPgErtP/EN5mcO.ChWQW21
The above is the detailed content of How to use php crypt function. For more information, please follow other related articles on the PHP Chinese website!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics





The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

PHP 8's JIT compilation enhances performance by compiling frequently executed code into machine code, benefiting applications with heavy computations and reducing execution times.

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

The article discusses symmetric and asymmetric encryption in PHP, comparing their suitability, performance, and security differences. Symmetric encryption is faster and suited for bulk data, while asymmetric is used for secure key exchange.

The article discusses implementing robust authentication and authorization in PHP to prevent unauthorized access, detailing best practices and recommending security-enhancing tools.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies to prevent CSRF attacks in PHP, including using CSRF tokens, Same-Site cookies, and proper session management.
