Base64

Common
JVM
JS
Native
1.8
@ExperimentalEncodingApi open class Base64
(source)

Provides Base64 encoding and decoding functionality. Base64 encoding, as defined by the RFC 4648 and a few other RFCs, transforms arbitrary binary data into a sequence of printable characters.

For example, a sequence of bytes 0xC0 0xFF 0xEE will be transformed to a string "wP/u" using a Base64 encoding defined by the RFC 4648. Decoding that string will result in the original byte sequence.

Base64 is not an encryption scheme and should not be used when data needs to be secured or obfuscated.

Characters used by a particular Base64 scheme form an alphabet of 64 regular characters and an extra padding character.

Almost all Base64 encoding schemes share the same first 62 characters of an alphabet, which are 'A'..'Z' followed by 'a'..'z', but the last two characters may vary. RFC 4648 section 4 defines an alphabet that uses '+' and '/' as the last two characters, while the URL-safe alphabet defined in RFC 4648 section 5 uses '-' and '_' instead.

When decoding, a Base64 scheme usually accepts only characters from its alphabet, presence of any other characters is treated as an error (see Base64.Mime for an exception to this rule). That also implies that a Base64 scheme could not decode data encoded by another Base64 scheme if their alphabets differ.

In addition to 64 characters from the alphabet, Base64-encoded data may also contain one or two padding characters '=' at its end. Base64 splits data that needs to be encoded into chunks three bytes long, which are then transformed into a sequence of four characters (meaning that every character encodes six bits of data). If the number of bytes constituting input data is not a multiple of three (for instance, input consists of only five bytes), the data will be padded by zero bits first and only then transformed into Base64-alphabet characters. If padding takes place, the resulting string is augmented by '='. The padding could consist of zero, two or four bits, thus encoded data will contain zero, one or two padding characters ('='), respectively. The inclusion of padding characters in the resulting string depends on the PaddingOption set for the Base64 instance.

This class is not supposed to be inherited or instantiated by calling its constructor. However, predefined instances of this class are available for use. The companion object Base64.Default is the default instance of Base64. There are also Base64.UrlSafe and Base64.Mime instances. The padding option for all predefined instances is set to PaddingOption.PRESENT. New instances with different padding options can be created using the withPadding function.

import kotlin.io.encoding.*
import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val encoded = Base64.Default.encode("Hello, World!".encodeToByteArray())
println(encoded) // SGVsbG8sIFdvcmxkIQ==

val decoded = Base64.Default.decode(encoded)
println(decoded.decodeToString()) // Hello, World!
//sampleEnd
}
import kotlin.io.encoding.*
import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
// Default encoding uses '/' and '+' as the last two characters of the Base64 alphabet
println(Base64.Default.encode(byteArrayOf(-1, 0, -2, 0))) // /wD+AA==
// Mime's alphabet is the same as Default's
println(Base64.Mime.encode(byteArrayOf(-1, 0, -2, 0))) // /wD+AA==
// UrlSafe encoding uses '_' and '-' as the last two Base64 alphabet characters
println(Base64.UrlSafe.encode(byteArrayOf(-1, 0, -2, 0))) // _wD-AA==

// UrlSafe uses `-` and `_`, so the following string could not be decoded
// Base64.UrlSafe.decode("/wD+AA==") // will fail with IllegalArgumentException
//sampleEnd
}
import kotlin.io.encoding.*
import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
// "base".length == 4, which is not multiple of 3;
// base64-encoded data padded with 4 bits
println(Base64.Default.encode("base".encodeToByteArray())) // YmFzZQ==
// "base6".length == 5, which is not multiple of 3;
// base64-encoded data padded with 2 bits
println(Base64.Default.encode("base6".encodeToByteArray())) // YmFzZTY=
// "base64".length == 6, which is the multiple of 3, so no padding is required
println(Base64.Default.encode("base64".encodeToByteArray())) // YmFzZTY0
//sampleEnd
}

Types

Common
JVM
JS
Native
1.0

Default

The "base64" encoding specified by RFC 4648 section 4, Base 64 Encoding.

companion object Default : Base64
Common
JVM
JS
Native
1.0

PaddingOption

An enumeration of the possible padding options for Base64 encoding and decoding.

enum class PaddingOption

Functions

Common
JVM
JS
Native
1.0

decode

Decodes symbols from the specified source array or its subrange. Returns a ByteArray containing the resulting bytes.

fun decode(
    source: ByteArray,
    startIndex: Int = 0,
    endIndex: Int = source.size
): ByteArray

Decodes symbols from the specified source char sequence or its substring. Returns a ByteArray containing the resulting bytes.

fun decode(
    source: CharSequence,
    startIndex: Int = 0,
    endIndex: Int = source.length
): ByteArray
Common
JVM
JS
Native
1.0

decodeIntoByteArray

Decodes symbols from the specified source array or its subrange and writes resulting bytes into the destination array. Returns the number of bytes written.

fun decodeIntoByteArray(
    source: ByteArray,
    destination: ByteArray,
    destinationOffset: Int = 0,
    startIndex: Int = 0,
    endIndex: Int = source.size
): Int

Decodes symbols from the specified source char sequence or its substring and writes resulting bytes into the destination array. Returns the number of bytes written.

fun decodeIntoByteArray(
    source: CharSequence,
    destination: ByteArray,
    destinationOffset: Int = 0,
    startIndex: Int = 0,
    endIndex: Int = source.length
): Int
Common
JVM
JS
Native
1.0

encode

Encodes bytes from the specified source array or its subrange. Returns a string with the resulting symbols.

fun encode(
    source: ByteArray,
    startIndex: Int = 0,
    endIndex: Int = source.size
): String
Common
JVM
JS
Native
1.0

encodeIntoByteArray

Encodes bytes from the specified source array or its subrange and writes resulting symbols into the destination array. Returns the number of symbols written.

fun encodeIntoByteArray(
    source: ByteArray,
    destination: ByteArray,
    destinationOffset: Int = 0,
    startIndex: Int = 0,
    endIndex: Int = source.size
): Int
Common
JVM
JS
Native
1.0

encodeToAppendable

Encodes bytes from the specified source array or its subrange and appends resulting symbols to the destination appendable. Returns the destination appendable.

fun <A : Appendable> encodeToAppendable(
    source: ByteArray,
    destination: A,
    startIndex: Int = 0,
    endIndex: Int = source.size
): A
Common
JVM
JS
Native
1.0

encodeToByteArray

Encodes bytes from the specified source array or its subrange. Returns a ByteArray containing the resulting symbols.

fun encodeToByteArray(
    source: ByteArray,
    startIndex: Int = 0,
    endIndex: Int = source.size
): ByteArray
Common
JVM
JS
Native
2.0

withPadding

Returns a new Base64 instance that is equivalent to this instance but configured with the specified padding option.

fun withPadding(option: PaddingOption): Base64

Companion Object Properties

Common
JVM
JS
Native
1.0

Mime

The encoding specified by RFC 2045 section 6.8, Base64 Content-Transfer-Encoding.

val Mime: Base64
Common
JVM
JS
Native
1.0

UrlSafe

The "base64url" encoding specified by RFC 4648 section 5, Base 64 Encoding with URL and Filename Safe Alphabet.

val UrlSafe: Base64