Php crypt
From w3cyberlearnings
Contents |
PHP function crypt
This function is one way string hashing.
Syntax crypt
- string: string to be encoded
- salt(optional): String to use to increase the number of characters encoded in order for the encoding for more secure.
crypt(string,salt)
Example 1
<?php $mypassword="pass$2122012"; // let the salt to be automatically generated $myencrypted_pass = crypt($mypassword); echo 'My unencrypted password: '. $mypassword . '<br/>encrypted:'.$myencrypted_pass; ?>
Output
My unencrypted password: pass$2122012 encrypted:$1$dW3.1i5.$hPk2K2euxw/Rla0FY9gQC/
Example 2
<?php $salt="#232343cesalt"; $mypassword="pass$2122012"; $myencrypted_pass = crypt($mypassword,$salt); echo 'Encrypted and salt:'.$myencrypted_pass; ?>
Output
Encrypted and salt:#2bS51/5SJjcs
Example 3
<?php // user login $salt = "#232343cesalt"; $mypassword = "pass$2122012"; $myencrypted_pass = '#2bS51/5SJjcs'; if (crypt($mypassword, $salt) == $myencrypted_pass) { echo "password is correct"; } ?>
Output
password is correct
Example 4
<?php if (CRYPT_STD_DES == 1) { echo 'Standard DES: ' . crypt('secret password', 'rl') . "<br/>"; } else { echo 'Standard DES DOES NOT SUPPORT <br/>'; } if (CRYPT_EXT_DES == 1) { echo 'Extended DES: ' . crypt('secret password', '_J9..rasm') . "<br/>"; } else { echo 'Extended DES DOES NOT SUPPORT<br/>'; } if (CRYPT_MD5 == 1) { echo 'MD5: ' . crypt('secret password', '$1$rasmusle$') . "<br/>"; } else { echo 'MD5 DOES NOT SUPPORT <br/>'; } if (CRYPT_BLOWFISH == 1) { echo 'Blowfish: ' . crypt('secret password', 'Joing200f9=325=5fdfilshR$@@@@') . "<br/>"; } else { echo 'Blowfish DOES NOT SUPPORT<br/>'; } if (CRYPT_SHA256 == 1) { echo 'SHA-256: ' . crypt('secret password', '$6$rounds=80$gotringforsalt$') . "<br/>"; } else { echo 'SHA-256 DOES NOT SUPPORT<br/>'; } if (CRYPT_SHA512 == 1) { echo 'SHA-512: ' . crypt('secret password', '$6$rounds=80$ueclforsalt$') . "<br/>"; } else { echo 'SHA-512 Does not support<br/>'; } ?>
Output
Standard DES: rlaEv0jJ6KOY. Extended DES: _J9..rasmuZlhptpYmYs MD5: $1$rasmusle$Fw/9CTt3Hy/.X3Zo5anW3/ Blowfish: JokA23yF8wWYk SHA-256: $6$rounds=1000$gotringforsalt$LbMhyJiI/nEiWg/AEWTMAFj 0QK9wryQZ0Xp1h1jVZCoPmRjZZTYmAQADr71PHLEtYacv4hqxzWJiqLe6yAqbC1 SHA-512: $6$rounds=1000$ueclforsalt$hoYaKBBznaMiRIYIc kXTVbnlJw5HaCrPdPtwJuuhfDUxqpYkdo9WMlr.xwvb./g54/AZ