Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * A method to generate randomized data for example salting or session keys
- * @param integer $length DESCRIPTION: used to set a length that key should have
- * @param boolean $base64encode OPTIONAL DESCRIPTION: if true it will use base64_encode() on the return data
- * @param boolean $isSalt OPTIONAL DESCRIPTION: if true it will base64_encode() on the return data and will truncate the size to 22
- * @return string $value DESCRIPTION: returns the randomized data
- */
- function generator($length, $base64encode = false, $isSalt = false){
- if(!is_integer($length)){
- trigger_error("Length variable must be an integer; ".gettype($length)." given", E_USER_ERROR);
- }
- if(!is_bool($base64encode)){
- trigger_error("base64encode variable must be a boolean; ".gettype($base64encode)." given", E_USER_ERROR);
- }
- if(!is_bool($isSalt)){
- trigger_error("isSalt variable must be a boolean; ".gettype($base64encode)." given", E_USER_ERROR);
- }
- if($isSalt){
- $base64encode = true;
- }
- if(function_exists('mcrypt_create_iv')){
- $value = @mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
- if($value == null || $value == false || $value == ""){
- $value = @mcrypt_create_iv($length, MCRYPT_DEV_RANDOM);
- if($value == null || $value == false || $value == ""){
- trigger_error("MCRYPT_DEV_RANDOM and/or MCRYPT_DEV_URANDOM are/is not available", E_USER_ERROR);
- }
- }
- //return $value;
- }else if(function_exists('openssl_random_pseudo_bytes')){
- $value = openssl_random_pseudo_bytes($length);
- if($value == null || $value == false || $value == ""){
- trigger_error("openssl_random_pseudo_bytes is not available", E_USER_ERROR);
- }
- }else{
- trigger_error("mcrypt_create_iv and openssl_random_pseudo_bytes are not available", E_USER_ERROR);
- }
- if($base64encode){
- $value = rtrim(base64_encode($value), "=");
- }
- if($isSalt){
- $value = substr($value, 0, 22);
- if(strlen($value) <> 22){
- trigger_error("The salt length must be 22", E_USER_ERROR);;
- }
- }
- return $value."<hr />".strlen($value);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement