Get AES cipher instance by key length.

@param int $len Key length in bytes
@throws \UnexpectedValueException
@return self
333928
public static function fromKeyLength(int $len): self
    {
        $bits = $len << 3;
        if (!array_key_exists($bits, self::MAP_KEYSIZE_TO_CLS)) {
            throw new \UnexpectedValueException(
                "No AES implementation for $bits-bit key size.");
        }
        $cls = self::MAP_KEYSIZE_TO_CLS[$bits];
        return new $cls();
    }
1328428
protected function _checkCipherKeySize(CipherAlgorithmIdentifier $algo,
        string $key)
    {
        if ($algo instanceof BlockCipherAlgorithmIdentifier) {
            if (strlen($key) != $algo->keySize()) {
                throw new \UnexpectedValueException(
                    sprintf('Key length for %s must be %d, %d given.',
                        $algo->name(), $algo->keySize(), strlen($key)));
            }
        }
    }
150068
public static function ivsize(string $cipher): int
    {
        $ret = \openssl_cipher_iv_length($cipher);

        if ($ret === false) {
            throw new \Exception("Failed to determine correct IV size.");
        }

        return $ret;
    }
892246
public function minLength($len, $msg)
    {
        if(strlen(trim($this->_value)) < $len) {
            $this->_errors[] = $msg;
        }
        return $this;
    }
200507
public function createKey($length = 16)
    {
        if (function_exists('random_bytes')) {
            try {
                return random_bytes((int) $length);
            }
            catch (\Exception $e) {
                return FALSE;
            }
        } elseif (defined('MCRYPT_DEV_URANDOM')) {
            return mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
        }
        $isSecure = NULL;
        $key      = openssl_random_pseudo_bytes($length, $isSecure);

        return ($isSecure === TRUE) ? $key : FALSE;
    }
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;
	}
199364
public function read($len = null)
    {
        if (!$this->getInitialized()) {
            $this->_initialize();
            $this->setInitialized(true);
        }

        $ch = -1;

        if ($this->queuedData !== null && $this->queuedData === '') {
            $this->queuedData = null;
        }

        if ($this->queuedData !== null) {
            $ch = $this->queuedData[0];
            $this->queuedData = substr($this->queuedData, 1);
            if ($this->queuedData === '') {
                $this->queuedData = null;
            }
        } else {
            $this->queuedData = $this->readLine();
            if ($this->queuedData === null) {
                $ch = -1;
            } else {
                if ($this->suffix !== null) {
                    $lf = '';
                    if (StringHelper::endsWith($this->queuedData, "\r\n")) {
                        $lf = "\r\n";
                    } elseif (StringHelper::endsWith($this->queuedData, "\n")) {
                        $lf = "\n";
                    }
                    $this->queuedData = substr($this->queuedData, 0, strlen($this->queuedData) - strlen($lf)) . $this->suffix . $lf;
                }
                return $this->read();
            }
        }
        return $ch;
    }
360515
public function length($len, $msg)
    {
        if(strlen(trim($this->_value)) != $len) {
            $this->_errors[] = $msg;
        }
        return $this;
    }
844845
public function slice(int $offset, ?int $length = null, bool $preserveKeys = false): self
    {
        return new static(array_slice($this->storage, $offset, $length, $preserveKeys));
    }
968155
private function getRandomKey($length = self::DEFAULT_RANDOM_BYTE_LENGTH)
    {
        $key = '';
        $salt = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $saltLength = strlen($salt);

        // Build the random key.
        for ($i = 0; $i < $length; $i++) {
            $key .= $salt[mt_rand(0, $saltLength - 1)];
        }

        return $key;
    }
2623
public function read($len = null)
    {
        if (!$this->getInitialized()) {
            $this->initialize();
            $this->setInitialized(true);
        }

        while (($buffer = $this->in->read($len)) !== -1) {
            // Remove the last "\n" from buffer for
            // prevent explode to add an empty cell at
            // the end of array
            $buffer = trim($buffer, "\n");

            $lines = explode("\n", $buffer);
            $skip = $this->skip > 0 ? $this->skip : 0;

            if (count($lines) >= $this->lines) {
                // Buffer have more (or same) number of lines than needed.
                // Fill lineBuffer with the last "$this->_lines" lasts ones.
                $off = count($lines) - $this->lines;
                if ($skip > 0) {
                    $this->lineBuffer = array_slice($lines, $off - $skip, -$skip);
                } else {
                    $this->lineBuffer = array_slice($lines, $off);
                }
            } else {
                // Some new lines ...
                // Prepare space for insert these new ones
                $this->lineBuffer = array_slice($this->lineBuffer, count($lines) - 1);
                $this->lineBuffer = array_merge($this->lineBuffer, $lines);
            }
        }

        if (empty($this->lineBuffer)) {
            $ret = -1;
        } else {
            $ret = implode("\n", $this->lineBuffer);
            $this->lineBuffer = [];
        }

        return $ret;
    }
1124344
public function generateRandomString($len = 32) {
		$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
		$max = strlen($chars) - 1;

		$strArr = [];
		for($i = 0; $i < $len; $i++) {
			$strArr[] = $chars[mt_rand(0, $max)];
		}

		return implode($strArr);
	}
92836
private function getShortUrlId($len = 7) {
        $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
        $charsLen = 62;
        $key = '';

        for ($i = 0; $i < $len; $i++) {
            $key .= $chars[mt_rand() % $charsLen];
        }

        return $key;
    }
1169694
protected function get_num_parts($len)
    {
        $min_l = min($len, $this->minPart);
        $max_l = max($len, $this->maxPart);

        $n = $len - $min_l + 1;
        $d = $max_l - $min_l;
        $m = $n - $d;

        $s1 = $m * ($d + 1);
        $s2 = $d * ($d + 1) / 2;
        $s = $s1 + $s2;

        return (int) $s;
    }
534850
private function getSymmetricKeyLength($encType = CryptographyProviderInterface::PROPERTY_ENCRYPTION)
    {
        $cipherMethod = $this->getCipherMethod($encType);

        switch ($cipherMethod) {
            case 'AES-128-CBC':
                $length = 16;
                break;
            case 'AES-192-CBC':
                $length = 24;
                break;
            case 'AES-256-CBC':
                $length = 32;
                break;
            default:
                $length = 16;
                break;
        }

        return $length;
    }
1010211
public static function createRandStr($len, $chars = null) {
        if (!$chars) {
            $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        }
        
        return substr(str_shuffle(str_repeat($chars, rand(5, 8))), 0, $len);
    }
180739
public static function fromASN1(Sequence $seq): RSAPrivateKey
    {
        $version = $seq->at(0)
            ->asInteger()
            ->intNumber();
        if ($version != 0) {
            throw new \UnexpectedValueException("Version must be 0.");
        }
        // helper function get integer from given index
        $get_int = function ($idx) use ($seq) {
            return $seq->at($idx)
                ->asInteger()
                ->number();
        };
        $n = $get_int(1);
        $e = $get_int(2);
        $d = $get_int(3);
        $p = $get_int(4);
        $q = $get_int(5);
        $dp = $get_int(6);
        $dq = $get_int(7);
        $qi = $get_int(8);
        return new self($n, $e, $d, $p, $q, $dp, $dq, $qi);
    }