Skip to main content

Crate bouncycastle_hex

Crate bouncycastle_hex 

Source
Expand description

Good old fashioned hex encoder and decoder.

This one is implemented using constant-time operations in the conversions from Strings to byte values, so it is safe to use on cryptographic secret values.

It should just work the way you expect: encode takes any bytes-like rust type and returns a String, decode takes a String (which can be in any bytes-like container) and returns a Vec<u8>.

use bouncycastle_hex as hex;

let out = hex::encode(b"\x00\x01\x02\x03"); // "00010203"
let out = hex::encode(&[0x00, 0x01, 0x02, 0x03]); // "00010203"
let out = hex::encode(vec![0x00, 0x01, 0x02, 0x03]); // "00010203"

let out = hex::decode("00010203").unwrap(); // [0x00, 0x01, 0x02, 0x03]
let out = hex::decode(b"00010203").unwrap(); // [0x00, 0x01, 0x02, 0x03]

The decoder ignores whitespace and “\x”.

Enums§

HexError

Functions§

decode
One-shot decode from a hex string to a bytes using a constant-time implementation. ignores whitespace and \x
decode_out
expects an output array which is at least input.len() / 2 in size. Returns the number of bytes written.
encode
One-shot encode from bytes to a hex-encoded string using a constant-time implementation.
encode_out
expects an output array which is at least input.len() / 2 in size. Returns the number of bytes written.