Latest web development tutorials

PHP crypt () function

PHP String Reference PHP String Reference

Definition and Usage

crypt () function returns the use of DES, Blowfish, or MD5 algorithm to encrypt a string.

On different operating systems, different behavior of the function, some operating systems support more than one type of algorithm. When installing, PHP will check what is available and what the algorithm algorithm.

The exact algorithms rely on the format and length of the salt argument. salt can make the encryption more secure by increasing the number by a particular string with a specific encryption method to generate the string.

Here are some constants and crypt () function is used with. The constant value is set at installation by the PHP.

constant:

  • [CRYPT_SALT_LENGTH] - default encryption length. Using standard DES encryption, a length of 2
  • [CRYPT_STD_DES] - DES encryption based on a 2-character salt standard, from the alphabet "./0-9A-Za-z". Invalid characters in the salt will lead to function failed.
  • [CRYPT_EXT_DES] - Extended DES-based encryption have salt 9 characters by an underscore behind with 4 bytes of iteration count and 4 bytes of salt composition. These are encoded as printable characters, each of 6, least significant character first. 0-63 value is encoded as "./0-9A-Za-z". Invalid characters in the salt will lead to function failed.
  • [CRYPT_MD5] - MD5 encryption have salt 12 characters, beginning with $ 1 $.
  • [CRYPT_BLOWFISH] - Blowfish encryption has a $ 2a $, $ 2x $ or $ 2y $ beginning of salt, a double-digit cost parameter, "$", as well as from the alphabet "./0-9A-Za-z" 22 characters. Use other than alphabet characters will trigger function returns a string of 0's. "$" Parameter is the base 2 logarithm of the number of iterations Blowfish hashing algorithm must be in the 04-31 range. Values ​​outside this range will lead to function failed.
  • [CRYPT_SHA_256] - SHA-256 encryption has salt 16 characters, beginning with $ 5 $. If the salt string "rounds = <N> $" starts, the digital value of N times the hash used to indicate the loop is executed, which is similar to the cost parameter Blowfish. The default number of cycles is 5000, the minimum value is 1000, the maximum is 999,999,999. N is any value outside this range will be converted to the nearest boundary value.
  • [CRYPT_SHA_512] - SHA-512 encryption has salt 16 characters, beginning with $ 6 $. If the salt string "rounds = <N> $" starts, the digital value of N times the hash used to indicate the loop is executed, which is similar to the cost parameter Blowfish. The default number of cycles is 5000, the minimum value is 1000, the maximum is 999,999,999. N is any value outside this range will be converted to the nearest boundary value.

On This function supports multiple algorithms system, if supported above constant is set to "1", otherwise it is set to "0."

Note: There isno corresponding decryption function. crypt () function uses a one-way algorithm.


grammar

crypt( str,salt )

参数 描述
str 必需。规定要编码的字符串。
salt 可选。用于增加被编码字符数目的字符串,以使编码更加安全。如果未提供 salt 参数,则每次调用该函数时会随机生成一个。

technical details

return value: Returns encrypted string, if it fails to return less than 13 characters and a guarantee of the string is different from the salt.
PHP version: 4+
Update log: In PHP 5.3.7, add the $ 2x $ and $ 2y $ Blowfish modes to handle high potential attack.

In PHP 5.3.2, add the constant SHA-256 and SHA-512.

Since PHP 5.3.2 onwards, Blowfish in an invalid loop will return "failure" string ( "* 0" or "* 1"), rather than back to DES.

Starting from PHP 5.3.0, PHP comes with MD5 encryption to achieve, achieve the standard DES, Blowfish and extended DES algorithm to achieve. If the system does not support these algorithms will use PHP's own algorithm.


Examples

Example 1

In this example, we will test different algorithms:

<?php
// 2 character salt
if (CRYPT_STD_DES == 1)
{
echo "Standard DES: ".crypt('something','st')."n<br>";
}
else
{
echo "Standard DES not supported.n<br>";
}

// 4 character salt
if (CRYPT_EXT_DES == 1)
{
echo "Extended DES: ".crypt('something','_S4..some')."n<br>";
}
else
{
echo "Extended DES not supported.n<br>";
}

// 12 character salt starting with $1$
if (CRYPT_MD5 == 1)
{
echo "MD5: ".crypt('something','$1$somethin$')."n<br>";
}
else
{
echo "MD5 not supported.n<br>";
}

// Salt starting with $2a$. The two digit cost parameter: 09. 22 characters
if (CRYPT_BLOWFISH == 1)
{
echo "Blowfish: ".crypt('something','$2a$09$anexamplestringforsalt$')."n<br>";
}
else
{
echo "Blowfish DES not supported.n<br>";
}

// 16 character salt starting with $5$. The default number of rounds is 5000.
if (CRYPT_SHA256 == 1)
{
echo "SHA-256: ".crypt('something','$5$rounds=5000$anexamplestringforsalt$')."n<br>"; }
else
{
echo "SHA-256 not supported.n<br>";
}

// 16 character salt starting with $5$. The default number of rounds is 5000.
if (CRYPT_SHA512 == 1)
{
echo "SHA-512: ".crypt('something','$6$rounds=5000$anexamplestringforsalt$');
}
else
{
echo "SHA-512 not supported.";
}
?>

The code above outputs the following (depending on the operating system):

Standard DES: stqAdD7zlbByI
Extended DES: _S4..someQXidlBpTUu6
MD5: $1$somethin$4NZKrUlY6r7K7.rdEOZ0w.
Blowfish: $2a$09$anexamplestringforsaleLouKejcjRlExmf1671qw3Khl49R3dfu
SHA-256: $5$rounds=5000$anexamplestringf$KIrctqsxo2wrPg5Ag/hs4jTi4PmoNKQUGWFXlVy9vu9
SHA-512: $6$rounds=5000$anexamplestringf$Oo0skOAdUFXkQxJpwzO05wgRHG0dhuaPBaOU/
oNbGpCEKlf/7oVM5wn6AN0w2vwUgA0O24oLzGQpp1XKI6LLQ0.



PHP String Reference PHP String Reference