decode

Decodes the first PEM document found in text

The input should be in the form:

-----BEGIN {label}-----
Base64-encoded {content} with line breaks every 64 characters
-----END {label}-----

Any content before -----BEGIN {label}----- and after -----END {label}----- is ignored

Only the first complete document is decoded. For decoding of multiple documents, use PemDocument.decodeToSequence

Return

the decoded PemDocument

Parameters

text

the textual input that may contain a PEM document

Throws

if no PEM documents present in text, or the PEM encoding is invalid

Samples

val pem: String = """
-----BEGIN CUSTOM-----
aGVsbG8gd29ybGQ=
-----END CUSTOM-----

""".trimIndent()

val document = PemDocument.decode(pem)

assertEquals(
    expected = PemDocument(
        label = PemLabel("CUSTOM"),
        content = "hello world".encodeToByteString()
    ),
    actual = document
)

Decodes the first PEM document found in bytes. The bytes are treated as an encoded string

The input should be in the form:

-----BEGIN {label}-----
Base64-encoded {content} with line breaks every 64 characters
-----END {label}-----

Any content before -----BEGIN {label}----- and after -----END {label}----- is ignored

Only the first complete document is decoded. For decoding of multiple documents, use PemDocument.decodeToSequence

Return

the decoded PemDocument

Parameters

bytes

the byte array that may contain a PEM document

Throws

if no PEM documents present in bytes, or the PEM encoding is invalid

Samples

val pem: ByteArray = """
-----BEGIN CUSTOM-----
aGVsbG8gd29ybGQ=
-----END CUSTOM-----

""".trimIndent().encodeToByteArray()

val document = PemDocument.decode(pem)

assertEquals(
    expected = PemDocument(
        label = PemLabel("CUSTOM"),
        content = "hello world".encodeToByteString()
    ),
    actual = document
)

Decodes the first PEM document found in bytes. The bytes are treated as an encoded string

The input should be in the form:

-----BEGIN {label}-----
Base64-encoded {content} with line breaks every 64 characters
-----END {label}-----

Any content before -----BEGIN {label}----- and after -----END {label}----- is ignored

Only the first complete document is decoded. For decoding of multiple documents, use PemDocument.decodeToSequence

Return

the decoded PemDocument

Parameters

bytes

the byte array that may contain a PEM document

Throws

if no PEM documents present in bytes, or the PEM encoding is invalid

Samples

val pem: ByteString = """
-----BEGIN CUSTOM-----
aGVsbG8gd29ybGQ=
-----END CUSTOM-----

""".trimIndent().encodeToByteString()

val document = PemDocument.decode(pem)

assertEquals(
    expected = PemDocument(
        label = PemLabel("CUSTOM"),
        content = "hello world".encodeToByteString()
    ),
    actual = document
)

Decodes the first PEM document found in source. The source is treated as an encoded string and is consumed up to and including the decoded document

The input should be in the form:

-----BEGIN {label}-----
Base64-encoded {content} with line breaks every 64 characters
-----END {label}-----

Any content before -----BEGIN {label}----- and after -----END {label}----- is ignored

Only the first complete document is decoded. For decoding of multiple documents, use PemDocument.decodeToSequence

Return

the decoded PemDocument

Parameters

source

the source to read from

Throws

if no PEM documents present in source, or the PEM encoding is invalid

Samples

val pem = """
-----BEGIN CUSTOM-----
aGVsbG8gd29ybGQ=
-----END CUSTOM-----

""".trimIndent()

val buffer = Buffer()
buffer.writeString(pem)

val document = PemDocument.decode(buffer)

assertEquals(
    expected = PemDocument(
        label = PemLabel("CUSTOM"),
        content = "hello world".encodeToByteString()
    ),
    actual = document
)