Struct Hex

Source
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

Source§

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 output
  • hex - Hexadecimal input data to decode
  • ignore - Optional set of characters to ignore during decoding
§Returns
  • Ok(&[u8]) - A slice of the binary buffer containing the decoded data
  • Err(Error::Overflow) - If the output buffer is too small
  • Err(Error::InvalidInput) - If the input contains invalid characters or has odd length
Source§

fn decode_to_vec<IN: AsRef<[u8]>>( encoded: IN, ignore: Option<&[u8]>, ) -> Result<Vec<u8>, Error>

Decodes text data and returns the result as a Vec. Read more
Source§

impl Encoder for Hex

Source§

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 output
  • Err(Error::Overflow) - If the calculation would overflow
Source§

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 output
  • bin - Binary input data to encode
§Returns
  • Ok(&[u8]) - A slice of the encoded buffer containing the hex data
  • Err(Error::Overflow) - If the output buffer is too small
Source§

fn encode_to_str<IN: AsRef<[u8]>>( encoded: &mut [u8], bin: IN, ) -> Result<&str, Error>

Encodes binary data and returns the result as a string slice. Read more
Source§

fn encode_to_string<IN: AsRef<[u8]>>(bin: IN) -> Result<String, Error>

Encodes binary data and returns the result as a String. Read more

Auto Trait Implementations§

§

impl Freeze for Hex

§

impl RefUnwindSafe for Hex

§

impl Send for Hex

§

impl Sync for Hex

§

impl Unpin for Hex

§

impl UnwindSafe for Hex

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.