From 2cc5be57c848016c3654861f280d106276564811 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Sat, 11 Nov 2023 15:21:16 +0200 Subject: [PATCH] Add functions to get possible keys. --- project.clj | 3 ++- .../analyzers/simple_substitution.clj | 22 ++++++++++++++++++ .../analyzers/simple_substitution_test.clj | 23 +++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 src/cipher_analytical_machine/analyzers/simple_substitution.clj create mode 100644 test/cipher_analytical_machine/analyzers/simple_substitution_test.clj diff --git a/project.clj b/project.clj index a9445d9..1168895 100644 --- a/project.clj +++ b/project.clj @@ -7,7 +7,8 @@ [org.clojure/tools.cli "1.0.219"] [org.apache.tika/tika-core "1.28.5"] [org.apache.tika/tika-langdetect "1.28.5"] - [cheshire "5.12.0"]] + [cheshire "5.12.0"] + [org.clojure/math.combinatorics "0.2.0"]] :main ^:skip-aot cipher-analytical-machine.core :java-source-paths ["src/main/java"] :target-path "target/%s" diff --git a/src/cipher_analytical_machine/analyzers/simple_substitution.clj b/src/cipher_analytical_machine/analyzers/simple_substitution.clj new file mode 100644 index 0000000..4f1d16c --- /dev/null +++ b/src/cipher_analytical_machine/analyzers/simple_substitution.clj @@ -0,0 +1,22 @@ +(ns cipher-analytical-machine.analyzers.simple-substitution + (:require [clojure.string :as cs] + [clojure.math.combinatorics :as comb] + [cipher-analytical-machine.ciphers.simple-substitution :as ss] + [cipher-analytical-machine.analyzers.analyzers :as analyzers] + [cipher-analytical-machine.analyzers.caesar :as caesar]) + (:gen-class)) + +(defn get-possible-key-vector + "Conver a possible key map to a vector of possible keys. 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]]." + [possible-key-map symbols] + (reduce + (fn [acc el] + (conj acc (get possible-key-map el))) + [] symbols)) + +(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] + (->> (apply comb/cartesian-product possible-key-vector) + (map cs/join))) + diff --git a/test/cipher_analytical_machine/analyzers/simple_substitution_test.clj b/test/cipher_analytical_machine/analyzers/simple_substitution_test.clj new file mode 100644 index 0000000..f71ff9b --- /dev/null +++ b/test/cipher_analytical_machine/analyzers/simple_substitution_test.clj @@ -0,0 +1,23 @@ +(ns cipher-analytical-machine.analyzers.simple-substitution-test + (:require + [clojure.test :refer :all] + [cipher-analytical-machine.symbols.frequencies :as sf] + [cipher-analytical-machine.ciphers.simple-substitution :as ss] + [cipher-analytical-machine.analyzers.simple-substitution :refer :all]) + (:gen-class)) + +(deftest get-possible-keys-vector-test + (testing "The vector must be ordered by symbols" + (are [possible-keys-map symbols expected-key-vector] + (= expected-key-vector (get-possible-key-vector possible-keys-map symbols)) + {\a [\a \b] \b [\b \a] \c [\a \c]} "abc" [[\a \b] [\b \a] [\a \c]] + {\a [\a \b] \c [\b \a] \b [\a \c]} "abc" [[\a \b] [\a \c] [\b \a]]))) + +(deftest get-possible-keys-test + (testing "The keys must be the all combinations" + (are [possible-keys-vector expected-keys] + (= expected-keys (get-possible-keys possible-keys-vector)) + [[\a \b] [\f] [\a \c] [\g]] '("afag" "afcg" "bfag" "bfcg") + [[\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")))) +