Skip to content

Instantly share code, notes, and snippets.

@keeguon
Created November 16, 2010 07:09
Show Gist options
  • Save keeguon/701545 to your computer and use it in GitHub Desktop.
Save keeguon/701545 to your computer and use it in GitHub Desktop.
Generate somewhat strong unique identifiers
<?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