Skip to content

Instantly share code, notes, and snippets.

@lkorth
Last active February 20, 2016 22:02
Show Gist options
  • Save lkorth/3800810 to your computer and use it in GitHub Desktop.
Save lkorth/3800810 to your computer and use it in GitHub Desktop.
PHP function for generating cryptography secure strings of a given length
<?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