Current Location: Home> Function Categories> crypt

crypt

One-way string hash
Name:crypt
Category:String
Programming Language:php
One-line Description:One-way string encryption method (hashing).

Definition and usage

crypt() function returns a string encrypted using the DES, Blowfish, or MD5 algorithm.

The function behaves differently on different operating systems, and some operating systems support more than one algorithm type. During installation, PHP checks what algorithms are available and what algorithms are used.

The specific algorithm depends on the format and length of the salt parameter. By increasing the number of strings generated by a specific string using a specific encryption method, salt can make encryption safer.

Here are some constants used with crypt() function. These constant values ​​are set by PHP at installation time.

constant:

[CRYPT_SALT_LENGTH] The default encryption length. Use standard DES encryption with length 2
[CRYPT_STD_DES] Hash based on the standard DES algorithm uses two characters in the "./0-9A-Za-z" character as the salt value. Using illegal characters in salt values ​​will cause crypt() to fail.
[CRYPT_EXT_DES] Extended hashing based on DES algorithm. A string with a salt value of 9 characters consists of 1 underscore followed by a 4-byte cycle and a 4-byte salt value. They are encoded into printable characters, each character has 6 bits, with the least significant bits preferred. 0 to 63 are encoded as "./0-9A-Za-z". Using illegal characters in salt values ​​will cause crypt() to fail.
[CRYPT_MD5] The MD5 hash uses a 12-character string salt value starting with $1$.
[CRYPT_BLOWFISH] The Blowfish algorithm uses the following salt value: "$2a$", a two-bit cost parameter, "$" and a 64-bit string composed of characters in "./0-9A-Za-z". Using characters outside this range in a salt value will cause crypt() to return an empty string. The two-bit cost parameter is a logarithm with the number of loops at the base 2, and its range is 04-31. Exceeding this range will cause crypt() to fail.
CRYPT_SHA256 The SHA-256 algorithm hashed using a 16-character string salt value starting with $5$. If the salt value string begins with "rounds=<N>$", the numeric value of N will be used to specify the number of executions of the hash loop, which is much like the cost parameter of the Blowfish algorithm. The default number of loops is 5000, the minimum is 1000, and the maximum is 999,999,999. N outside this range will be converted to the closest value.
CRYPT_SHA512 The SHA-512 algorithm uses a 16-character string salt value starting with $6$ for hashing. If the salt value string begins with "rounds=<N>$", the numeric value of N will be used to specify the number of executions of the hash loop, which is much like the cost parameter of the Blowfish algorithm. The default number of loops is 5000, the minimum is 1000, and the maximum is 999,999,999. N outside this range will be converted to the closest value.

On systems where this function supports multiple algorithms, if the above constant is supported, it is set to "1", otherwise it is set to "0".

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

Example

In this example, we will test different algorithms:

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

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

//12 characters 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$. Cost parameters of double numbers: 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 circumference 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 circumference is 5000.
if ( CRYPT_SHA512 == 1 )
{
echo "SHA-512: " . crypt ( 'something' , '$6$rounds=5000$anexamplestringforsalt$' ) ;
}
else
{
echo "SHA-512 not supported." ;
}
?>

The output of the above code (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.

grammar

 crypt ( str , salt )
parameter describe
str Required. Specifies the string to be encoded.
salt Optional. Used to increase the number of encoded characters to make encoding more secure. If the salt parameter is not provided, PHP will randomly generate one each time the function is called.