Export the key to a string as PEM.

@param string $password

@return string
490426
public function export(string $password = NULL): string {
        $status = openssl_pkey_export(
            $this->getHandle(),
            $output,
            $password
        );
        if(!$status) {
            throw new RuntimeException(OpenSSL::getLastError());
        }
        return $output;
    }
1010377
public function sign($key, $password = null)
    {
        OpenSslException::reset();

        if (false === extension_loaded('openssl')) {
            // @codeCoverageIgnoreStart
            throw OpenSslException::create(
                'The "openssl" extension is not available.'
            );
            // @codeCoverageIgnoreEnd
        }

        if (false === ($resource = openssl_pkey_get_private($key, $password))) {
            // @codeCoverageIgnoreStart
            throw OpenSslException::lastError();
            // @codeCoverageIgnoreEnd
        }

        if (false === openssl_pkey_export($resource, $private)) {
            // @codeCoverageIgnoreStart
            throw OpenSslException::lastError();
            // @codeCoverageIgnoreEnd
        }

        if (false === ($details = openssl_pkey_get_details($resource))) {
            // @codeCoverageIgnoreStart
            throw OpenSslException::lastError();
            // @codeCoverageIgnoreEnd
        }

        $this->phar->setSignatureAlgorithm(Phar::OPENSSL, $private);

        if (false === @file_put_contents($this->file . '.pubkey', $details['key'])) {
            throw FileException::lastError();
        }
    }
869540
private static function decodePem($pem, array $matches, $password = null)
    {
        Assertion::notNull($password, 'Password required for encrypted keys.');

        $iv = pack('H*', trim($matches[2]));
        $iv_sub = mb_substr($iv, 0, 8, '8bit');
        $symkey = pack('H*', md5($password.$iv_sub));
        $symkey .= pack('H*', md5($symkey.$password.$iv_sub));
        $key = preg_replace('#^(?:Proc-Type|DEK-Info): .*#m', '', $pem);
        $ciphertext = base64_decode(preg_replace('#-.*-|\r|\n#', '', $key));

        $decoded = openssl_decrypt($ciphertext, strtolower($matches[1]), $symkey, true, $iv);

        $number = preg_match_all('#-{5}.*-{5}#', $pem, $result);
        Assertion::eq($number, 2, 'Unable to load the key');

        $pem = $result[0][0].PHP_EOL;
        $pem .= chunk_split(base64_encode($decoded), 64);
        $pem .= $result[0][1].PHP_EOL;

        return $pem;
    }
1261073
private function hashModePbkdf2($password)
    {
        $this->cwsDebug->titleH2('Create password hash using PBKDF2');
        $this->cwsDebug->labelValue('Password', $password);

        $salt = $this->random(self::PBKDF2_RANDOM_BYTES);
        $this->cwsDebug->labelValue('Salt', $salt);

        $algorithm = $this->encode(self::PBKDF2_ALGORITHM);
        $this->cwsDebug->labelValue('Algorithm', self::PBKDF2_ALGORITHM);

        $ite = rand(self::PBKDF2_MIN_ITE, self::PBKDF2_MAX_ITE);
        $this->cwsDebug->labelValue('Iterations', $ite);
        $ite = $this->encode(rand(self::PBKDF2_MIN_ITE, self::PBKDF2_MAX_ITE));

        $params = $algorithm.self::PBKDF2_SEPARATOR;
        $params .= $ite.self::PBKDF2_SEPARATOR;
        $params .= $salt.self::PBKDF2_SEPARATOR;

        $hash = $this->getPbkdf2($algorithm, $password, $salt, $ite, self::PBKDF2_HASH_BYTES, true);
        $this->cwsDebug->labelValue('Hash', $hash);
        $this->cwsDebug->labelValue('Length', strlen($hash));

        $finalHash = $params.base64_encode($hash);
        $this->cwsDebug->dump('Encoded hash (length : '.strlen($finalHash).')', $finalHash);

        if (strlen($finalHash) == self::PBKDF2_LENGTH) {
            return $finalHash;
        }

        $this->error = 'Cannot generate the PBKDF2 password hash...';
        $this->cwsDebug->error($this->error);
    }
