Key pair generation.

@param array Key/User data (user, email, password, size)

@return mixed Key (enigma_key) object or enigma_error
337209
public function gen_key($data)
    {
        try {
            $debug  = $this->rc->config->get('enigma_debug');
            $keygen = new Crypt_GPG_KeyGenerator(array(
                    'homedir' => $this->homedir,
                    // 'binary'  => '/usr/bin/gpg2',
                    'debug'   => $debug ? array($this, 'debug') : false,
            ));

            $key = $keygen
                ->setExpirationDate(0)
                ->setPassphrase($data['password'])
                ->generateKey($data['user'], $data['email']);

            return $this->parse_key($key);
        }
        catch (Exception $e) {
            return $this->get_error_from_exception($e);
        }
    }
133394
public static function encrypt($data, $key) {

        static :: check();
        
        $iv = static :: random_bytes(16);
        
        //Encryption
        $ciphertext = openssl_encrypt($data, self :: METHOD, mb_substr($key, 0, 32, self :: ENCODING), OPENSSL_RAW_DATA, $iv);

        //Authentication
        $hmac = hash_hmac(self :: ALGORITHM, $iv . $ciphertext, mb_substr($key, 32, null, self :: ENCODING), true);

        return $hmac . $iv . $ciphertext;
    }
252952
protected function generateCacheKey()
    {
        $name = $this->connection->getName();

        $data = $name.$this->toSql().serialize($this->getBindings());

        return hash(static::HASH_ALGO, $data);
    }
1298325
public function GenerateKeypair()
    {
        $point = $this->GenerateNewPoint();

        $point['Rx_hex']        = $this->stripHexPrefix($point['Rx_hex']);
        $point['Ry_hex']        = $this->stripHexPrefix($point['Ry_hex']);
        $point['random_number'] = $this->stripHexPrefix($point['random_number']);

        $comp_prefix = ($this->Modulo($point['R']['y'], '2') == '1') ? '03' : '02';

        $this->keyInfo = array(
                               'private_key_hex'       => $this->encodeHex($point['random_number']),
                               'private_key_dec'       => $point['random_number'],
                               'public_key'            => '04' . $point['Rx_hex'] . $point['Ry_hex'],
                               'public_key_compressed' => $comp_prefix . $point['Rx_hex'],
                               'public_key_x'          => $point['Rx_hex'],
                               'public_key_y'          => $point['Ry_hex'],
                              );

        return $this->keyInfo;
    }
307147
private function decryptPrivateKey(PKIPrivateKey $pkiKey)
    {
        // Get the symmetric key used to encrypt the private key of the user
        $encryptedKey = base64_decode($pkiKey->getKey());

        $privateMasterKey = $this->getPrivateMasterKey();
        $symmetricKey = $this->cryptographyProvider->decryptWithPrivateKey($encryptedKey, $privateMasterKey);

        if ($symmetricKey) {
            // Decrypt the private key of the user
            $iv = $pkiKey->getIv();
            $keyData = new KeyData($symmetricKey, $iv);

            $encryptedUserPrivateKey = $pkiKey->getPrivateKey();
            $userPrivateKey = $this->cryptographyProvider->decrypt($encryptedUserPrivateKey, $keyData);

            return $userPrivateKey;
        }
        else {
            throw new EncryptionException('Could not retrieve the encryption keys of the user');
        }
    }
219103
private function _generateSignatureKey()
    {
        $date_h = hash_hmac('sha256', $this->_date, 'AWS4' . $this->_aws_secret, true);
        $region_h = hash_hmac('sha256', $this->_region, $date_h, true);
        $service_h = hash_hmac('sha256', self::SERVICE, $region_h, true);
        $signing_h = hash_hmac('sha256', 'aws4_request', $service_h, true);

        return $signing_h;
    }
1199367
private function lookupKey($key)
    {
        foreach ($this->pairs as $pair) {
            if ($this->keysAreEqual($pair->key, $key)) {
                return $pair;
            }
        }
    }
892256
public SecretKey generateSecretKey() throws NoSuchAlgorithmException {
        final KeyGenerator keyGen = KeyGenerator.getInstance("AES");
        final SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        keyGen.init(256, random);
        return this.secretKey = keyGen.generateKey();
    }
383927
public static function keyCreate(){
		return base64_encode(mcrypt_create_iv(
			 mcrypt_get_key_size(self::$crypt_cipher,self::$crypt_mode)
			,self::$crypt_rand
		));
	}
1276132
public function getKey()
    {
        if (!isset($this->key)) {
            $this->key = md5(microtime().rand());
        }
        return $this->key;
    }
1157831
public static function generateKey($length = Constants::GENERICHASH_KEYBYTES)
    {
        # Test the length for validity.
        Helpers::rangeCheck($length, Constants::GENERICHASH_KEYBYTES_MAX, Constants::GENERICHASH_KEYBYTES_MIN, 'Hash', 'generateKey');

        # Return the hash to the client.
        return self::hash(Entropy::bytes($length), '', $length);
    }
682405
private function generatePKIKeys()
    {
        // OPENSSL config
        $config = array(
            'digest_alg' => $this->settings['private_key']['digest_method'],
            'private_key_bits' => $this->settings['private_key']['bits'],
            'private_key_type' => $this->settings['private_key']['type'],
        );

        $privateKey = null;
        $resource = openssl_pkey_new($config);

        openssl_pkey_export($resource, $privateKey);

        if(!$privateKey) {
            throw new EncryptionException('Private key could not be generated');
        }

        $publicKeyDetails = openssl_pkey_get_details($resource);
        $publicKey = $publicKeyDetails['key'];

        return array($publicKey, $privateKey);
    }
1211281
public KeyPair generateKeyPair() {
        KeyPair keyPair = null;
        try {
            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM);
            keyPairGenerator.initialize(KEYLENGTH);
            keyPair = keyPairGenerator.generateKeyPair();
        } catch (NoSuchAlgorithmException e) {
            LOG.error("Failed to create publi/private key pair", e);
        }
        
        return keyPair;
    }
1182140
public function key()
    {
        if (is_null($this->ext)) {
            $this->ext = $this->generateExt();
        }
        return $this->ext;
    }
1324049
protected function doEncrypt($data, $key)
    {
        $encrypted = '';

        // Split up the input into a character array and get the number of characters.
        $chars = preg_split('//', $data, -1, PREG_SPLIT_NO_EMPTY);

        // Make sure private key is as long as chars length
        $key = str_repeat($key, (int) ceil(count($chars) / strlen($key)));

        // Get the XOR values between the ASCII values of the input and key characters for all input offsets.
        foreach ($chars as $i => $char) {
            $encrypted .= $this->intToHex(ord($key[$i]) ^ ord($char));
        }

        return $encrypted;
    }
95606
function _setupKey()
    {
        $key = $this->key;
        $keyLength = strlen($key);
        $keyStream = range(0, 255);
        $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->stream = array();
        $this->stream[self::DECRYPT] = $this->stream[self::ENCRYPT] = array(
            0, // index $i
            0, // index $j
            $keyStream
        );
    }
788052
private function generatePrivateECKey(): JWK
    {
        return JWKFactory::createFromKeyFile($this->privateKeyPath, $this->privateKeySecret, [
            'kid' => $this->keyId,
            'alg' => 'ES512',
            'use' => 'sig'
        ]);
    }