cipher_utils/
lib.rs

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
pub mod alphabet;
pub mod character_set;
pub mod cipher_type;
pub mod score;

/// The `frequency` module, providing various utilities relating to frequency analysis.
pub mod frequency;

use alphabet::Alphabet;

pub trait Analyze {
    fn index_of_coincidence(&self) -> f64;

    /// Alias for `index_of_coincidence()`.
    fn ioc(&self) -> f64 {
        self.index_of_coincidence()
    }

    /// Returns an `Alphabet` containing the unique characters of this string in-order.
    fn alphabet(&self) -> Alphabet;
}

impl<T: AsRef<str>> Analyze for T {
    fn index_of_coincidence(&self) -> f64 {
        let mut frequency = [0u32; 26];
        let mut total_letters = 0;

        for c in self.as_ref().chars() {
            if c.is_alphabetic() {
                let idx = c.to_ascii_lowercase() as usize - 'a' as usize;
                frequency[idx] += 1;
                total_letters += 1;
            }
        }

        if total_letters < 2 {
            return 0.0;
        }

        let mut numerator = 0u32;

        for &count in &frequency {
            numerator += count * (count - 1);
        }

        let denominator = total_letters * (total_letters - 1);

        numerator as f64 / denominator as f64
    }

    fn alphabet(&self) -> Alphabet {
        Alphabet::of_cased(self.as_ref())
    }
}