400362
public function encodePEM($keypair)
    {
        $this->pemInitialDataCheck($keypair);

        $ecpemstruct = array(
                             'sequence_beg' => '30',
                             'total_len'    => '74',
                             'int_sec_beg'  => '02',
                             'int_sec_len'  => '01',
                             'int_sec_val'  => '01',
                             'oct_sec_beg'  => '04',
                             'oct_sec_len'  => '20',
                             'oct_sec_val'  => $keypair[0],
                             'a0_ele_beg'   => 'a0',
                             'a0_ele_len'   => '07',
                             'obj_id_beg'   => '06',
                             'obj_id_len'   => '05',
                             'obj_id_val'   => '2b8104000a',
                             'a1_ele_beg'   => 'a1',
                             'a1_ele_len'   => '44',
                             'bit_str_beg'  => '03',
                             'bit_str_len'  => '42',
                             'bit_str_val'  => '00' . $keypair[1],
                             );

        $dec = trim(implode($ecpemstruct));

        $this->pemDataLenCheck($dec);

        return '-----BEGIN EC PRIVATE KEY-----' . "\r\n" . chunk_split(base64_encode($this->binConv($dec)), 64) . '-----END EC PRIVATE KEY-----';
    }
46170
private static function passgen ($password,	$hash = 'SHA')
    {
        switch ($hash) {
			case 'SSHA':
				$salt = pack("CCCCCCCC", mt_rand(), mt_rand(), mt_rand(), mt_rand(), mt_rand(), mt_rand(), mt_rand(), mt_rand());
				return "{SSHA}" . base64_encode(pack("H*", sha1($password . $salt)) . $salt);

			case 'SHA':
			default:
				return '{SHA}' . base64_encode(pack('H*', sha1($pass)));
        }
    }
914976
private function getKey(array $config, $password)
    {
        $key = rtrim(file_get_contents($config['rsa_key']));
        $rsa = new \Crypt_RSA();
        if (null !== $password) {
            $rsa->setPassword($password);
        }
        $rsa->loadKey($key);

        return $rsa;
    }
630633
public function exportToFile(string $filename, string $password = NULL) {
        $status = openssl_pkey_export_to_file(
            $this->getHandle(),
            $filename,
            $password
        );
        if(!$status) {
            throw new RuntimeException(OpenSSL::getLastError());
        }
    }
229178
def export_private_key(self, password=None):
		

		if self.__private_key is None:
			raise ValueError('Unable to call this method. Private key must be set')

		if password is not None:
			if isinstance(password, str) is True:
				password = password.encode()
			return self.__private_key.private_bytes(
				encoding=serialization.Encoding.PEM,
				format=serialization.PrivateFormat.PKCS8,
				encryption_algorithm=serialization.BestAvailableEncryption(password)
			)
		return self.__private_key.private_bytes(
			encoding=serialization.Encoding.PEM,
			format=serialization.PrivateFormat.TraditionalOpenSSL,
			encryption_algorithm=serialization.NoEncryption()
		)
1276763
public function hashPassword($password)
    {
        if ($this->mode == self::MODE_BCRYPT) {
            return $this->hashModeBcrypt($password);
        } elseif ($this->mode == self::MODE_PBKDF2) {
            return $this->hashModePbkdf2($password);
        }

        $this->error = 'You have to set the mode...';
        $this->cwsDebug->error($this->error);
    }
601373
public function hash(string $password, string $algorithm): string
    {
        return $this->getPasswordHashStrategy($algorithm)->hash($password);
    }
867190
private static function getPasswordHash($username , $password)
	{
		return sha1( self::getSecurityToken() . $username . self::getGenerated() . $password );
	}
1145069
protected function encodePassword($password)
    {
        $passwordSalted = $this->saltPassword($password);
        $passwordEncoded = password_hash($passwordSalted, PASSWORD_DEFAULT);
        
        return $passwordEncoded;
    }
434786
public function string(): string
    {
        return implode("\n",
            array_map(
                function (PEM $pem) {
                    return $pem->string();
                }, $this->_pems));
    }
1201753
public function verifyPasswordHash(string $password, string $hash, string $salt = ''): string
    {
        $password = ($salt ?: self::$salt) . trim($password);

        return password_verify($password, $hash);
    }
374277
protected static function validatePassword($password) {
		if (empty($password)) {
			throw new InvalidPasswordException();
		}

		$password = \trim($password);

		if (\strlen($password) < 1) {
			throw new InvalidPasswordException();
		}

		return $password;
	}
210416
public static function passwordHash(string $password): string
  {
    $options = ['cost' => self::$cost];

    $hash = password_hash($password, PASSWORD_DEFAULT, $options);
    if ($hash===false)
    {
      throw new \RuntimeException('Function password_hash failed');
    }

    return $hash;
  }