diff --git a/src/cipher_analytical_machine/analyzers/simple_substitution.clj b/src/cipher_analytical_machine/analyzers/simple_substitution.clj index 2c03881..3742489 100644 --- a/src/cipher_analytical_machine/analyzers/simple_substitution.clj +++ b/src/cipher_analytical_machine/analyzers/simple_substitution.clj @@ -23,6 +23,13 @@ (map second) (into []))) +(defn get-all-permutation-for-block + "Return a vector for all possible blocks that can be made from a vector of symbols." + [symbols] + (->> (comb/permutations symbols) + (map cs/join) + (into []))) + (defn get-possible-keys "Return possible keys for a possible key vector. For example, if you have a map: {\\a [\\a \\b \\c] \\b [\\b \\c] \\c [\\a \\c]}, then the vector is [[\\a \\b \\c] [\\b \\c] [\\a \\c]] and keys (\"aba\" \"abc\" ...)." [possible-key-vector] diff --git a/test/cipher_analytical_machine/analyzers/simple_substitution_test.clj b/test/cipher_analytical_machine/analyzers/simple_substitution_test.clj index 5b77935..9ae5566 100644 --- a/test/cipher_analytical_machine/analyzers/simple_substitution_test.clj +++ b/test/cipher_analytical_machine/analyzers/simple_substitution_test.clj @@ -14,12 +14,19 @@ {\a [\a \b] \c [\b \a] \b [\a \c]} "abc" [[\a \b] [\a \c] [\b \a]]))) (deftest get-possible-key-vector-from-reversed-count-map-test - (testing "" + (testing "The block order must be in descending order" (are [reversed-count-map expected-key-vector] (= expected-key-vector (get-possible-key-vector-from-reversed-count-map reversed-count-map)) {1 [\a \c] 2 [\b]} [[\b] [\a \c]] {2 [\a] 1 [\b] 3 [\c]} [[\c] [\a] [\b]]))) +(deftest get-all-permutation-for-block-test + (testing "It must return all permutations" + (are [symbols expected-permutations] + (= expected-permutations (get-all-permutation-for-block symbols)) + [\a \b] ["ab" "ba"] + [\a \b \c] ["abc" "acb" "bac" "bca" "cab" "cba"]))) + (deftest get-possible-keys-test (testing "The keys must be the all combinations" (are [possible-keys-vector expected-keys]