Last active
April 26, 2017 09:29
-
-
Save dsturm/6eae39851bde9eb4923ed5d9a6d9f5e5 to your computer and use it in GitHub Desktop.
Create Auth Tokens with PHP
This file contains hidden or 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
function create_auth_token($length = 64) | |
{ | |
if (!$length || intval($length) <= 8) { | |
$length = 64; | |
} | |
$bytes = null; | |
if (function_exists('random_bytes')) { | |
$bytes = random_bytes($length); | |
} elseif (function_exists('mcrypt_create_iv')) { | |
$bytes = mcrypt_create_iv($length, MCRYPT_DEV_URANDOM); | |
} elseif (function_exists('openssl_random_pseudo_bytes')) { | |
$bytes = openssl_random_pseudo_bytes($length); | |
} | |
return $bytes ? bin2hex($bytes) : false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment