Showing posts with label PHP md5. Show all posts
Showing posts with label PHP md5. Show all posts

Monday, 23 February 2015

PHP: Generate an alpha-numeric password salt

<?php
/**
 * This function generates an alpha-numeric password salt (with a default of 32 characters)
 * @param $max integer The number of characters in the string
 *
 */
function generateSalt($max = 32) {
$baseStr = time() . rand(0, 1000000) . rand(0, 1000000);
$md5Hash = md5($baseStr);
if($max < 32){
$md5Hash = substr($md5Hash, 0, $max);
}
return $md5Hash;
}

//Usage:
/*
echo "Salt with 32 characters:\n";
echo generateSalt() . "\n";
echo "Salt with 5 characters:\n";
echo generateSalt(5) . "\n";
*/
?>

Monday, 29 September 2014

md5 in PHP

PHP md5() function is utilized to the calculated MD5 hash (the hash as a 32-character hexadecimal number) of a specified string.
PHP md5() function uses the "RSA Data Security, Inc. md5 Message-Digest Algorithm" and returns that hash.

Syntax for PHP md5() function

md5(string,raw)
string : Required. The input string

raw : Optional. Defines hex or binary output format: 
             TRUE – Returns the digest in raw binary format with a length of 16. 
            FALSE – Default. Returns the digest in raw hex format with a length of 32.  


Example on PHP md5() function

<?php
$str_name = "Good Day";
echo md5($str_name);
?>
Output will be:
e18d0d486c1d7d3a5fb02e637c0455ff