Skip to content

Repository files navigation

faster-hex

Crates.io docs.rs

Fast, checked hex encoding and decoding with SIMD on x86 and AArch64, a portable scalar fallback, and no_std support. Requires Rust 1.95 or later.

Usage

[dependencies]
faster-hex = "2.1"
use faster_hex::{hex_decode, hex_encode};

fn main() -> Result<(), faster_hex::Error> {
    let mut encoded = [0; 10];
    let text = hex_encode(b"hello", &mut encoded)?;
    assert_eq!(text, "68656c6c6f");

    let mut decoded = [0; 5];
    assert_eq!(hex_decode(text.as_bytes(), &mut decoded)?, b"hello");
    Ok(())
}

Both calls return the written slice and preserve the buffer on error. hex_decode accepts mixed-case ASCII hex and reports the first invalid digit's byte offset. Use hex_validate to check hex pairs without an output buffer, or hex_decode_append (with alloc) to extend an existing Vec<u8>.

Hash prefixes

let view = Hex::new(bytes).leading_digits(n) selects up to n hex digits. Use the repository's abbreviation length for a Git object ID, or choose a width for a log. Zero selects an empty prefix. A count beyond the end selects the whole input.

view.encode_to(dst) writes one lowercase ASCII byte per selected digit. For deadbeef1234, seven digits use four input bytes and write deadbee. All twelve digits use six input bytes. Use write!(out, "{view:#X}") for formatted output or hex_encode for a complete value in a byte buffer.

Timings across prefix lengths are in the 2.1 benchmarks for SHA-1 and SHA-256.

Features

std and serde are enabled by default. Set default-features = false for a dependency-free no_std build. Enable alloc for owned strings and vectors, heapless-09 for fixed-capacity strings, or defmt for defmt 1.x logging.

Security

faster-hex is a throughput-oriented codec, not a constant-time implementation, and it does not zeroize temporary stack or spare heap buffers. Do not use it on secrets where timing side channels or memory residue matter, and enforce raw input size limits at the transport or parser boundary before calling allocating decoders (hex_decode_vec, hex_decode_append, or Serde adapters; deserialize_bounded only caps decoded output length) on untrusted data. See SECURITY.md for reporting vulnerabilities.

API documentation · Changelog · Migration guide · Security

License

MIT. See the third-party notices.

Releases

Packages

Used by

Contributors

Languages