Convert a certificate to PEM format.

@param string $cert X509 certificate to convert to PEM format.

@return string
192926
protected function convertCertToPem($cert)
    {
        $output  = '-----BEGIN CERTIFICATE-----'.PHP_EOL;
        $output .= chunk_split($cert, 64, PHP_EOL);
        $output .= '-----END CERTIFICATE-----'.PHP_EOL;
        return $output;
    }
391655
public function withCertificates(Certificate ...$cert): self
    {
        $obj = clone $this;
        $obj->_certs = array_merge($obj->_certs, $cert);
        return $obj;
    }
743681
def certs
      unless @@certs
        pattern = /-{5}BEGIN CERTIFICATE-{5}\n.*?-{5}END CERTIFICATE-{5}\n/m
        dir = @cert_directory
        certs = Dir[File.join(dir, '*.crt')].map {|f| File.read(f) }
        certs = certs.map {|c| c.scan(pattern) }.flatten
        certs.map! {|c| OpenSSL::X509::Certificate.new(c) }
        @@certs = certs.reject {|c| c.not_after < Time.now }
      end
      @@certs
    end
676533
function getCertificates($path)
    {
        return collect($this->files->scandir($path))->filter(function ($value, $key) {
            return ends_with($value, '.crt');
        })->map(function ($cert) {
            $certWithoutSuffix = substr($cert, 0, -4);
            return substr($certWithoutSuffix, 0, strrpos($certWithoutSuffix, '.'));
        })->flip();
    }
173322
def pem_as_string(cert):
    
    if hasattr(cert, 'read'): # File object - return as is
        return cert
    cert = cert.encode('utf-8') if isinstance(cert, unicode) else cert
    if re.match(_PEM_RE, cert):
        return True
    return False
1266010
public static String getThumbprint(final X509Certificate cert) throws CertificateEncodingException {

		if(cert == null)
		{
			return null;
		}

		byte[] rawOctets = cert.getEncoded();

		SHA1Digest digest = new SHA1Digest();

		byte[] digestOctets = new byte[digest.getDigestSize()];

		digest.update(rawOctets, 0, rawOctets.length);

		digest.doFinal(digestOctets, 0);

		return new String(Base64.encode(digestOctets));
	}
726162
public static URI getResponderURI(X509Certificate cert) {
        try {
            return getResponderURI(X509CertImpl.toImpl(cert));
        } catch (CertificateException ce) {
            // treat this case as if the cert had no extension
            return null;
        }
    }
1121234
def _get_certificate_obj(cert):
    '''
    
    '''
    if isinstance(cert, M2Crypto.X509.X509):
        return cert

    text = _text_or_file(cert)
    text = get_pem_entry(text, pem_type='CERTIFICATE')
    return M2Crypto.X509.load_cert_string(text)
1018477
private function _checkValidity(Certificate $cert)
    {
        $refdt = $this->_config->dateTime();
        $validity = $cert->tbsCertificate()->validity();
        if ($validity->notBefore()
            ->dateTime()
            ->diff($refdt)->invert) {
            throw new PathValidationException(
                "Certificate validity period has not started.");
        }
        if ($refdt->diff($validity->notAfter()
            ->dateTime())->invert) {
            throw new PathValidationException("Certificate has expired.");
        }
    }
162326
public static function getPKCS1($key)
	{
		$prefix = '-----BEGIN RSA PUBLIC KEY-----';
		$postfix = '-----END RSA PUBLIC KEY-----';
		
		$key = str_replace($prefix, '', $key);
		$key = str_replace($postfix, '', $key);
		$key = str_replace("\n", '', $key);
		
		$lines = str_split($key, 65);
		$body = implode("\n", $lines);
		$result = $prefix . "\n";
		$result .= $body . "\n";
		$result .= $postfix;
		
		return $result;
	}
