I am attempting to use the codeigniter security class to generate a unique long code which I can use to update an online db securely:
http://mydomain.com/myproject/index.php/my_controller/index/***3d51693c8c4***..
I noticed that when I create a hash for individual records in a loop using:
$hash= $this->security->get_csrf_hash();
the same hash keeps getting generated. I need a unique hash/security code to be generated for each record in my loop. I don't see anything in http://ellislab.com/codeigniter%20/user-guide/libraries/security.html discussing how to do this.
What is the best way to do this in CI?
If it's a unique code that you want to generate, why not build one yourself with built-in php function?
<?php
for ($i = 0; $i <= 1000; $i++) {
$hash = uniqid(md5(time()), true);
var_dump($hash);
}
?>
Sample output: http://pastebin.com/eSWaLhUt
As you can see, it's unique, regardless of how fast you iterate through the loop.
The uniqid
method will supply a unique security code (be sure to use the more_entropy
argument however this isn't particulary secure so depending on what you're using it for consider using mt_rand
.
I prefer to use the OS to generate unique values. This solution is a *nix only solution, but one of my favs
/**
* A Linux-dependent password generator. This seems to be about 1000x better than
* typical PHP-based password generation, and from what I hear, /dev/urandom should
* be secure enough for temporary passwords.
*
* Lifted from:
* http://stackoverflow.com/questions/6101956/generating-a-random-password-in-php
*/
static public function generatePassword($iLength=0)
{
$rFp = fopen('/dev/urandom', 'r');
if(!$rFp)
throw new RuntimeException("Can't access /dev/urandom to get random data.");
// 1024 bytes should be enough
$sRandom = fread($rFp, 1024);
fclose($rFp);
return substr(trim(base64_encode(md5($sRandom, true)), '='), $iLength * -1);
}