Compare commits

...
4 Commits
Author SHA1 Message Date
KKlochko 6f15eedacd Add tests for the cipher-analyzers module.
continuous-integration/drone/push Build is passing
2023-09-18 22:01:02 +03:00
KKlochko 182d9894f7 Rename the cipher_analyzer module to cipher-analyzer. 2023-09-18 21:57:32 +03:00
KKlochko e3954804c8 Add the cracking option.
continuous-integration/drone/push Build is passing
2023-09-17 21:45:09 +03:00
KKlochko b1f3bb8585 Rename the symbol_frequences module to symbol-frequences. 2023-09-17 21:43:09 +03:00
5 changed files with 39 additions and 3 deletions
@@ -1,6 +1,6 @@
(ns cipher-analytical-machine.caesar-analyzers (ns cipher-analytical-machine.caesar-analyzers
(:require [cipher-analytical-machine.caesar :as caesar] (:require [cipher-analytical-machine.caesar :as caesar]
[cipher-analytical-machine.cipher_analyzers :as ca]) [cipher-analytical-machine.cipher-analyzers :as ca])
(:gen-class)) (:gen-class))
(defn get-all-texts (defn get-all-texts
@@ -1,4 +1,4 @@
(ns cipher-analytical-machine.cipher_analyzers (ns cipher-analytical-machine.cipher-analyzers
(:require [clojure.string :as cs]) (:require [clojure.string :as cs])
(:gen-class)) (:gen-class))
+11
View File
@@ -2,7 +2,9 @@
(:require (:require
[cipher-analytical-machine.file :as file] [cipher-analytical-machine.file :as file]
[cipher-analytical-machine.caesar :as caesar] [cipher-analytical-machine.caesar :as caesar]
[cipher-analytical-machine.caesar-analyzers :as caesar-analyzers]
[cipher-analytical-machine.symbol-factories :as sf] [cipher-analytical-machine.symbol-factories :as sf]
[cipher-analytical-machine.symbol-frequences :as symbol-frequences]
[clojure.string :as cs] [clojure.string :as cs]
[clojure.tools.cli :refer [parse-opts]]) [clojure.tools.cli :refer [parse-opts]])
(:gen-class)) (:gen-class))
@@ -20,6 +22,7 @@
["-d" "--decrypt" "Decrypt the message."] ["-d" "--decrypt" "Decrypt the message."]
["-s" "--symbols" "The string will be used as a set of symbols for a cipher." ["-s" "--symbols" "The string will be used as a set of symbols for a cipher."
:default (sf/default-symbol-factory "en")] :default (sf/default-symbol-factory "en")]
["-C" "--cracking" "Cracking the encrypted message."]
["-h" "--help"]]) ["-h" "--help"]])
(defn usage [options-summary] (defn usage [options-summary]
@@ -72,6 +75,11 @@
(contains? options :decrypt) (contains? options :decrypt)
{:options options :arguments arguments :action-type :decrypt}) {:options options :arguments arguments :action-type :decrypt})
(and (map-has-keys? options [:cipher, :cracking])
(or (contains? options :message)
(contains? options :message-file)))
{:options options :arguments arguments :action-type :cracking}
:else :else
{:exit-message (usage summary)}))) {:exit-message (usage summary)})))
@@ -97,6 +105,9 @@
symbols (:symbols options)] symbols (:symbols options)]
(println (println
(cond (cond
(= action-type :cracking)
(caesar-analyzers/get-plaintext message symbols symbol-frequences/english-letter-frequences)
(= action-type :encrypt) (= action-type :encrypt)
(caesar/encrypt-message message key symbols) (caesar/encrypt-message message key symbols)
@@ -1,4 +1,4 @@
(ns cipher-analytical-machine.symbol_frequences (ns cipher-analytical-machine.symbol-frequences
(:gen-class)) (:gen-class))
(def english-letter-frequences { (def english-letter-frequences {
@@ -0,0 +1,25 @@
(ns cipher-analytical-machine.cipher-analyzers-test
(:require
[clojure.test :refer :all]
[cipher-analytical-machine.cipher-analyzers :refer :all]))
(deftest count-characters-test
(testing "Count two dublicates and one uniq character from a string."
(is (= {\a 2 \b 1}
(count-characters "aba")))))
(deftest chi-squared-letter-statistic-test
(testing "If the frequence of A is 0.1, the text length is 100 and contains 20 times, then the chi-squared is 10."
(is (= 10.0
(chi-squared-letter-statistic 20 0.1 100))))
(testing "If the frequence of A is 0.1, the text length is 10 and contains 5 times, then the chi-squared is 16.0."
(is (= 16.0
(chi-squared-letter-statistic 5 0.1 10)))))
(deftest chi-squared-statistic-test
(let [text "aaaaabbbbb"]
(testing "If the frequences of a and b is 0.1, the text length is 10 and contains them 5 times, then score is 32."
(is (= 32.0
(chi-squared-statistic text {\a 0.1 \b 0.1}))))))