From 99fababf0baf4ce444c63c9f81ddc43aa51974a4 Mon Sep 17 00:00:00 2001 From: Alexander Krotov Date: Sun, 27 Oct 2019 02:05:06 +0300 Subject: [PATCH] to_base64: operate on characters instead of bytes to avoid unsafe code --- src/key.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/key.rs b/src/key.rs index a643aa391..12f10793a 100644 --- a/src/key.rs +++ b/src/key.rs @@ -180,15 +180,15 @@ impl Key { let encoded = base64::encode(&buf); encoded - .as_bytes() - .chunks(break_every) - .fold(String::new(), |mut res, buf| { - // safe because we are using a base64 encoded string - res += unsafe { std::str::from_utf8_unchecked(buf) }; - res += " "; + .chars() + .enumerate() + .fold(String::new(), |mut res, (i, c)| { + if i > 0 && i % break_every == 0 { + res.push(' ') + } + res.push(c); res }) - .trim() .to_string() }