mirror of
				https://github.com/pierre42100/ComunicAPI
				synced 2025-10-31 02:04:53 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			25 lines
		
	
	
		
			645 B
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			25 lines
		
	
	
		
			645 B
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| /**
 | |
|  * Strings methods
 | |
|  *
 | |
|  * @author Pierre HUBERT
 | |
|  */
 | |
| 
 | |
| /**
 | |
|  * Generate a random string, using a cryptographically secure 
 | |
|  * pseudorandom number generator (random_int)
 | |
|  * 
 | |
|  * @param int $length      How many characters do we want?
 | |
|  * @param string $keyspace A string of all possible characters
 | |
|  *                         to select from
 | |
|  * @return string
 | |
|  */
 | |
| function random_str($length, $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
 | |
| {
 | |
|     $str = '';
 | |
|     $max = mb_strlen($keyspace, '8bit') - 1;
 | |
|     for ($i = 0; $i < $length; ++$i) {
 | |
|         $str .= $keyspace[random_int(0, $max)];
 | |
|     }
 | |
|     return $str;
 | |
| } | 
