Crates.io | ubase64 |
lib.rs | ubase64 |
version | 0.0.1 |
source | src |
created_at | 2024-08-12 01:28:59.75138 |
updated_at | 2024-08-12 01:28:59.75138 |
description | Lightweight, simple, and configurable base64 encode and decode. |
homepage | https://github.com/xelamonster/ubase64 |
repository | https://github.com/xelamonster/ubase64 |
max_upload_size | |
id | 1333770 |
size | 37,678 |
Lightweight, simple, and configurable base64 encode and decode.
⚠️ WARNING: this is alpha-stage software and has not been extensively tested. Use at your own risk.
Contributions (tests and benchmarks especially) are very welcome!
Yes, indeed. There's a wide array of base64 Rust implementations out there, some likely work very well, but none of them are perfect. Neither is this I'm sure, as much as I tried to address the issues I saw there's always room for improvement (PRs welcome! 😜), but it's done quite nicely for my needs so far.
I had four main goals in designing ubase64
:
ubase64
is to provide enough options to support as many potential base64
use cases as possible while keeping the "basic" API clean and simple.ubase64
tries to strike a balance between the best possible performance
and the smallest possible binary size.ubase64
should work in a wide variety of contexts. In addition
to library usage, it provides a command-line interface so you can benefit from fast
encoding and decoding in the terminal too!You can use ubase64
in your project as a library, or install it as a command-line binary.
Add ubase64
to your project's dependencies:
[dependencies]
ubase64 = { version = "0.0.1", features = [] }
Please note: the package's default features include dependencies only needed for a binary
install. If you don't need these, ensure you have an empty dependency array as shown above;
you can install with cargo add --no-default-features ubase64
to do this automatically.
Example library usage:
use ubase64::encode_std;
let encoded = encode_std(b"Hello, world!");
assert_eq!(encoded, "SGVsbG8sIHdvcmxkIQ==");
Install ubase64
as a binary with Cargo:
cargo install ubase64
Example binary usage:
$ ubase64 encode "Hello, world!"
SGVsbG8sIHdvcmxkIQ==
$ echo -n "Hello, world!" > test.txt
$ ubase64 encode -f test.txt
SGVsbG8sIHdvcmxkIQ==
ubase64
installed as a library includes only two dependencies (thiserror
and miette
, both for
error handling).encode_std
and decode_std
are provided to make the
most common use case as painless as possible, and the more complex configurations should
still be relatively intuitive.base64
utility. I found
that interface confusing (why does it encode by default and require a flag to decode?) and
it's always a process for me to figure out how to give it a string input, so I took a more
opinionated approach.Two alphabet options are provided, standard (STD_ALPHABET
) and URL-safe (URL_ALPHABET
).
These are represented as 64-byte const arrays.
For decoding, the base64_reverse_alphabet
function is used to create a reverse lookup table
from the alphabet array at compile time. This is implemented as a 256-byte const array which
maps the byte value of each character in the alphabet to its index in the array. Only 64 of
the 256 values will ever be filled, but this is an insignificant amount of memory and should
incur less overhead than an equivalent map type.
Decoded values are arbitrary data and thus are represented as &[u8]
(as input) or Vec<u8>
(as output).
Encoded values are strings represented as &str
(input) or String
(output).