Convert .cer file to PEM format in PHP

Hi tyros!

Now, i show you how to convert a .cer file to PEM format with PHP.

I have a .cer file, like this:


But i need it in this format:

-----BEGIN CERTIFICATE----- 
MIIE2jCCA8KgAwIBAgIUMjAwMDEwMDAwMDAyMDAwMDAyOTMwDQYJKoZIhvcNAQEF 
BQAwggFcMRowGAYDVQQDDBFBLkMuIDIgZGUgcHJ1ZWJhczEvMC0GA1UECgwmU2Vy 
dmljaW8gZGUgQWRtaW5pc3RyYWNpw7NuIFRyaWJ1dGFyaWExODA2BgNVBAsML0Fk 
... 
JwYJKoZIhvcNAQkBFhphc2lzbmV0QHBydWViYXMuc2F0LmdvYi5teDEmMCQGA1UE 
CQwdQXYuIEhpZGFsZ28gNzcsIENvbC4gR3VlcnJlcm8xDjAMBgNVBBEMBTA2MzAw 
MQswCQYDVQQGEwJNWDEZMBcGA1UECAwQRGlzdHJpdG8gRmVkZXJhbDESMBAGA1UE 
... 
aSBHYW5kYXJhIEJhdXRpc3RhMB4XDTEyMTAyNjE5MjI0M1oXDTE2MTAyNjE5MjI0 
M1owggFTMUkwRwYDVQQDE0BBU09DSUFDSU9OIERFIEFHUklDVUxUT1JFUyBERUwg 
RElTVFJJVE8gREUgUklFR08gMDA0IERPTiBNQVJUSU4gMWEwXwYDVQQpE1hBU09D 
SUFDSU9OIERFIEFHUklDVUxUT1JFUyBERUwgRElTVFJJVE8gREUgUklFR08gMDA0 
... 
-----END CERTIFICATE----- 


Here the code:

public function GetCertPEM($fileName) {
    $cerContent = file_get_contents($fileName);
    /* Convert .cer to .pem, cURL uses .pem */
    $cerPEM = '-----BEGIN CERTIFICATE-----' . PHP_EOL . chunk_split(base64_encode($cerContent), 64, PHP_EOL) . '-----END CERTIFICATE-----' . PHP_EOL;
    return $cerPEM;
}


easy!

No hay comentarios:

Publicar un comentario