1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//! # Single byte XOR against string.
//! `Sbxor` takes a string argument that has been encoded with a single byte
//! XOR.  Following that it decodes it, and determines if it contains English
//! words.
//!
//!
extern crate hex;
extern crate words;

use hex::{decode, encode};
use words::*;


pub struct Sbxor {
    pub encoded_str: String,
    pub decoded_strs: Vec<String>, 
    pub is_english: bool,
    pub encoding_byte: Option<u8> 
}

impl Sbxor { 
    /// Instantiate a Sbxor object
    ///
    /// # Examples
    ///
    /// ```
    /// use sbxor::*;
    /// let sbxor = Sbxor::new();
    ///
    /// assert_eq!(sbxor.is_english, false);
    /// ```
    #[allow(unused)]
    pub fn new() -> Sbxor {
        Sbxor {
            encoded_str: String::new(),
            decoded_strs: vec![],
            is_english: false,
            encoding_byte: None 
        }
    }

    /// Populates the Sbxor object's encoded_str field
    ///
    /// # Examples
    ///
    /// ```
    /// use sbxor::*;
    ///
    /// let encoded_str =
    /// "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736".to_string();
    ///
    /// let sbxor = Sbxor::create(encoded_str);
    /// 
    /// assert_ne!(sbxor.encoded_str.len(), 0);
    /// ```
    pub fn create(encoded_str: String) -> Sbxor {
        Sbxor {
            encoded_str: encoded_str,
            decoded_strs: vec![], 
            is_english: false,
            encoding_byte: None
        }
    }

    /// Populate the decode field of the Sbxor object.
    ///
    /// # Examples
    /// ```
    /// use sbxor::*;
    ///
    /// let encoded_str = 
    /// "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736".to_string();
    /// 
    /// let mut sbxor = Sbxor::create(encoded_str);
    /// sbxor.decode_sbxor();
    /// assert_ne!(sbxor.decoded_strs.len(), 0);
    /// ```
    pub fn decode_sbxor(&mut self) {
        let mut wordy = Words::new();
        wordy.populate();
        for i in 0..255 {
            let vec = decode(&self.encoded_str).unwrap();
            // double check if encoded string has been consumed
            let vec: Vec<u8> = vec.into_iter().map(|x| x ^ i).collect();
            let chrs_after_xor: Vec<char> = vec.into_iter().map(|x| x as char).collect();
            let mut soln: Vec<char> = vec![];
            for chr in chrs_after_xor {
                if (chr.is_ascii() && chr.is_ascii_alphanumeric()) || chr == ' ' {
                    soln.push(chr.to_lowercase().to_string().chars().next().unwrap());
                } 
            }
            let s: String = soln.into_iter().collect();
            if is_english(&s, &wordy) {
                self.is_english = true;
                self.encoding_byte = Some(i);
                
            }
            self.decoded_strs.push(s);
        }
    }
    
    // TODO: flush out this doctest
    pub fn decode_sbxor_char(&mut self, encoding_byte: u8) {
        let mut wordy = Words::new();
        wordy.populate();
        let vec = decode(&self.encoded_str).unwrap();
        let vec: Vec<u8> = vec.into_iter().map(|x| x ^ encoding_byte).collect();
        let chrs_after_xor: Vec<char> = vec.into_iter().map(|x| x as char).collect();
        let mut soln: Vec<char> = vec![];
        for chr in chrs_after_xor {
            if (chr.is_ascii() && chr.is_ascii_alphanumeric()) || chr == ' ' {
                soln.push(chr.to_lowercase().to_string().chars().next().unwrap());
            }
        }
        let s: String = soln.into_iter().collect();
        self.decoded_strs.push(s);
    }

    pub fn encode_sbxor_phrase(&mut self, encoding_phrase: String, input_phrase: String) {
        // add input_phrase to self
        let input_phrase_tmp = input_phrase.clone();
        self.decoded_strs.push(input_phrase_tmp);

        // convert to u8
        let mut input_phrase = input_phrase.into_bytes();
        let mut encoding_phrase = encoding_phrase.into_bytes();

        // encoding logic
        let mut enc_posn = 0;
        let mut inp_posn = 0;
        while input_phrase.len() > inp_posn {
            let encoding_char;
            if encoding_phrase.len() <= enc_posn {
                // then we'll reset the encoding phrase to repeat
                enc_posn = 0;
            }
            encoding_char = &mut encoding_phrase[enc_posn] as *mut u8;
            let r = &mut input_phrase[inp_posn] as *mut u8;

            unsafe {
                *r = *r ^ (*encoding_char as u8);
            }

            inp_posn += 1;
            enc_posn += 1;
        }

        // add encoded phrase to self
        let input_phrase = encode(input_phrase);
        self.encoded_str = input_phrase;
    }
}
        

/// Checks if the decoded string is English
///
/// # Examples
/// ```
/// extern crate words;
/// extern crate sbxor;
/// use sbxor::*;
/// use words::*;
/// let encoded_str = 
/// "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736".to_string();
/// let mut wordy = Words::new();
/// wordy.populate();
/// let mut sbxor = Sbxor::create(encoded_str);
/// sbxor.decode_sbxor();
/// while sbxor.decoded_strs.len() > 0 {
///     if is_english(&sbxor.decoded_strs.pop().unwrap(), &wordy) {
///         sbxor.is_english = true;
///         break;
///     }
/// } 
/// assert!(sbxor.is_english);
/// ```   
pub fn is_english(s: &String, wordy: &Words) -> bool{
    let s: &str = &*s;//.as_ref();
    let iter = s.split_whitespace();
    for word in iter {
        if word.len() > 3 { // horrid heuristic 
            if wordy.exists(word.to_string()) {
                return true;
            }
        }
    }
    
    false
}