My Ride
Started: Friday 24th May 2013 4:33pm
Distance: 17.46km
Duration: 00:39:15
Rest Time: 00:06:05
Climb: 124m
Max Speed: 51.48kmph
Average Speed: 26.68kmphInstagrams
-
Recent Posts
Recent Comments
- Arie on Tag Time: CakePHP Tag Plugin
- Paul on Sign Me Up A CakePHP User Registration Plugin
- Paul on Sign Me Up A CakePHP User Registration Plugin
- Watch The Big Bang Theory season 6 episode 13 on On My Tv: With Trakt.tv
- veloura et bellagenix on Ultimate Guestbook Tutorial: How to build a Guestbook with a honeypot, error checking, IP banning, pagination, e-mail notification and smilies with PHP and mySQL
Archives
- February 2013
- December 2012
- September 2012
- July 2012
- January 2012
- September 2011
- August 2011
- February 2011
- January 2011
- November 2010
- October 2010
- August 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
Categories
Meta
PHP: Random Salt String Generator
September 13, 2009,
7,562 views
As i develop new CakePHP projects i like to spin the wheel with generating a “secure” salt string to be used in core.php. Instead of just bashing my head against the keyboard hoping for some random, usable, 40 character long string, i decided to let php do it for me, which i run from console!






9 Comments
An alternative is to use “cake bake project” to bake your project, as it will automatically generate a random salt value.
You could get the same effect writing less code and using less memory (your doing 2 randoms per loop).
function generate($length = 10) { $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'; $return = ''; if ($length > 0) { $totalChars = strlen($characters) - 1; for ($i = 0; $i <= $length; ++$i) { $return .= $characters[rand(0, $totalChars)]; } } return $return; }@ Mile Johnson’s code
The code has an off by one bug. A length 10 code will return 11. The for loop should be:
“for ($i = 0; $i < $length; ++$i) {" // change <= to simply <
@Miles hey thanks alot. I might post up your code as a replacement. I did the rand simply because i was too lazy to write the upper cased letters.
Updated the post with a merge of @Miles’s code and a rethink on my part.
Also if anyone knows a good wordpress code formatting plugin please let me know! I am really hating my one at the moment!
Re: A wordpress code formatting plugin
WP-Syntax
Google Syntax Highlighter
Both are good.
Google Syntax Highlighter has more functionality.
There is a tool for linux called pwgen. Just run pwgen -c -n -s 128 from your console and you’ll get some 128 characters long secure strings.
Very nice code buddy. Thanks for it. I have just one short question. How long can the SALT value be? Is there any min/max length? This “$length = 10″ will generate 10 characters long SALT value, right?
Thanks in advance.
A salt string is simply a string of characters that are used to distort a hash or other type of encryption. For example a 32 character salt would result in:
Assume a user’s (encrypted) secret key is stolen and he is known to use one of 200,000 English words as his password. The system uses a 32-bit salt. The salted key is now the original password appended to this random 32-bit salt. Because of this salt, the attacker’s pre-calculated hashes are of no value. He must calculate the hash of each word with each of 232 (4,294,967,296) possible salts appended until a match is found. The total number of possible inputs can be obtained by multiplying the number of words in the dictionary with the number of possible salts:
To complete a brute-force attack, the attacker must now compute about 800 trillion hashes, instead of only 200,000. Even though the password itself is known to be simple, the secret salt makes breaking the password increasingly difficult.
//Wikipedia
Generally i use 40 chars for my salt