PHP crypt() Function
PHP String Reference Manual
Definition and Usage
The crypt() function returns an encrypted string using the DES, Blowfish or MD5 algorithm.
On different operating systems, this function behaves differently; some operating systems support more than one algorithm type. At install time, PHP checks which algorithms are available and which to use.
The exact algorithm depends on the format and length of the salt parameter. The salt can make the encryption more secure by increasing the number of strings generated by a specific string combined with a specific encryption method.
Here are some constants used with the crypt() function. These constant values are set by PHP at install time.
Constants:
- [CRYPT_SALT_LENGTH] - Default encryption length. With standard DES encryption, the length is 2
- [CRYPT_STD_DES] - Standard DES-based encryption has a 2-character salt from the alphabet "./0-9A-Za-z". Using an invalid character in the salt will cause the function to fail.
- [CRYPT_EXT_DES] - Extended DES-based encryption has a 9-character salt consisting of 1 underscore followed by 4 bytes of iteration count and 4 bytes of salt. These are encoded as printable characters, 6 bits per character, least significant character first. Values 0 to 63 are encoded as "./0-9A-Za-z". Using an invalid character in the salt will cause the function to fail.
- [CRYPT_MD5] - MD5 encryption has a 12-character salt starting with $1$.
- [CRYPT_BLOWFISH] - Blowfish encryption has a salt starting with $2a$, $2x$ or $2y$, a two-digit cost parameter "$", and 22 characters from the alphabet "./0-9A-Za-z". Using a character outside the alphabet will cause the function to return a string of length 0. The "$" parameter is the base-2 logarithm of the iteration count of the Blowfish hash algorithm and must be in the range 04-31. Values outside this range will cause the function to fail.
- [CRYPT_SHA_256] - SHA-256 encryption has a 16-character salt starting with $5$. If the salt string starts with "rounds=
$", the numeric value of N represents the number of hash loop executions, similar to the cost parameter in Blowfish. The default number of loops is 5000, the minimum is 1000, and the maximum is 999,999,999. Any value of N outside this range will be converted to the nearest boundary value. - [CRYPT_SHA_512] - SHA-512 encryption has a 16-character salt starting with $6$. If the salt string starts with "rounds=
$", the numeric value of N represents the number of hash loop executions, similar to the cost parameter in Blowfish. The default number of loops is 5000, the minimum is 1000, and the maximum is 999,999,999. Any value of N outside this range will be converted to the nearest boundary value.
On systems where this function supports multiple algorithms, the constants above are set to "1" if supported, otherwise "0".
**Note:** There is no corresponding decryption function. The crypt() function uses a one-way algorithm.
Syntax
crypt(
str,salt
)
| Parameters | Description |
|---|---|
| str | Required. Specifies the string to encode. |
| salt | Optional. A string used to increase the number of encoded characters, making the encoding more secure. If the salt parameter is not provided, a random one is generated each time the function is called. |
Technical Details
| Return Value: | Returns the encrypted string, or on failure a string of less than 13 characters that is guaranteed to be different from the salt. |
|---|---|
| PHP Version: | 4+ |
| Changelog: | In PHP 5.3.7, the $2x$ and $2y$ Blowfish modes were added to handle potential high-bit attacks. |
在 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 自带的算法实现。 |
Example
Example 1
Example 2
Use htpasswd for crypt() encryption:
Example 1
In this example, we use with different hash types:
The output of the above code is as follows (depending on the operating system):
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
PHP String Reference Manual