From 481735d9df0281729145786f94d475e1af0e5fd8 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Tue, 14 Nov 2023 21:55:29 +0200 Subject: [PATCH] Add the function that return all key combinations for a possible key vector. --- .../analyzers/simple_substitution.clj | 6 ++++++ .../analyzers/simple_substitution_test.clj | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/cipher_analytical_machine/analyzers/simple_substitution.clj b/src/cipher_analytical_machine/analyzers/simple_substitution.clj index 59ad06b..717b49b 100644 --- a/src/cipher_analytical_machine/analyzers/simple_substitution.clj +++ b/src/cipher_analytical_machine/analyzers/simple_substitution.clj @@ -36,3 +36,9 @@ (->> (apply comb/cartesian-product possible-key-vector) (map cs/join))) +(defn get-possible-key-combinations + "Return possible keys for a possible key vector. For example, if you have a map: {\\a [\\a \\b \\c] \\e [\\f \\g] \\c [\\a \\c]}, then the keys are (\"abcefg\" \"abcegf\" ...)." + [possible-key-vector] + (->> (map get-all-permutation-for-block possible-key-vector) + (get-all-combinations-for-blocks))) + diff --git a/test/cipher_analytical_machine/analyzers/simple_substitution_test.clj b/test/cipher_analytical_machine/analyzers/simple_substitution_test.clj index fc80894..c0f2c09 100644 --- a/test/cipher_analytical_machine/analyzers/simple_substitution_test.clj +++ b/test/cipher_analytical_machine/analyzers/simple_substitution_test.clj @@ -35,3 +35,16 @@ [[\a \b] [\a \c]] '("aa" "ac" "ba" "bc") [[\a \b \c] [\a \c] [\b \c]] '("aab" "aac" "acb" "acc" "bab" "bac" "bcb" "bcc" "cab" "cac" "ccb" "ccc")))) +(deftest get-possible-key-combinations-test + (testing "The keys must be the all combinations, but the blocks must create a permutations which will be joined as a combination" + (are [possible-keys-vector expected-keys] + (= expected-keys (get-possible-key-combinations possible-keys-vector)) + [[\a \b] [\f] [\e \c] [\g]] '("abfecg" "abfceg" "bafecg" "bafceg") + [[\a \b] [\a \c]] '("abac" "abca" "baac" "baca") + [[\a \b \c] [\a \c] [\b \c]] '("abcacbc" "abcaccb" "abccabc" "abccacb" + "acbacbc" "acbaccb" "acbcabc" "acbcacb" + "bacacbc" "bacaccb" "baccabc" "baccacb" + "bcaacbc" "bcaaccb" "bcacabc" "bcacacb" + "cabacbc" "cabaccb" "cabcabc" "cabcacb" + "cbaacbc" "cbaaccb" "cbacabc" "cbacacb")))) +