156778
public function parse(Certificate $certificate)
    {
        $rawData = openssl_x509_parse($certificate->getPEM());

        if (!\is_array($rawData)) {
            throw new CertificateParsingException(
                sprintf('Fail to parse certificate with error: %s', openssl_error_string())
            );
        }

        if (!isset($rawData['subject']['CN'])) {
            throw new CertificateParsingException('Missing expected key "subject.cn" in certificate');
        }
        if (!isset($rawData['serialNumber'])) {
            throw new CertificateParsingException('Missing expected key "serialNumber" in certificate');
        }
        if (!isset($rawData['validFrom_time_t'])) {
            throw new CertificateParsingException('Missing expected key "validFrom_time_t" in certificate');
        }
        if (!isset($rawData['validTo_time_t'])) {
            throw new CertificateParsingException('Missing expected key "validTo_time_t" in certificate');
        }

        $subjectAlternativeName = [];

        if (isset($rawData['extensions']['subjectAltName'])) {
            $subjectAlternativeName = array_map(
                function ($item) {
                    return explode(':', trim($item), 2)[1];
                },
                array_filter(
                    explode(
                        ',',
                        $rawData['extensions']['subjectAltName']
                    ),
                    function ($item) {
                        return false !== strpos($item, ':');
                    }
                )
            );
        }

        return new ParsedCertificate(
            $certificate,
            $rawData['subject']['CN'],
            isset($rawData['issuer']['CN']) ? $rawData['issuer']['CN'] : null,
            $rawData['subject'] === $rawData['issuer'],
            new \DateTime('@'.$rawData['validFrom_time_t']),
            new \DateTime('@'.$rawData['validTo_time_t']),
            $rawData['serialNumber'],
            $subjectAlternativeName
        );
    }
1093070
def certs
      @@sources ||= begin
        pattern = /-{5}BEGIN CERTIFICATE-{5}\n.*?-{5}END CERTIFICATE-{5}\n/m
        pairs = Dir[File.join(@dir, '*.crt')].map do |name|
          File.open(name, "r:UTF-8") do |f|
            pems = f.read.scan(pattern)
            certs = pems.map {|pem| OpenSSL::X509::Certificate.new(pem) }
            certs.reject! {|cert| cert.not_after < Time.now }
            [name, certs]
          end
        end
        Hash[pairs]
      end
      @@sources.values.flatten
    end
40084
public static function isBlackListed($certificate)
    {
        $certificate = trim($certificate);
        $lines = explode("\n", $certificate);

        // Kludgily remove the PEM padding
        array_shift($lines); array_pop($lines);

        $der_cert = base64_decode(implode("", $lines));
        $fingerprint = sha1($der_cert);

        return in_array($fingerprint, self::blacklistedCerts());
    }
1149358
private function _hasEqualPublicKey(Certificate $cert): bool
    {
        $kid1 = $this->_tbsCertificate->subjectPublicKeyInfo()->keyIdentifier();
        $kid2 = $cert->_tbsCertificate->subjectPublicKeyInfo()->keyIdentifier();
        return $kid1 == $kid2;
    }
1089010
public function clientSideCert($cert, $key, $passphrase = null, $encoding = 'PEM')
    {
        $this->client_cert          = $cert;
        $this->client_key           = $key;
        $this->client_passphrase    = $passphrase;
        $this->client_encoding      = $encoding;

        return $this;
    }
1355178
public static java.security.cert.X509Certificate convert(javax.security.cert.X509Certificate cert) {
        try {
            byte[] encoded = cert.getEncoded();
            ByteArrayInputStream bis = new ByteArrayInputStream(encoded);
            java.security.cert.CertificateFactory cf
                = java.security.cert.CertificateFactory.getInstance("X.509");
            return (java.security.cert.X509Certificate)cf.generateCertificate(bis);
        } catch (java.security.cert.CertificateEncodingException e) {
        } catch (javax.security.cert.CertificateEncodingException e) {
        } catch (java.security.cert.CertificateException e) {
        }
        return null;
    }
429054
private static function _getCertKeyId(Certificate $cert): string
    {
        $exts = $cert->tbsCertificate()->extensions();
        if ($exts->hasSubjectKeyIdentifier()) {
            return $exts->subjectKeyIdentifier()->keyIdentifier();
        }
        return $cert->tbsCertificate()
            ->subjectPublicKeyInfo()
            ->keyIdentifier();
    }