Exemplo n.º 1
0
    /**
     * Generate keys
     *
     * @param  array $options
     * @return ArrayObject
     * @throws Rsa\Exception\RuntimeException
     */
    public function generateKeys(array $options = null)
    {
        $config = array(
            'private_key_bits' => self::DEFAULT_KEY_SIZE,
            'private_key_type' => OPENSSL_KEYTYPE_RSA
        );

        if (isset($options['pass_phrase'])) {
            $passPhrase = $options['pass_phrase'];
        } else {
            $passPhrase = $this->options->getPassPhrase();
        }

        if (isset($options['private_key_bits'])) {
            $config['private_key_bits'] = $options['private_key_bits'];
        }

        // generate
        $privateKey = null;
        $publicKey  = null;
        $resource   = openssl_pkey_new($config);
        $result     = openssl_pkey_export($resource, $private, $passPhrase);
        if (false === $result) {
            throw new Exception\RuntimeException(
                'Can not export key; openssl ' . openssl_error_string()
            );
        }

        $privateKey = new Rsa\PrivateKey($private, $passPhrase);
        $details    = openssl_pkey_get_details($resource);
        $publicKey  = new Rsa\PublicKey($details['key']);

        return new ArrayObject(array(
            'privateKey' => $privateKey,
            'publicKey' => $publicKey
        ), ArrayObject::ARRAY_AS_PROPS);
    }