Browse Source

Updated to xor_bytes function.

Added notes on conversions URL.
Accept &str instead of String.
Steve Thielemann 1 year ago
parent
commit
d6e04ee5df
1 changed files with 27 additions and 23 deletions
  1. 27 23
      src/lib.rs

+ 27 - 23
src/lib.rs

@@ -1,21 +1,25 @@
 use base64::{Engine as _, engine::general_purpose};
 use std::error::Error;
 
-pub fn encode(text: &String) -> String {
-    let mut temp = Vec::with_capacity(text.len());
-    for ch in text.bytes() {
-        temp.push( ch ^ 'U' as u8 );
+fn xor_bytes(buffer: &mut Vec<u8>, xor: u8) {
+    for b in buffer {
+        *b = *b ^ xor
     }
+}
+
+// See:  https://stackoverflow.com/questions/41034635/how-do-i-convert-between-string-str-vecu8-and-u8
+// for conversion examples (to/from str/String/Vec<u8>).
+
+pub fn encode(text: &str, xor: u8) -> String {
+    let mut temp = text.as_bytes().to_owned();
+    xor_bytes(temp.as_mut(), xor);
     general_purpose::STANDARD.encode(temp)
 }
 
-pub fn decode(text: &String) -> Result<String, Box<dyn Error>> {
-    let chars = general_purpose::STANDARD.decode(text)?;
-    let mut temp = Vec::with_capacity(chars.len());
-    for ch in chars {
-        temp.push( ch ^ 'U' as u8 );
-    }
-    Ok(String::from_utf8(temp)?)
+pub fn decode(text: &str, xor: u8) -> Result<String, Box<dyn Error>> {
+    let mut chars = general_purpose::STANDARD.decode(text)?;
+    xor_bytes(chars.as_mut(), xor);
+    Ok(String::from_utf8(chars)?)
 }
 
 #[cfg(test)]
@@ -24,22 +28,22 @@ mod tests {
 
     #[test]
     fn encode_test() {
-        let t1: String = String::from("hello world");
-        let t2 = encode(&t1);
+        let t1 = "hello world";
+        let t2 = encode(t1, 'U' as u8);
         assert_eq!(t2, "PTA5OTp1IjonOTE=");
     }
 
     #[test]
     fn encode_utf8_test() {
-        let t1: String = String::from("🍺");
-        let t2 = encode(&t1);
+        let t1 = "🍺";
+        let t2 = encode(&t1, 'U' as u8);
         assert_eq!(t2, "pcrY7w==");
     }
 
     #[test]
     fn decode_test() {
-        let t1: String = String::from("ICYiPCE2PW9vMTA2OjEw");
-        let t2 = decode(&t1);
+        let t1= "ICYiPCE2PW9vMTA2OjEw";
+        let t2 = decode(&t1, 'U' as u8);
         match t2 {
             Ok(s) => assert_eq!(s, "uswitch::decode"),
             Err(err) => panic!("{}", err),
@@ -48,8 +52,8 @@ mod tests {
 
     #[test]
     fn decode_utf8_test() {
-        let t1: String = String::from("t9f5");
-        let t2 = decode(&t1);
+        let t1 = "t9f5";
+        let t2 = decode(&t1, 'U' as u8);
         match t2 {
             Ok(s) => assert_eq!(s, "€"),
             Err(err) => panic!("{}", err),
@@ -58,8 +62,8 @@ mod tests {
 
     #[test]
     fn decode_fail_test() {
-        let t1: String = String::from("yAyL3WlysPBdTEnPDs+JnWlytjv"); // Invalid base64
-        let t2 = decode(&t1);
+        let t1 = "yAyL3WlysPBdTEnPDs+JnWlytjv"; // Invalid base64
+        let t2 = decode(&t1, 'U' as u8);
         match t2 {
             Ok(s) => panic!("Expected failure! Got '{}'", s),
             Err(_) => ()
@@ -68,8 +72,8 @@ mod tests {
 
     #[test]
     fn decode_fail_utf8_test() {
-        let t1: String = String::from("woXChQ=="); // Invalid UTF-8
-        let t2 = decode(&t1);
+        let t1 = "woXChQ=="; // Invalid UTF-8
+        let t2 = decode(&t1, 'U' as u8);
         match t2 {
             Ok(s) => panic!("Expected failure! Got '{}'", s),
             Err(_) => ()