https://github.com/slowli/const-decoder
Constant functions for converting hex- and base64-encoded strings into bytes
https://github.com/slowli/const-decoder
base64 const-evaluation encoding hex
Last synced: 4 months ago
JSON representation
Constant functions for converting hex- and base64-encoded strings into bytes
- Host: GitHub
- URL: https://github.com/slowli/const-decoder
- Owner: slowli
- License: apache-2.0
- Created: 2021-04-16T13:16:48.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2025-02-24T03:21:29.000Z (5 months ago)
- Last Synced: 2025-03-01T12:25:28.264Z (5 months ago)
- Topics: base64, const-evaluation, encoding, hex
- Language: Rust
- Homepage:
- Size: 1.11 MB
- Stars: 6
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# Constant Functions for Hex / Base64 Decoding
[](https://github.com/slowli/const-decoder/actions)
[](https://github.com/slowli/const-decoder#license)
**Documentation:** [](https://docs.rs/const-decoder/)
[](https://slowli.github.io/const-decoder/const_decoder/)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`:
```toml
[dependencies]
const-decoder = "0.4.0"
```Example of usage:
```rust
use const_decoder::Decoder;
// An Ed25519 secret key.
const SECRET_KEY: [u8; 64] = Decoder::Hex.decode(
b"9e55d1e1aa1f455b8baad9fdf975503655f8b359d542fa7e4ce84106d625b352\
06fac1f22240cffd637ead6647188429fafda9c9cb7eae43386ac17f61115075",
);
// Alternatively, you can use `decode!` macro:
const PUBLIC_KEY: &[u8] = &const_decoder::decode!(
Decoder::Hex,
b"06fac1f22240cffd637ead6647188429fafda9c9cb7eae43386ac17f61115075",
);
```[Bech32] encoding:
```rust
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](LICENSE-APACHE)
or [MIT license](LICENSE-MIT) 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.[Bech32]: https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki
[`binary_macros`]: https://crates.io/crates/binary_macros
[`hex-literal`]: https://crates.io/crates/hex_literal