encodeBase64

Encode a ubyte array as Base64, placing the result onto an Vec!ubyte.

  1. ubyte[] encodeBase64(const(ubyte)[] buf, char plusChar, char slashChar)
  2. void encodeBase64(const(ubyte)[] input, Vec!ubyte outBuf, char plusChar, char slashChar)
    nothrow @nogc @trusted
    void
    encodeBase64
    (
    scope const(ubyte)[] input
    ,
    scope ref Vec!ubyte outBuf
    ,
    char plusChar = '+'
    ,
    char slashChar = '/'
    )

Examples

Test encoding of data which has a length which CANNOT be cleanly encoded. This typically means that there's padding.

// Note: encoded data leaked there.
// 1 byte 
{
    enum data = cast(immutable(ubyte)[])"A";
    assert(data.encodeBase64 == "QQ==");
}
// 2 bytes
{
    enum data = cast(immutable(ubyte)[])"AB";
    assert(data.encodeBase64 == "QUI=");
}
// 2 bytes
{
    enum data = [0xFF, 0xFF];
    assert(data.encodeBase64 == "//8=");
}
// 4 bytes
{
    enum data = [0xDE, 0xAD, 0xBA, 0xBE];
    assert(data.encodeBase64 == "3q26vg==");
}
// 37 bytes
{
    enum data = cast(immutable(ubyte)[])"A Very Very Very Very Large Test Blob";
    assert(data.encodeBase64 == "QSBWZXJ5IFZlcnkgVmVyeSBWZXJ5IExhcmdlIFRlc3QgQmxvYg==");
}

Test nogc encoding

{

    enum data = cast(immutable(ubyte)[])"A Very Very Very Very Large Test Blob";
    Vec!ubyte outBuf;
    data.encodeBase64(outBuf); 
    assert(outBuf[] == "QSBWZXJ5IFZlcnkgVmVyeSBWZXJ5IExhcmdlIFRlc3QgQmxvYg==");     
}

{
    enum data = cast(immutable(ubyte)[])"abc123!?$*&()'-=@~";
    Vec!ubyte outBuf;
    data.encodeBase64(outBuf);
    assert(outBuf[] == "YWJjMTIzIT8kKiYoKSctPUB+");
}

Make sure we can decode what we encode.

// Test an example string
{
    enum data = cast(immutable(ubyte)[])"abc123!?$*&()'-=@~";
    assert(data.encodeBase64.decodeBase64 == data);
}
// Test an example from Ion data
{
    enum data = cast(immutable(ubyte)[])"a b c d e f g h i j k l m n o p q r s t u v w x y z";
    assert(data.encodeBase64.decodeBase64 == data);
}

Meta