Anyone got a base64url example in squirrel?


#1

Like the subject says: can anyone share a working, performant, implementation of base64url encode/decode in Squirrel?


#2
Base64URL <- {};

Base64URL.padString <- function (input) {
    local strLen = input.len();
    local diff = strLen % 4;
    if (!diff) return input;

    local pos = strLen;
    local padLen = 4 - diff;
    local buffer = blob(strLen + padLen);
    buffer.writestring(input);
    while (padLen--) {
        buffer.writestring("=");
        pos++;
    }

    return buffer.tostring();
}

Base64URL.fromBase64 <- function (base64string) {
    local rs = "";
    local i = 0;
    local a = 0;

    do {
        if (a > base64string.len()) break;

        i = base64string.find("=", a);

        if (i != null) {
            rs = rs + base64string.slice(a, i);
            a = i + 1;
        } else {
            rs = rs + base64string.slice(a);
        }
    } while (i != null);

    base64string = rs;

    rs = "";
    i = 0;
    a = 0;
    do {
        if (a > base64string.len()) break;

        i = base64string.find("+", a);

        if (i != null) {
            rs = rs + base64string.slice(a, i) + "-";
            a = i + 1;
        } else {
            rs = rs + base64string.slice(a);
        }
    } while (i != null);

    base64string = rs;

    rs = "";
    i = 0;
    a = 0;
    do {
        if (a > base64string.len()) break;

        i = base64string.find("/", a);

        if (i != null) {
            rs = rs + base64string.slice(a, i) + "_";
            a = i + 1;
        } else {
            rs = rs + base64string.slice(a);
        }
    } while (i != null);

    return rs;
}

Base64URL.toBase64 <- function (base64url) {
    local rs = "";
    local i = 0;
    local a = 0;

    do {
        if (a > base64url.len()) break;

        i = base64url.find("-", a);

        if (i != null) {
            rs = rs + base64url.slice(a, i) + "+";
            a = i + 1;
        } else {
            rs = rs + base64url.slice(a);
        }
    } while (i != null);

    base64url = rs;

    rs = "";
    i = 0;
    a = 0;
    do {
        if (a > base64url.len()) break;

        i = base64url.find("_", a);

        if (i != null) {
            rs = rs + base64url.slice(a, i) + "/";
            a = i + 1;
        } else {
            rs = rs + base64url.slice(a);
        }
    } while (i != null);

    return padString(rs);
}

Base64URL.encode <- function (input){
    return fromBase64(http.base64encode(input));
}

Base64URL.decode <- function (base64url) {
    return http.base64decode(toBase64(base64url));
}
server.log(Base64URL.encode("ladies and gentlemen we are floating in space"));
server.log(Base64URL.fromBase64("qL8R4QIcQ/ZsRqOAbeRfcZhilN/MksRtDaErMA=="));
server.log(Base64URL.toBase64("qL8R4QIcQ_ZsRqOAbeRfcZhilN_MksRtDaErMA"));
server.log(Base64URL.decode("cmlkZTogZHJlYW1zIGJ1cm4gZG93bg"));

Logs:

2017-09-26 16:16:02 Streamed from "Cheeky": [status]       Agent restarted: reload.
2017-09-26 16:16:02 Streamed from "Cheeky": [agent.log]    Location class instantiated on the agent
2017-09-26 16:16:02 Streamed from "Cheeky": [agent.log]    bGFkaWVzIGFuZCBnZW50bGVtZW4gd2UgYXJlIGZsb2F0aW5nIGluIHNwYWNl
2017-09-26 16:16:02 Streamed from "Cheeky": [agent.log]    qL8R4QIcQ_ZsRqOAbeRfcZhilN_MksRtDaErMA
2017-09-26 16:16:02 Streamed from "Cheeky": [agent.log]    qL8R4QIcQ/ZsRqOAbeRfcZhilN/MksRtDaErMA==
2017-09-26 16:16:02 Streamed from "Cheeky": [agent.log]    ride: dreams burn down

Can’t pretend it’s all my own work, just ported


#3

Thanks. Maybe we should just have arbitrary-base encode/decode functions in the API…?