Crate base16ct

source ·
Expand description

Pure Rust implementation of Base16 (RFC 4648, a.k.a. hex).

Implements lower and upper case Base16 variants without data-dependent branches or lookup tables, thereby providing portable “best effort” constant-time operation. Not constant-time with respect to message length (only data).

Supports no_std environments and avoids heap allocations in the core API (but also provides optional alloc support for convenience).

Based on code from: https://github.com/Sc00bz/ConstTimeEncoding/blob/master/hex.cpp

§Examples

let lower_hex_str = "abcd1234";
let upper_hex_str = "ABCD1234";
let mixed_hex_str = "abCD1234";
let raw = b"\xab\xcd\x12\x34";

let mut buf = [0u8; 16];
// length of return slice can be different from the input buffer!
let res = base16ct::lower::decode(lower_hex_str, &mut buf).unwrap();
assert_eq!(res, raw);
let res = base16ct::lower::encode(raw, &mut buf).unwrap();
assert_eq!(res, lower_hex_str.as_bytes());
// you also can use `encode_str` and `encode_string` to get
// `&str` and `String` respectively
let res: &str = base16ct::lower::encode_str(raw, &mut buf).unwrap();
assert_eq!(res, lower_hex_str);

let res = base16ct::upper::decode(upper_hex_str, &mut buf).unwrap();
assert_eq!(res, raw);
let res = base16ct::upper::encode(raw, &mut buf).unwrap();
assert_eq!(res, upper_hex_str.as_bytes());

// In cases when you don't know if input contains upper or lower
// hex-encoded value, then use functions from the `mixed` module
let res = base16ct::mixed::decode(lower_hex_str, &mut buf).unwrap();
assert_eq!(res, raw);
let res = base16ct::mixed::decode(upper_hex_str, &mut buf).unwrap();
assert_eq!(res, raw);
let res = base16ct::mixed::decode(mixed_hex_str, &mut buf).unwrap();
assert_eq!(res, raw);

Modules§

  • Function for decoding and encoding lower Base16 (hex)
  • Function for decoding mixed Base16 (hex)
  • Function for decoding and encoding upper Base16 (hex)

Structs§

  • core::fmt presenter for binary data encoded as hexadecimal (Base16).

Enums§

Functions§

  • Compute decoded length of the given hex-encoded input.
  • Get the length of Base16 (hex) produced by encoding the given bytes.

Type Aliases§

  • Result type with the base16ct crate’s Error type.