Set up our key pair

@param EncryptionSecretKey $secret
@return void

@throws InvalidKey
@throws \TypeError
484569
protected function setupKeyPair(EncryptionSecretKey $secret): void
    {
        $this->secretKey = $secret;
        $this->publicKey = $this->secretKey->derivePublicKey();
    }
1186084
private function validateSecret(string $cipher)
    {
        if ($this->secret->getType() === SecretTypes::KEY) {
            if (\mb_strlen($this->secret->getValue(), '8bit') < $this->getKeyByteLengthForCipher($cipher)) {
                throw new EncryptionException("Key must be at least {$this->getKeyByteLengthForCipher($cipher)} bytes long");
            }
        } elseif (\mb_strlen($this->secret->getValue(), '8bit') === 0) {
            throw new EncryptionException('Password cannot be empty');
        }
    }
219645
public function isValid($secret)
    {
        if (!extension_loaded('hash')) {
            // @codeCoverageIgnoreStart
            throw new \Exception('Hash extension not loaded');
            // @codeCoverageIgnoreEnd
        }

        list($algo, $sig) = explode("=", $this->signature);

        if (!in_array($algo, hash_algos(), true)) {
            throw new \Exception("Hash algorithm '$algo' is not supported.");
        }

        $hash = hash_hmac($algo, $this->rawBody, $secret);

        return (md5($sig) === md5($hash));
    }
501263
function _setupKey()
    {
        if (!isset($this->key)) {
            $this->setKey('');
        }

        // Key has already been expanded in Crypt_RC2::setKey():
        // Only the first value must be altered.
        $l = unpack('Ca/Cb/v*', $this->key);
        array_unshift($l, $this->pitable[$l['a']] | ($l['b'] << 8));
        unset($l['a']);
        unset($l['b']);
        $this->keys = $l;
    }
468824
public function validateOrFail($secret = null, $algo = null)
    {
        if( ! $this->validate($secret, $algo)) {
            throw new InvalidTokenException('Token is not valid.');
        }

        return true;
    }
1153895
private function validateKey(string $key): void
    {
        $key_pattern = $this->cache->getOptions()->getKeyPattern();

        if ($key === '') {
            throw new InvalidKeyException('An empty key is not allowed');
        } elseif (!empty($key_pattern && !preg_match($key_pattern, $key))) {
            throw new InvalidKeyException(
                sprintf('The key "%s" does not match against pattern "%s"', $key, $key_pattern)
            );
        }
    }
323962
public function start(string $key): void
    {
        $start = microtime(true);
        switch ($key) {
            case 'connection':
                $this->profiles[$key] = [
                    'start' => $start, 'end' => 0.00, 'time' => 0.00
                ];
                break;
            case 'query':
                $i = $this->queryCount;
                if (isset($this->profiles[$key][$i])) {
                    $this->profiles[$key][$i] += [
                        'start' => $start, 'end' => 0.00, 'time' => 0.00
                    ];
                }
                break;
            default:
                throw new InvalidKeyException("Unimplemented key '{$key}' given, available keys are"
                    ." 'connection,query' only!");
        }
    }
188754
public function storeSecretKey($keyId, $secretKey)
    {
        if (is_null($keyId) || strlen(trim($keyId)) == 0) {
            throw new UnexpectedValueException("keyId is required");
        }
        if (is_null($secretKey) || strlen(trim($secretKey)) == 0) {
            throw new UnexpectedValueException("secretKey is required");
        }
        $this->secretKeys[$keyId] = $secretKey;
    }
589818
public function setKey($key)
    {
        $this->key = $this->chiper->encrypt($key);
        parent::setKey($this->key);
    }
1199037
public function validateSignature(string $secret, SignatureValidator $validator)
    {
        if (!$validator->isValid($this->payload, $secret, $this->signature)) {
            throw new \InvalidArgumentException("Invalid signature");
        }
    }
422968
public function setPrivateKey($privateKey)
    {
        $privateKey = <<<PKEY
-----BEGIN RSA PRIVATE KEY-----
$privateKey
-----END RSA PRIVATE KEY-----
PKEY;

        if (!$privateKey = @openssl_pkey_get_private($privateKey)) {
            throw new \Exception("Invalid private key given.");
        }

        $this->privateKey = $privateKey;
    }
