Skip to content

Commit

Permalink
Fix hex representation of KeyIds
Browse files Browse the repository at this point in the history
  • Loading branch information
sosthene-nitrokey committed Dec 1, 2023
1 parent 4c9ca40 commit b885afb
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,7 @@ impl Eq for Id {}

impl core::fmt::Debug for Id {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "Id(")?;
for ch in &self.hex() {
write!(f, "{}", &(*ch as char))?;
}
write!(f, ")")
write!(f, "Id({})", &self.hex_clean())
}
}

Expand Down Expand Up @@ -78,13 +74,28 @@ impl Id {

for i in 0..array.len() {
if array[i] == 0 && i != (array.len() - 1) {
// Skip leading zeros.
// This actually skips all zeroes, not only leading ones
// This is kept for backward compatibility with already serialized KeyIds
continue;
}

buffer.push(HEX_CHARS[(array[i] >> 4) as usize]).unwrap();
buffer.push(HEX_CHARS[(array[i] & 0xf) as usize]).unwrap();
}
buffer
}

/// skips leading zeros
pub fn hex_clean(&self) -> String<32> {
const HEX_CHARS: &[u8] = b"0123456789abcdef";
let mut buffer = String::new();
let array = self.0.to_be_bytes();

// skip leading zeros
for v in array.into_iter().skip_while(|v| *v == 0) {
buffer.push(HEX_CHARS[(v >> 4) as usize] as char).unwrap();
buffer.push(HEX_CHARS[(v & 0xf) as usize] as char).unwrap();
}

buffer
}
Expand Down

0 comments on commit b885afb

Please sign in to comment.