PHP crypt() Function
The crypt () function in PHP returns a hashed string using DES, Blowfish, or MD5 algorithms.
Some constants of crypt() function are as follows:
- [CRYPT_STD_DES]
- [CRYPT_EXT_DES]
- [CRYPT_MD5]
- [CRYPT_BLOWFISH]
- [CRYPT_SHA_256]
- [CRYPT_SHA_512] etc.
Syntax
1 2 3 |
crypt ( string $str [, string $salt ] ) |
Parameter
str(required)- This parameter represents the string to be hashed
salt(optional)- This parameter represents the salt string to base the hashing on.
Return
This function returns the hashed string or a string that is shorter than 13 characters.
Example 1
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php // 2 character salt if (CRYPT_STD_DES == 1) { echo "Standard DES: ".crypt('something','st')."\n"; } else { echo "Standard DES not supported.\n"; } ?> |
Output
1 2 3 |
Standard DES: stqAdD7zlbByI |
Example 2
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php // 12 character salt starting with $1$ if (CRYPT_MD5 == 1) { echo "MD5: ".crypt('tutorialandexample','$1$tutorialandexample$')."\n"; } else { echo "MD5 not supported.\n"; } ?> |
Output
1 2 3 |
MD5: $1$tutorial$h33eIOE.o5xIc2MCML1M2/ |
Example 3
1 2 3 4 5 6 7 8 9 10 |
<?php // 16 character salt starting with $5$. The default number of rounds is 5000. if (CRYPT_SHA256 == 1) { echo "SHA-256: ".crypt('tutorialandexample','$565$rounds=98$tutorialandexamplestringforsalt$')."\n"; } else{ echo "SHA-256 not supported.\n"; } ?> |
Output
1 2 3 |
SHA-256: $5c8GnoWJydmI |
Example 4
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php // 16 character salt starting with $6$. 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."; } ?> |
Output
1 2 3 |
SHA-512: $6$rounds=5000$anexamplestringf$Oo0skOAdUFXkQxJpwzO05wgRHG0dhuaPBaOU/oNbGpCEKlf/7oVM5wn6AN0w2vwUgA0O24oLzGQpp1XKI6LLQ0 |
Example 5
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php // 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"; } else { echo "SHA-256 not supported.\n"; } ?> |
Output
1 2 3 |
SHA-256: $5$rounds=5000$anexamplestringf$KIrctqsxo2wrPg5Ag/hs4jTi4PmoNKQUGWFXlVy9vu9 |