项目作者: slowli

项目描述 :
Constant functions for converting hex- and base64-encoded strings into bytes
高级语言: Rust
项目地址: git://github.com/slowli/const-decoder.git
创建时间: 2021-04-16T13:16:48Z
项目社区:https://github.com/slowli/const-decoder

开源协议:Other

下载


Constant Functions for Hex / Base64 Decoding

Build Status
License: MIT OR Apache-2.0
rust 1.67+ 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:

  1. [dependencies]
  2. const-decoder = "0.4.0"

Example of usage:

  1. use const_decoder::Decoder;
  2. // An Ed25519 secret key.
  3. const SECRET_KEY: [u8; 64] = Decoder::Hex.decode(
  4. b"9e55d1e1aa1f455b8baad9fdf975503655f8b359d542fa7e4ce84106d625b352\
  5. 06fac1f22240cffd637ead6647188429fafda9c9cb7eae43386ac17f61115075",
  6. );
  7. // Alternatively, you can use `decode!` macro:
  8. const PUBLIC_KEY: &[u8] = &const_decoder::decode!(
  9. Decoder::Hex,
  10. b"06fac1f22240cffd637ead6647188429fafda9c9cb7eae43386ac17f61115075",
  11. );

Bech32 encoding:

  1. use const_decoder::{Decoder, Encoding};
  2. const BECH32: Decoder = Decoder::custom("qpzry9x8gf2tvdw0s3jn54khce6mua7l");
  3. // Sample address from the Bech32 spec excluding the `tb1q` prefix
  4. // and the checksum suffix.
  5. const SAMPLE_ADDR: [u8; 32] =
  6. 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.