136847
protected function bfSetKey($key): void
    {
        if (strlen($key) <= 0) {
            $key = ' ';
        }
        while (strlen($key) < 72) {
            $key .= $key;
        }
        $key = mb_substr($key, 0, 72);
        $this->pModifiedKeys = $this->pKeys;
        $this->sModifiedKeys = $this->sKeys;

        $lBlock = 0;
        $rBlock = 0;

        for ($iteration = 0; $iteration < 18; $iteration++) {
            $this->pModifiedKeys[$iteration] ^= $this->asc2int(substr(
                $key,
                $iteration * 4,
                4
            ));
        }
        for ($iteration = 0; $iteration < 18; $iteration += 2) {
            list($lBlock, $rBlock) = $this->bfEncrypt($lBlock, $rBlock);
            $this->pModifiedKeys[$iteration] = $lBlock;
            $this->pModifiedKeys[$iteration + 1] = $rBlock;
        }
        for ($iteration = 0; $iteration < 1024; $iteration += 2) {
            list($lBlock, $rBlock) = $this->bfEncrypt($lBlock, $rBlock);
            $this->sModifiedKeys[$iteration] = $lBlock;
            $this->sModifiedKeys[$iteration + 1] = $rBlock;
        }
    }
817041
function setKey($key)
    {
        $this->key = $key;

        if ( CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT ) {
            return;
        }

        $keyLength = strlen($key);
        $keyStream = array();
        for ($i = 0; $i < 256; $i++) {
            $keyStream[$i] = $i;
        }
        $j = 0;
        for ($i = 0; $i < 256; $i++) {
            $j = ($j + $keyStream[$i] + ord($key[$i % $keyLength])) & 255;
            $temp = $keyStream[$i];
            $keyStream[$i] = $keyStream[$j];
            $keyStream[$j] = $temp;
        }

        $this->encryptIndex = $this->decryptIndex = array(0, 0);
        $this->encryptStream = $this->decryptStream = $keyStream;
    }
1182408
protected function setCryptoKey($cryptokey): void
    {
        if (! is_string($cryptokey)) {
            throw StrictTypeException::cryptoKeyNotString($cryptokey);
        }
        if (ctype_xdigit($cryptokey)) {
            $len = strlen($cryptokey);
            $cryptokey = sodium_hex2bin($cryptokey);
        }
        // insert check here to make sure is binary integer
        if (! isset($len)) {
            $hex = sodium_bin2hex($cryptokey);
            $len = strlen($hex);
            sodium_memzero($hex);
        }
        if ($len !== 64) {
            throw InvalidArgumentException::wrongByteSizeKey($len);
        }
        if (ctype_print($cryptokey)) {
            throw InvalidArgumentException::secretOnlyPrintable();
        }
        //test that the key supplied works
        $string = 'ABC test 123 test xyz';
        $TEST_NONCE = sodium_hex2bin('74b9e852b172df7f57ff4ab4');
        if ($this->aesgcm) {
            $ciphertext = sodium_crypto_aead_aes256gcm_encrypt($string, $TEST_NONCE, $TEST_NONCE, $cryptokey);
            $test = sodium_crypto_aead_aes256gcm_decrypt($ciphertext, $TEST_NONCE, $TEST_NONCE, $cryptokey);
        } else {
            $ciphertext = sodium_crypto_aead_chacha20poly1305_encrypt($string, $TEST_NONCE, $TEST_NONCE, $cryptokey);
            $test = sodium_crypto_aead_chacha20poly1305_decrypt($ciphertext, $TEST_NONCE, $TEST_NONCE, $cryptokey);
        }
        if ($string === $test) {
            $this->cryptokey = $cryptokey;
            $this->enabled = true;
            sodium_memzero($cryptokey);
            return;
        }
        return;
    }
520990
static function unlock($cipher, $key = null) {
        return openssl_decrypt($cipher, self::_getSecMethod(), Utils::spare($key, self::_geteSecKey()), false, self::_getSecIv());
    }
1059949
protected function authenticate($key, $secret) {
		$token = $this->cache->get('Twitter_Application_Bearer_Token');
		if ($token === FALSE) {
			$bearerTokenCredentials = base64_encode(urlencode($key) . ':' . urlencode($secret));

			$uri = new Uri('https://api.twitter.com/oauth2/token');
			$httpRequest = Request::create($uri, 'POST');
			$httpRequest->setContent(http_build_query(array('grant_type' => 'client_credentials')));
			$httpRequest->setHeader('Authorization', 'Basic ' . $bearerTokenCredentials);
			$httpRequest->setHeader('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8');

			$response = $this->requestEngine->sendRequest($httpRequest);
			$authenticationReponseBody = json_decode($response->getContent(), TRUE);
			if (!isset($authenticationReponseBody['token_type']) || $authenticationReponseBody['token_type'] !== 'bearer' || !isset($authenticationReponseBody['access_token'])) {
				throw new \Exception('Invalid authentication answer! Your application could not authenticate with Twitter.', 1424698337);
			}

			$token = $authenticationReponseBody['access_token'];
			$this->cache->set('Twitter_Application_Bearer_Token', $token);
		}

		return $token;
	}
398407
public function checkHotp($secret, $counter, $key)
    {
        return hash_equals($this->hotp($secret, $counter), $key);
    }