#hex #decoding #base64 #codec #base-16 #constant-evaluation

no-std const-decoder

Constant functions for converting hex- and base64-encoded strings into bytes

3 releases (breaking)

0.3.0 Nov 1, 2022
0.2.0 Oct 10, 2021
0.1.0 Apr 16, 2021

#1500 in Encoding

Download history 859/week @ 2023-11-27 964/week @ 2023-12-04 861/week @ 2023-12-11 835/week @ 2023-12-18 357/week @ 2023-12-25 725/week @ 2024-01-01 1026/week @ 2024-01-08 872/week @ 2024-01-15 868/week @ 2024-01-22 1069/week @ 2024-01-29 1134/week @ 2024-02-05 1316/week @ 2024-02-12 1437/week @ 2024-02-19 1436/week @ 2024-02-26 1075/week @ 2024-03-04 352/week @ 2024-03-11

4,368 downloads per month
Used in 7 crates (6 directly)

MIT/Apache

25KB
395 lines

Constant Functions for Hex / Base64 Decoding

Build Status License: MIT OR Apache-2.0 rust 1.57+ required

Documentation: Docs.rs crate docs (master)

Constant functions for converting hex- and base64-encoded strings into bytes in Rust. Works on stable Rust and in no-std environments. Base-(2,4,8,16,32,64) encodings with custom alphabets are supported as well.

Usage

Add this to your Crate.toml:

[dependencies]
const-decoder = "0.3.0"

Example of usage:

use const_decoder::Decoder;
// An Ed25519 secret key.
const SECRET_KEY: [u8; 64] = Decoder::Hex.decode(
    b"9e55d1e1aa1f455b8baad9fdf975503655f8b359d542fa7e4ce84106d625b352\
      06fac1f22240cffd637ead6647188429fafda9c9cb7eae43386ac17f61115075",
);

Bech32 encoding:

use const_decoder::{Decoder, Encoding};
const BECH32: Decoder = Decoder::custom("qpzry9x8gf2tvdw0s3jn54khce6mua7l");
// Sample address from the Bech32 spec excluding the `tb1q` prefix
// and the checksum suffix.
const SAMPLE_ADDR: [u8; 32] =
    BECH32.decode(b"rp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q");

See more examples in the crate docs.

Alternatives

hex-literal and binary_macros crates expose similar functionality as procedural macros. Because of this, macros cannot be used in no-std environments, while this approach can.

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in const-decoder by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

No runtime deps