pub struct Hex;
Expand description
Hexadecimal encoder and decoder implementation.
Provides constant-time encoding and decoding of binary data to and from hexadecimal representation. The implementation uses only lowercase hexadecimal characters (0-9, a-f) for encoding.
§Security
All operations run in constant time relative to input length, making this implementation suitable for handling sensitive cryptographic data.
§Examples
use ct_codecs::{Hex, Encoder, Decoder};
fn example() -> Result<(), ct_codecs::Error> {
let data = b"Hello";
// Encode binary data to hex
let encoded = Hex::encode_to_string(data)?;
assert_eq!(encoded, "48656c6c6f");
// Decode hex back to binary
let decoded = Hex::decode_to_vec(&encoded, None)?;
assert_eq!(decoded, data);
// Working with preallocated buffers (useful for no_std)
let mut hex_buf = [0u8; 10];
let hex = Hex::encode(&mut hex_buf, data)?;
assert_eq!(hex, b"48656c6c6f");
let mut bin_buf = [0u8; 5];
let bin = Hex::decode(&mut bin_buf, hex, None)?;
assert_eq!(bin, data);
Ok(())
}
Trait Implementations§
Source§impl Decoder for Hex
impl Decoder for Hex
Source§fn decode<'t, IN: AsRef<[u8]>>(
bin: &'t mut [u8],
hex: IN,
ignore: Option<&[u8]>,
) -> Result<&'t [u8], Error>
fn decode<'t, IN: AsRef<[u8]>>( bin: &'t mut [u8], hex: IN, ignore: Option<&[u8]>, ) -> Result<&'t [u8], Error>
Decodes hexadecimal data back into its binary representation.
The decoding is performed in constant time relative to the input length. Both uppercase and lowercase hexadecimal characters are accepted.
§Arguments
bin
- Mutable buffer to store the decoded outputhex
- Hexadecimal input data to decodeignore
- Optional set of characters to ignore during decoding
§Returns
Ok(&[u8])
- A slice of the binary buffer containing the decoded dataErr(Error::Overflow)
- If the output buffer is too smallErr(Error::InvalidInput)
- If the input contains invalid characters or has odd length
Source§impl Encoder for Hex
impl Encoder for Hex
Source§fn encoded_len(bin_len: usize) -> Result<usize, Error>
fn encoded_len(bin_len: usize) -> Result<usize, Error>
Calculates the encoded length for a hexadecimal representation.
The encoded length is always twice the binary length, as each byte is represented by two hexadecimal characters.
§Arguments
bin_len
- The length of the binary input in bytes
§Returns
Ok(usize)
- The required length for the encoded outputErr(Error::Overflow)
- If the calculation would overflow
Source§fn encode<IN: AsRef<[u8]>>(hex: &mut [u8], bin: IN) -> Result<&[u8], Error>
fn encode<IN: AsRef<[u8]>>(hex: &mut [u8], bin: IN) -> Result<&[u8], Error>
Encodes binary data into a hexadecimal representation.
The encoding is performed in constant time relative to the input length.
§Arguments
hex
- Mutable buffer to store the encoded outputbin
- Binary input data to encode
§Returns
Ok(&[u8])
- A slice of the encoded buffer containing the hex dataErr(Error::Overflow)
- If the output buffer is too small