Thursday, 13 October 2011

crypt and md5 usage in php

MD5 Encryption :

$str = "Hello";
echo md5($str);

if (md5($str) == '8b1a9953c4611296a827abf8c47804d7')
{
echo "
Hello world!";
exit;
}

The output of the above code would be :
8b1a9953c4611296a827abf8c47804d7
Hello world!

Crypt Encryption :

$password = crypt('mypassword'); // let the salt be automatically generated

/* You should pass the entire results of crypt() as the salt for comparing a
password, to avoid problems when different hashing algorithms are used. (As
it says above, standard DES-based password hashing uses a 2-character salt,
but MD5-based hashing uses 12.) */
if (crypt($user_input, $password) == $password) {
echo "Password verified!";
}