#codec #string-literal #base64 #binary-encoding #hex #decoding #binary

binary_macros

Macros for decoding base64-like encodings in string literals to [u8] literals

15 releases (1 stable)

Uses old Rust 2015

1.0.0 Sep 25, 2020
0.6.3 Mar 3, 2017
0.5.4 Mar 2, 2017
0.5.0 Feb 28, 2017
0.1.2 Aug 24, 2016

#2070 in Encoding

Download history 34/week @ 2024-04-22 13/week @ 2024-04-29 37/week @ 2024-05-06 20/week @ 2024-05-13 59/week @ 2024-05-20 23/week @ 2024-05-27 96/week @ 2024-06-03 22/week @ 2024-06-10 17/week @ 2024-06-17 73/week @ 2024-06-24 9/week @ 2024-07-01 19/week @ 2024-07-08 14/week @ 2024-07-15 202/week @ 2024-07-22 22/week @ 2024-07-29 55/week @ 2024-08-05

295 downloads per month

MIT license

6KB

binary_macros

Rust macros for decoding base64 and hexadecimal -like encodings from string literals to [u8] literals at compile-time.

Bug reports, pull requests etc. welcome!

Why are these macros useful? Let's say you want to include a binary blob inside your crate; a public key, for example. You can do that with the include_bytes!() macro from the Rust std. However, editing, viewing and copy-pasting raw binary blobs is hard! There is a reason public keys are often distributed as base64. On the other hand, if you include text with the include_str!() macro, you'll have to decode it runtime. Why defer it to runtime if you can do it compile-time?

To get started, include this in your Cargo.toml dependencies:

[dependencies]
binary_macros = "1.0.0"

And this to your source code:

#[macro_use]
extern crate binary_macros;

...and then you are ready to use the macros!

let public_key = base64!("aeSwwNywhbrmSuk32vuZmQRWHOKXbU1LziU18GAxVOE=");

This crate also supports prefixing the input with file: or env: to load input from file (path relative to current working directory) or environment variable. The env: prefix supports also .env files. (Check out rust-dotenv)

let public_key_a = base64!("file:id_rsa.pub");
let public_key_b = base64!("env:MYCRATE_PUBLIC_KEY");

Included macros:

Number 97 (ASCII 'a') included with different encodings for example:

base16!("61") // Hexadecimal. Uses numbers 0-9 and A-F. Group of 2 digits = 1 byte.
base32!("C4======") // Base32. Uses numbers A-Z and 2-7. Group of 8 digits = 5 bytes, uses = as end padding.
base32hex!("ME======") // Base32 that uses extended hexadecimal: 0-9 and A-V. Group of 8 digits = 5 bytes, uses = as end padding.
base64!("YQ==") // Base64. Uses numbers A-Z, a-z, 0-9, + and /. Group of 4 digits = 3 bytes, uses = as end padding.
base64url!("_A==") // URL-compatible Base64. Uses numbers A-Z, a-z, 0-9, - and _. Group of 4 digits = 3 bytes, uses = as end padding.
base32_nopad!("C4") // No padding version of base32.
base32hex_nopad!("ME") // No padding version of base32hex.
base64_nopad!("YQ") // No padding version of base64.
base64url_nopad!("_A") // No padding version of base64url.

Huge kudos to the data-encoding crate for providing a wide variety of encodings, and proc-macro-hack for providing an easy way to use procedural bang macros on stable Rust. (This crate will continue using proc-macro-hack for the foreseeable future to support old compilers.)

Dependencies

~155KB