If this has been addressed, my apologies. I found a posting The State of Lucee - #9 by Igal that may or may not have moved forward. Having said that …
Encoding a string to base64 is a breeze using toBase64
.
Decoding the base64 encoded string is a bit clunky. (so am I )
encodedString = toBase64('hello world');
dump(encodedString); // aGVsbG8gd29ybGQ=
shouldBeHelloWorld = toString(binaryDecode(encodedString, 'base64'));
dump(shouldBeHelloWorld); // hello world
Therefore, I propose the creation of a humble
fromBase64()
function,
isBase64()
function,
and a base64
type option for the isValid()
function.
Another possibility is to add atob()
and btoa()
functions to mimic JavaScript’s implementation.
Finally, if you’ve landed here because you forgot how to decode from base64, like me this morning, I offer the following helper functions. Your mileage may vary.
// I decode a base64 encoded string returned by toBase64()
public string function fromBase64(
required string b64String hint="base64 encoded string"
) {
return toString(binaryDecode(arguments.b64String, 'base64'));
}
public boolean function isBase64(
required string stringToTest hint="base64 encoded string. TRIM first!"
) {
return reFindNoCase('^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$', arguments.stringToTest, 1, false);
}
For further reading:
Pete Freitag has a great post at URL Safe Base64 Encoding / Decoding in CFML