Home > Enterprise >  How to format and remove spaces from a certificate with php?
How to format and remove spaces from a certificate with php?

Time:09-23

In javascript I see something like this which gives the certificate a shape:

substr(certificateX509, '\n');
certificateX509 = certificateX509.substr(certificateX509.indexOf('\n'));
certificateX509 = certificateX509.substr(0, certificateX509.indexOf('\n-----END CERTIFICATE-----'));
certificateX509 = certificateX509.replace(/\r?\n|\r/g, '').replace(/([^\0]{76})/g, '$1\n');
console.log(certificateX509);

output:

MIIIsDCCBpigAwIBAgIICbWlduJzlXMwDQYJKoZIhvcNAQELBQAwgbgxCzAJBgNVBAYTAkVTMUQw
QgYDVQQHDDtCYXJjZWxvbmEgKHNlZSBjdXJyZW50IGFkZHJlc3MgYXQgd3d3LnVhbmF0YWNhLmNv
bS9hZGRyZXNzKTEWMBQGA1UECgwNVUFOQVRBQ0EgUy5BLjEVMBMGA1UECwwMVFNQLVVBTkFUQUNB
MRowGAYDVQQDDBFVQU5BVEFDQSBDQTIgMjAxNjEYMBYGA1UEYQwPVkFURVMtQTY2NzIxNDk5MB4X
DTIyMDUzMDIzMjQwMFoXDTIzMDUzMDIzMjQwMFowfDELMAkGA1UEBhMCRUMxEzARBgNVBAQMCkRV
UkFOIE1FUk8xGDAWBgNVBCoMD1JPQkVSVE8gQUxGT05TTzEZMBcGA1UEBRMQSURDRUMtMTcyMTE3
OTEwNzEjMCEGA1UEAwwaUk9CRVJUTyBBTEZPTlNPIERVUkFOIE1FUk8wggEiMA0GCSqGSIb3DQEB
SPThSuNbh4yKl54tGscLCGPaSnCPS3Rmi7qDTEA4pYHpIy AFQtOg7klg 8ExGBeT2bZUYONWiTJ
/psGY1Sav62xu6L7hLRxsu5mCG6WhDAW9SelysNSjwhqP/IdrLvf3d2WkPfU4KeASlhhlFTsH5ja
uU8kyRxGzs8wY128uUF67bmEX9NWyyFMEPHR729r39BJGiIpe0wiYVd7jdKcPBbzAArRyWsYEk0n
HXZ0RgasOJUNH1q7bWsQf TEHyXM2AkynT8BeidMhgrUL0hmmI0AQ4Z3KUBF3kWFBFph0jNp5Dhs
on fORo=

While in php, I do the following:

$certificado = str_replace('-----BEGIN CERTIFICATE-----', '', $certificado);
$certificado = str_replace('-----END CERTIFICATE-----', '', $certificado);
$certificado=trim($certificado);
$certificado = str_replace("\r\n", "\n", $certificado);

output:

MIIIsDCCBpigAwIBAgIICbWlduJzlXMwDQYJKoZIhvcNAQELBQAwgbgxCzAJBgNV
BAYTAkVTMUQwQgYDVQQHDDtCYXJjZWxvbmEgKHNlZSBjdXJyZW50IGFkZHJlc3Mg
YXQgd3d3LnVhbmF0YWNhLmNvbS9hZGRyZXNzKTEWMBQGA1UECgwNVUFOQVRBQ0Eg
Uy5BLjEVMBMGA1UECwwMVFNQLVVBTkFUQUNBMRowGAYDVQQDDBFVQU5BVEFDQSBD
QTIgMjAxNjEYMBYGA1UEYQwPVkFURVMtQTY2NzIxNDk5MB4XDTIyMDUzMDIzMjQw
MFoXDTIzMDUzMDIzMjQwMFowfDELMAkGA1UEBhMCRUMxEzARBgNVBAQMCkRVUkFO
IE1FUk8xGDAWBgNVBCoMD1JPQkVSVE8gQUxGT05TTzEZMBcGA1UEBRMQSURDRUMt
MTcyMTE3OTEwNzEjMCEGA1UEAwwaUk9CRVJUTyBBTEZPTlNPIERVUkFOIE1FUk8w
ggEiMA0GCSqGSIb3DQEBSPThSuNbh4yKl54tGscLCGPaSnCPS3Rmi7qDTEA4pYHp
Iy AFQtOg7klg 8ExGBeT2bZUYONWiTJ/psGY1Sav62xu6L7hLRxsu5mCG6WhDAW
9SelysNSjwhqP/IdrLvf3d2WkPfU4KeASlhhlFTsH5jauU8kyRxGzs8wY128uUF6
7bmEX9NWyyFMEPHR729r39BJGiIpe0wiYVd7jdKcPBbzAArRyWsYEk0nHXZ0Rgas
OJUNH1q7bWsQf TEHyXM2AkynT8BeidMhgrUL0hmmI0AQ4Z3KUBF3kWFBFph0jNp
5Dhson fORo=

The output I get is very different, 76 characters or code of the certificate get in javascript horizontally and while in PHP 64 characters or code, how can I get the 76 in horizontally line in php?

Note, I have shortened the code of the certificate randomly so that my question is not extensive, but here is the demo with the complete certificate: https://3v4l.org/Q3N2R#v8.1.6

CodePudding user response:

The certificate string in your example is grouped into 64 characters chunks, that's why your current code keeps that length in each line. The javascript code you have uses a regular expression to break it into 76 characters chunks (you can see a 76 there in the 4th line).

You can do something similar in PHP:

$certificado = str_replace('-----BEGIN CERTIFICATE-----', '', $certificado);
$certificado = str_replace('-----END CERTIFICATE-----', '', $certificado);
$certificado = str_replace(" ", "", $certificado);
$certificado = str_replace("\r\n", "", $certificado);
$certificado = preg_replace('/([^\0]{76})/', "$1\r\n", $certificado);

echo $certificado;

You can see the result here: https://onlinephp.io/c/e1aa8

If you prefer, you can replace the preg_replace line with:

$certificado = chunk_split($certificado);
  • Related