From c46803a4557c9cd14df5e46192384a831b329179 Mon Sep 17 00:00:00 2001 From: Daniel Mueller Date: Sun, 13 Oct 2019 04:51:29 -0700 Subject: Correctly pad bytes with zero in hex conversion When reading a secret in ascii or base32 format from the user, we perform a conversion of the potentially decoded string into hexadecimal bytes, because that is what libnitrokey expects. The format string we used in the conversion, however, did not account for padding with a leading zero for single digit results. E.g., the newline/line feed symbol '\n', which has a decimal value of 10 would result in the string 'a' being produced, whereas '0a' would be the correct result. This change corrects the format string to fix this problem. --- nitrocli/src/commands.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'nitrocli/src/commands.rs') diff --git a/nitrocli/src/commands.rs b/nitrocli/src/commands.rs index b5fc282..eac8549 100644 --- a/nitrocli/src/commands.rs +++ b/nitrocli/src/commands.rs @@ -616,7 +616,7 @@ pub fn otp_get( fn format_bytes(bytes: &[u8]) -> String { bytes .iter() - .map(|c| format!("{:x}", c)) + .map(|c| format!("{:02x}", c)) .collect::>() .join("") } @@ -956,4 +956,11 @@ mod tests { let result = prepare_ascii_secret("Österreich"); assert!(result.is_err()); } + + #[test] + fn hex_string() { + assert_eq!(format_bytes(&[b' ']), "20"); + assert_eq!(format_bytes(&[b' ', b' ']), "2020"); + assert_eq!(format_bytes(&[b'\n', b'\n']), "0a0a"); + } } -- cgit v1.2.1