Skip to main content

Crate hex

Crate hex 

Source
Expand description

Fast hex encoding and decoding with SIMD acceleration, constant-time operations, and const fn support.

Encoding supports lowercase (0-9a-f) and uppercase (0-9A-F) alphabets via Alphabet. Decoding is case-insensitive.

§Feature flags

FlagDescription
stdstd::error::Error trait impls (enabled by default)
allocString/Vec-returning convenience APIs
serdeSerde serialize/deserialize helpers

§Performance

The encode_into and decode_into functions automatically dispatch to SIMD-accelerated paths (AVX2 on x86/x86_64, NEON on aarch64). When a constant-time guarantee is required, use encode_into_constant_time or decode_into_constant_time.

§const fn support

encode_array and decode_array are const fn, enabling hex encoding and decoding at compile time.

§Examples

let encoded = hex::encode(b"hello");
assert_eq!(encoded, "68656c6c6f");

let decoded = hex::decode(b"68656c6c6f").unwrap();
assert_eq!(decoded, b"hello");

let upper = hex::encode_with_alphabet(b"hello", hex::Alphabet::Upper);
assert_eq!(upper, "68656C6C6F");

Enums§

Alphabet
The hex character set used for encoding.
DecodeError
Errors that can occur during hex decoding.
EncodeError
Errors that can occur during hex encoding.

Functions§

decodealloc
Decode Decodes a hex string into bytes.
decode_array
Decodes a hex string into a fixed-size array at compile time.
decode_into
Decodes a hex string into an existing buffer.
decode_into_constant_time
Constant-time hex decoding. Processes all input data without secret-dependent branches or memory accesses, making it suitable for cryptographic applications.
encodealloc
Encodes bytes to a lowercase hex string.
encode_array
Encodes data into a fixed-size array at compile time.
encode_into
Encodes bytes into an existing buffer.
encode_into_constant_time
Constant-time hex encoding. Processes all input data without secret-dependent branches or memory accesses, making it suitable for cryptographic applications.
encode_into_stringalloc
Appends the hex-encoded representation of data to a String.
encode_with_alphabetalloc
Encodes bytes to a hex string using the given Alphabet.