Created
November 16, 2010 07:09
-
-
Save keeguon/701545 to your computer and use it in GitHub Desktop.
Generate somewhat strong unique identifiers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Generate somewhat strong unique identifiers | |
* Example usage: | |
* - create non standard mysql ids | |
* - create app key/secret for OAuth | |
* - ... | |
* | |
* @return string containing the unique identifier | |
*/ | |
function generateUid() { | |
$fp = fopen('/dev/urandom','rb'); | |
$entropy = fread($fp, 32); | |
fclose($fp); | |
// in case /dev/urandom is reusing entropy from its pool, let's add a bit more entropy | |
$entropy .= uniqid(mt_rand(), true); | |
$hash = sha1($entropy); | |
// if you're obsessed w/ security use the following instead | |
// $hash = hash('sha256', $entropy) | |
return $hash; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment