use crate::SecpError;
pub fn hex_encode(bytes: &[u8]) -> String {
const HEX: &[u8; 16] = b"0123456789abcdef";
let mut s = String::with_capacity(bytes.len() * 2);
for &b in bytes {
s.push(HEX[(b >> 4) as usize] as char);
s.push(HEX[(b & 0x0f) as usize] as char);
}
s
}
pub fn hex_decode(hex: &str) -> Result<Vec<u8>, SecpError> {
let hex = hex.as_bytes();
if hex.len() % 2 != 0 {
return Err(SecpError::InvalidHex("odd length"));
}
let mut out = Vec::with_capacity(hex.len() / 2);
for chunk in hex.chunks(2) {
let high = hex_nibble(chunk[0])?;
let low = hex_nibble(chunk[1])?;
out.push((high << 4) | low);
}
Ok(out)
}
fn hex_nibble(b: u8) -> Result<u8, SecpError> {
match b {
b'0'..=b'9' => Ok(b - b'0'),
b'a'..=b'f' => Ok(b - b'a' + 10),
b'A'..=b'F' => Ok(b - b'A' + 10),
_ => Err(SecpError::InvalidHex("invalid character")),
}
}