I want something as simple as "string" -> base64. With the older base64.encode-str it was easy (and sounded "more clojure", but the newer clojure.data.codec.base64
requires input and output streams and seems an ugly wrapper around Java way of doing things.
So, what is the way, having a string, to get a base64 encoded array? Thanks
For those trying (as I was) to turn images into data URIs (for embedding images in HTML):
You can use encode function and pass array of bytes:
ztellman/byte-transforms also support base64 encode/decode.
Possible duplicate of Clojure equivalent of python's base64 encode and decode
The Tupelo library has Clojure wrappers around the base Java Base64 and Base64Url functionality. A look at the unit tests show the code in action:
where the input & output values are plain strings (there is also a variant for byte arrays).
The API docs are here.
Four years later, but I think this is worth mentioning if you're at JDK 1.8 or greater. It just uses
java.util.Base64
To Encode String -> Base64:
To Encode String -> Base64 (String):
To Decode Base64 (byte[] or String) -> String:
There's one more step needed for the other answer: converting the byte-array result of
encode
into a string. Here's what I do: