Home >>PHP String Functions >PHP crypt() Function

PHP crypt() Function

PHP crypt() Function

PHP crypt() function is an in-built function of PHP. Crypt() function converts any given input string in to hash string. For the conversion, this function uses different algorithms like MD5, SHA256, or Blowfish. On different operating systems this crypt() function behaves differently.

The crypt() function doesn’t have any counter function for decryption.

Syntax :

crypt($string, $salt)

The $string parameter is the given input string which is to be hashed. This parameter is mandatory.The salt parameter is not mandatory, it’s optional but without this $salt crypt() function creates a weak password. So we try to give a strong salt value for strong security.

There are also some constants that are used With the crypt() function we also use some constants whose values are defined at the time of installation of PHP.

  • [CRYPT_SHA_512] - SHA-512 hash which has a 16 character salt starting with 66.
  • [CRYPT_MD5] - MD5 hashing which has a 12 character salt starting with 11.
  • [CRYPT_BLOWFISH] - Blowfish hashing which has a salt starting with 2x2x, or 2y2y, a two digit cost parameters "$", and 22 characters from the alphabet.
  • [CRYPT_SHA_256] - SHA-256 hash which has a 16 character salt starting with 55.
  • [CRYPT_STD_DES] - Standard DES-based hash which has two character salt from the alphabet.

Let's see an example for crypt() function:

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

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

if (CRYPT_MD5 == 1)
{
echo "MD5: ".crypt('PHPTPOINT','$1$somethin$')."\n<br><br>";
}
else
{
echo "MD5 not supported.\n<br>";
}

if (CRYPT_BLOWFISH == 1)
{
echo "Blowfish: ".crypt('PHPTPOINT','$2a$09$anexamplestringforsalt$')."\n<br><br>";
}
else
{
echo "Blowfish DES not supported.\n<br>";
}

if (CRYPT_SHA512 == 1)
{
echo "SHA-512: ".crypt('PHPTPOINT','$6$rounds=5000$anexamplestringforsalt$')."\n<br><br>";
}
else
{
echo "SHA-512 not supported.";
}

if (CRYPT_SHA256 == 1)
{
echo "SHA-256: ".crypt('PHPTPOINT','$5$rounds=5000$anexamplestringforsalt$')."\n<br><br>"; }
else
{
echo "SHA-256 not supported.\n<br>";
}

?>
Output:
Standard DES:  stxmBiiYmh7pI
Extended DES:  _S4..somerDvmdD/PmC.
MD5:  $1$somethin$GA4ckKRuM3E5rq8U8mXDl.
Blowfish:  $2a$09$anexamplestringforsaleyquYOT6deAyd1trMgh1H1J150cnbQam
SHA-512:  $6$rounds=5000$anexamplestringf$HoJbnhS6A4T6v8RChpEYYWP1uHyLFViq.eGWVqwQVglkSC4jh0pt07AylxcSosNGu/c6mBhNwZ2s/ffIw5d3V0
SHA-256:  $5$rounds=5000$anexamplestringf$XNo5mypnz38N9LGTn3fvAwPevjbAEQwNOUT1uCG.et4

PHP String Functions PHP addcslashes() Function PHP addslashes() Function PHP bin2hex() Function PHP chop() Function PHP chr() Function PHP chunk_split() Function PHP convert_cyr_string() Function PHP convert_uudecode() Function PHP convert_uuencode() Function PHP count_chars() Function PHP crc32() Function PHP crypt() Function PHP echo() Function PHP empty() function PHP explode() Function PHP strcmp() Function PHP fprintf() Function PHP strcoll() Function PHP get_html_translation_table() Function PHP strcspn() Function PHP hebrev() Function PHP strip_tags() Function PHP hebrevc() Function PHP hex2bin() Function PHP html_entity_decode() Function PHP htmlentities() Function PHP htmlspecialchars() Function PHP htmlspecialchars_decode() Function PHP implode() Function PHP join() Function PHP lcfirst() Function PHP levenshtein() Function PHP localeconv() Function PHP ltrim() Function PHP md5() Function PHP md5_file() Function PHP metaphone() Function PHP money_format() Function PHP nl_langinfo() Function PHP nl2br() Function PHP number_format() Function PHP ord() Function PHP parse_str() Function PHP print() Function PHP printf() Function PHP quoted_printable_decode() Function PHP quoted_printable_encode() Function PHP quotemeta() Function PHP rtrim() Function PHP setlocale() Function PHP sha1() Function PHP sha1_file() Function PHP similar_text() Function PHP soundex() Function PHP sprintf() Function PHP sscanf() Function PHP str_getcsv() Function PHP str_ireplace() Function PHP str_pad() Function PHP str_repeat() Function PHP str_replace() Function PHP str_rot13() Function PHP str_shuffle() Function PHP str_split() Function PHP str_word_count() Function PHP strcasecmp() Function PHP strchr() Function
No Sidebar ads