Work with Certificates and Keys as Strings

You can use the following command in a UNIX shell to concatenate a PEM-formatted certificate with \n (escaped new-line):

awk 'NF {sub(/\r/, ""); printf "%s\\n",$0;}' certificate.pem | pbcopy

This copies the value into to your clipboard buffer so you can paste it where needed.

More information

When working with certificates or keys in rules or Management API v2 requests, you will most likely require a string representation of the file.

If you open a certificate file (cer, pem) with a text editor, you'll see something like this:

-----BEGIN CERTIFICATE-----
MIICzDCCAbQCCQDH8GvxPIeH+DANBgkqhkiG9w0BAQsFADAoMQswCQYDVQQGEwJh
cjEZMBcGA1UEAwwQaHR0cHM6Ly9uaWNvLmNvbTAeFw0xOTA0MDgxODA3NTVaFw0y
//
// more lines of base64-encoded information
//
nSWyabd+LiBGtLTMB+ZLbOIi3EioWPGw/nHOI8jzPrqhiCLuZCSQmiqrLQYNsc1W
-----END CERTIFICATE-----


The lines between the -----BEGIN CERTIFICATE----- header and -----END CERTIFICATE----- footer contain base64-encoded binary information. Public keys and private keys (.key files) will look similar, with just a different header/footer.

For a string representation of a certificate/key file, you will need to concatenate everything in one line, with a \n (escaped new-line) sequence replacing the actual new lines in the file. So from the above sample you'd obtain something like this:

"-----BEGIN CERTIFICATE-----
\nMIICzDCCAbQCCQDH8GvxPIeH+DANBgkqhkiG9w0BAQsFADAoMQswCQYDVQQGEwJh\n
[..all the other lines..]
-----END CERTIFICATE-----\n"


Learn more