Mistři v programování na #hovnokod

PHP #917

složitěji to snad ani nešlo (bylo nahrazeno 2 řádky)

public static function generateRandomString($length = 8, $hashed = true) {
		$length = (intval($length) > 2) ? $length : 8;
		$string = '';

		// cast ascii tabulky
		$chars = array_flip(range(33, 126));

		// vyjmout znaky
		unset($chars[34]); // "
		unset($chars[60]); // <
		unset($chars[62]); // >
		unset($chars[92]); // /
		unset($chars[96]); // '
		unset($chars[124]); // |
		
		$chars = array_flip($chars);
		
		shuffle($chars);
		
		// vygenerovat
		$gens = array();
		for ($i = 0; $i < $length; $i++) {
			$rand = array_rand($chars);
			$gens[] = chr($chars[$rand]);
		}
		shuffle($gens);
		$string = implode('', $gens);
		
		// hash?
		if ($hashed) {
			$string = mb_substr(hash('sha256', $string), 0, $length);			
		}
		
		return $string;
	}	

Anonymous,