Advertisement
cafreak

PHP RDG

Feb 11th, 2015
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.08 KB | None | 0 0
  1. /**
  2.      *   A method to generate randomized data for example salting or session keys
  3.      *  @param integer $length DESCRIPTION: used to set a length that key should have
  4.      *  @param boolean $base64encode OPTIONAL DESCRIPTION: if true it will use base64_encode() on the return data
  5.      *  @param boolean $isSalt OPTIONAL DESCRIPTION: if true it will base64_encode() on the return data and will truncate the size to 22
  6.      *  @return string $value DESCRIPTION: returns the randomized data
  7.      */
  8.    
  9.     function generator($length, $base64encode = false, $isSalt = false){
  10.        
  11.         if(!is_integer($length)){
  12.             trigger_error("Length variable must be an integer; ".gettype($length)." given", E_USER_ERROR);
  13.         }
  14.        
  15.         if(!is_bool($base64encode)){
  16.             trigger_error("base64encode variable must be a boolean; ".gettype($base64encode)." given", E_USER_ERROR);
  17.         }
  18.        
  19.         if(!is_bool($isSalt)){
  20.             trigger_error("isSalt variable must be a boolean; ".gettype($base64encode)." given", E_USER_ERROR);
  21.         }
  22.        
  23.         if($isSalt){
  24.             $base64encode = true;
  25.         }
  26.        
  27.         if(function_exists('mcrypt_create_iv')){
  28.             $value = @mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
  29.             if($value == null || $value == false || $value == ""){
  30.                 $value = @mcrypt_create_iv($length, MCRYPT_DEV_RANDOM);
  31.                 if($value == null || $value == false || $value == ""){
  32.                     trigger_error("MCRYPT_DEV_RANDOM and/or MCRYPT_DEV_URANDOM are/is not available", E_USER_ERROR);
  33.                 }
  34.             }
  35.             //return $value;
  36.         }else if(function_exists('openssl_random_pseudo_bytes')){
  37.             $value = openssl_random_pseudo_bytes($length);
  38.             if($value == null || $value == false || $value == ""){
  39.                 trigger_error("openssl_random_pseudo_bytes is not available", E_USER_ERROR);
  40.             }
  41.  
  42.         }else{
  43.             trigger_error("mcrypt_create_iv and openssl_random_pseudo_bytes are not available", E_USER_ERROR);
  44.         }
  45.        
  46.         if($base64encode){
  47.             $value = rtrim(base64_encode($value), "=");
  48.         }
  49.        
  50.         if($isSalt){
  51.             $value = substr($value, 0, 22);
  52.             if(strlen($value) <> 22){
  53.                 trigger_error("The salt length must be 22", E_USER_ERROR);;
  54.             }
  55.         }
  56.        
  57.         return $value."<hr />".strlen($value);
  58.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement