Last active
February 20, 2016 22:02
-
-
Save lkorth/3800810 to your computer and use it in GitHub Desktop.
PHP function for generating cryptography secure strings of a given length
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 | |
/** | |
* PHP function for generating cryptography secure strings of a given length | |
* | |
* @param $length the length in bytes for the generated key | |
* @return the cryptography secure string of {$length} length | |
*/ | |
function generate_key($length) { | |
$strong = FALSE; | |
while($strong != TRUE){ | |
$key = base64_encode(openssl_random_pseudo_bytes($length, $strong)); | |
} | |
return substr($key, 0, $length); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment