diff --git a/src/secret.rs b/src/secret.rs index f13d91ae9..c30399a21 100644 --- a/src/secret.rs +++ b/src/secret.rs @@ -46,7 +46,31 @@ macro_rules! impl_display_secret { engine.input(&self.secret_bytes()); let hash = sha256::Hash::from_engine(engine); - f.debug_tuple(stringify!($thing)).field(&format_args!("#{:016x}", hash)).finish() + // hex-conservative does not support truncation and this is the correct behaviour if + // a hash is considered integral type. So we truncate manually. + // + // > Precision + // > ... + // > For integral types, this is ignored. + // + // ref: https://doc.rust-lang.org/std/fmt/ + + struct Id([u8; 8]); + + impl ::core::fmt::Display for Id { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + for byte in self.0.iter() { + write!(f, "{:02x}", byte)?; + } + Ok(()) + } + } + + let mut id = [0_u8; 8]; + id.copy_from_slice(&hash[0..8]); + let id = Id(id); + + f.debug_tuple(stringify!($thing)).field(&format_args!("#{}", id)).finish() } }