Welcome, guest | Sign In | My Account | Store | Cart

Generate an alpha-numeric password salt (with a default of 32 characters)

PHP, 24 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?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
 * @author Jayesh Sheth <js_scripts@fastmail.fm>
 * Inspired by: http://code.activestate.com/recipes/576894-generate-a-salt/?in=lang-php
 */
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";
*/
?>

Generate a random string / password / salt with minimal code. Inspired by: http://code.activestate.com/recipes/576894-generate-a-salt/?in=lang-php

Created by Jayesh Sheth on Thu, 25 Feb 2010 (MIT)
PHP recipes (51)
Jayesh Sheth's recipes (1)

Required Modules

  • (none specified)

Other Information and Tasks