From 371ec9b469f89a5ff2c5d22ac2638683df973fdf Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Thu, 22 Aug 2024 12:10:11 +1000 Subject: [PATCH] Test secret hash truncation When using `hashes` to hash a secret and print it in `Debug` we truncate the hash into an 8 byte identifier, test it. --- src/key.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/key.rs b/src/key.rs index 85482e05f..e66817306 100644 --- a/src/key.rs +++ b/src/key.rs @@ -1718,6 +1718,31 @@ mod test { "0100000000000000020000000000000003000000000000000400000000000000" ); } + #[test] + fn test_display_obfuscation() { + #[rustfmt::skip] + static SK_BYTES: [u8; 32] = [ + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, + 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, + ]; + + let sk = SecretKey::from_slice(&SK_BYTES).expect("sk"); + + #[cfg(feature = "std")] + { + let got = format!("{:?}", sk); + let want = "SecretKey(#2f4d520bd6874292)"; + assert_eq!(got, want); + } + #[cfg(all(not(feature = "std"), feature = "hashes"))] + { + let got = format!("{:?}", sk); + let want = "SecretKey(#de5a451f70110720)"; + assert_eq!(got, want); + } + } #[test] #[cfg(feature = "alloc")]