cipher_utils/
cipher_type.rs

1use crate::{
2    character_set::{self, CharacterSet},
3    Analyze,
4};
5
6pub enum CipherType {
7    Transposition,
8    Substitution,
9    Base64,
10    Morse,
11    Hex,
12    Octal,
13}
14
15impl CipherType {
16    pub fn best_match(ciphertext: &str) -> Option<Self> {
17        let characters = CharacterSet::raw(ciphertext);
18
19        if characters == *character_set::MORSE {
20            return Some(Self::Morse);
21        }
22
23        if characters == *character_set::BASE_64 {
24            return Some(Self::Base64);
25        }
26
27        let characters = CharacterSet::of(ciphertext);
28
29        if characters == *character_set::OCTAL {
30            return Some(Self::Octal);
31        }
32
33        if characters == *character_set::HEX {
34            return Some(Self::Hex);
35        }
36
37        if characters.is_alphabetic() {
38            if (0.6..0.75).contains(&ciphertext.index_of_coincidence()) {
39                return Some(Self::Transposition);
40            } else {
41                return Some(Self::Substitution);
42            }
43        }
44
45        None
46    }
47}