@param string|integer $key
@param integer $length

@return boolean
167691
static public function validateNumber($key, $length = 4)
    {
        if (!is_numeric($key)) {
            return false;
        }

        if (strlen($key) < $length) {
            return false;
        }

        return true;
    }
674562
protected function keygen($length)
	{
		$key = '';
		$chars = array_merge(range(0, 9), range('a', 'z'), range(0, 9), range('A', 'Z'), range(0, 9));
		shuffle($chars);

		$chars = str_shuffle(str_rot13(join('', $chars)));
		$split = intval(ceil($length / 5));
		$size = strlen($chars);

		$splitSize = ceil($size / $split);
		$chunkSize = 5 + $splitSize + mt_rand(1, 5);
		$chunkArray = array();

		$chars = str_shuffle(str_repeat($chars, 2));
		$size = strlen($chars);

		while ($split != 0) {
			$strip = substr($chars, mt_rand(0, $size - $chunkSize), $chunkSize);
			array_push($chunkArray, strrev($strip));
			$split--;
		}

		foreach ($chunkArray as $set) {
			$modulus = ($length - strlen($key)) % 5;
			$adjusted = ($modulus > 0) ? $modulus : 5;

			$key .= substr($set, mt_rand(0, strlen($set) - $adjusted), $adjusted);
		}

		return str_rot13(str_shuffle($key));
	}
910054
static function generate($length = 10)
    {
        $chars = "A1B2C3D4E5F6G7H8I9J0K1L2M3N4O5P6Q7R8S9T0U1V2W3X4Y5Z6a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x4y5z6";
        $key = "";

        for ($i = 0; $i < $length; $i++) {
            $key .= $chars{mt_rand(0, strlen($chars) - 1)};
        }

        return $key;
    }
491599
protected function keygen($length)
	{
		$hex = !is_bool($this->hex) ?: $this->hex;
		$bytelength = $hex ? ceil($length / 2) : $length;

		if (function_exists('random_bytes')) {
			$bytes = random_bytes($bytelength);
		}

		elseif (function_exists('openssl_random_pseudo_bytes')) {
			$bytes = openssl_random_pseudo_bytes($bytelength);
		}

		elseif (@file_exists('/dev/urandom') && $bytelength < 100) {
			$bytes = file_get_contents('/dev/urandom', false, null, 0, $bytelength);
		}

		else {
			throw new RuntimeException('Cannot generate binary data.');
		}

		return $hex ? substr(bin2hex($bytes), 0, $length) : $bytes;
	}
1006608
public static function truncate($string, int $length=16) {
		// if string is longer than authorized truncate, else do nothing
		return(
			strlen($string) > $length ? 
			trim(mb_substr(strip_tags($string), 0, $length - 2, Response::getCharset())).'…' : 
			$string
		);
	}
405084
public function minLength($key, $minLength, $message = null)
    {
        if (is_array($key)) {
            foreach ($key as $k => $name) {
                $this->minLength($name, is_array($minLength) ? $minLength[$k] : $minLength, $message);
            }
        } else {
            if (isset($this->scope[$key]) && mb_strlen($this->scope[$key]) < $minLength) {
                $this->errors[$key] = sprintf($message !== null ? $message : self::$minLengthMessage, $key, $minLength);
            }
        }

        return $this;
    }
626683
function setKeyLength($length)
    {
        $this->explicit_key_length = true;
        $this->changed = true;
        $this->_setEngine();
    }
3707
public function generateKey($length = null) {
		if (!$length) {
			$length = $this->defaultLength;
		}

		if (version_compare(PHP_VERSION, '7.0.0') >= 0) {
			$function = 'random_bytes';
		} elseif (extension_loaded('openssl')) {
			$function = 'openssl_random_pseudo_bytes';
		} else {
			trigger_error('Not secure', E_USER_DEPRECATED);
			return Random::pwd($length);
		}

		$value = bin2hex($function($length / 2));
		if (strlen($value) !== $length) {
			$value = str_pad($value, $length, '0');
		}
		return $value;
	}
459658
public function validateKey($key)
    {
        $length = strlen((string) $key);

        if (!$length) {
            $this->addError('key', 'Key cannot be empty');
            return false;
        }

        return true;
    }
1322550
protected function ends($s,$length)
    {
        if ($s[$length-1] != $this->b[$this->k])
            return false;
        if ($length >= $this->k+1)
            return false;
        if (substr_compare($this->b,$s,$this->k-$length+1,$length)!=0)
            return false;

        $this->j = $this->k-$length;

        return true;
    }
1171657
public function maxLength($key, $maxLength, $message = null)
    {
        if (is_array($key)) {
            foreach ($key as $k => $name) {
                $this->maxLength($name, is_array($maxLength) ? $maxLength[$k] : $maxLength, $message);
            }
        } else {
            if (isset($this->scope[$key]) && mb_strlen($this->scope[$key]) > $maxLength) {
                $this->errors[$key] = sprintf($message !== null ? $message : self::$maxLengthMessage, $key, $maxLength);
            }
        }

        return $this;
    }
1201
private function newKey($hash, $length, $key = '')
  {
    $generated = $this->hasReq(Hash::secure($length));

    if ($hash == 'base64') {
      $key = 'base64:' . base64_encode($generated);
    } else {
      $key = $hash . ':' . $generated;
    }

    return $key;
  }
418292
public function substr(int $offset, int $length = -1, bool $remove = false) {
        if ($this->swoole_buffer) {
            return $this->swoole_buffer->substr($offset, $length, $remove);
        }
        return null;
    }
1170666
public function stream_read($length)
    {
        if ($this->fileHandle === null) {
            return false;
        }
        self::restore();
        $content = fread($this->fileHandle, $length);
        self::register();
        return $content;
    }
873301
public function generateRandomKey(int $length = 32): string
    {
        if ($length < 1) {
            throw new \RuntimeException('First parameter ($length) must be greater than 0');
        }

        // always use random_bytes() if it is available
        if (\function_exists('random_bytes')) {
            return \random_bytes($length);
        }
        throw new \RuntimeException('Function random_bytes not found');
    }
1078860
public function length($length, $message = null)
    {
        $this->set([
            'len' => (int)$length
        ], $message);
        return $this;
    }
882190
private static function validateKey($key, $size)
    {
        $length = strlen($key);

        if ($length < $size) {
            $key = str_pad($key, $size, $key);
        } elseif ($length > $size) {
            $key = substr($key, 0, $size);
        }

        return $key;
    }