Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6833ade696 | ||
|
|
9e4c46814c | ||
|
|
6f15eedacd | ||
|
|
182d9894f7 |
@@ -1,6 +1,6 @@
|
||||
(ns cipher-analytical-machine.caesar-analyzers
|
||||
(:require [cipher-analytical-machine.caesar :as caesar]
|
||||
[cipher-analytical-machine.cipher_analyzers :as ca])
|
||||
[cipher-analytical-machine.cipher-analyzers :as ca])
|
||||
(:gen-class))
|
||||
|
||||
(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])
|
||||
(:gen-class))
|
||||
|
||||
|
||||
@@ -12,6 +12,9 @@
|
||||
(def cipher-options
|
||||
#{"Caesar"})
|
||||
|
||||
(def language-options
|
||||
#{"en", "uk"})
|
||||
|
||||
(def cli-options
|
||||
[["-m" "--message MESSAGE" "The message will be encrypted or decrypted."]
|
||||
["-M" "--message-file MESSAGE-FILE" "The file contains the message that will be encrypted or decrypted."]
|
||||
@@ -22,6 +25,8 @@
|
||||
["-d" "--decrypt" "Decrypt the message."]
|
||||
["-s" "--symbols" "The string will be used as a set of symbols for a cipher."
|
||||
:default (sf/default-symbol-factory "en")]
|
||||
["-l" "--language CODE" "The string will be used to set a default symbols for a cipher."
|
||||
:validate [#(contains? language-options %) "Must be a code from the list: en, uk!!!"]]
|
||||
["-C" "--cracking" "Cracking the encrypted message."]
|
||||
["-h" "--help"]])
|
||||
|
||||
@@ -88,29 +93,72 @@
|
||||
(println exit-message)
|
||||
(System/exit (if status 0 1)))
|
||||
|
||||
(defn get-message
|
||||
"Return the message"
|
||||
[options arguments]
|
||||
(defn get-or-load-option
|
||||
"Return the option value or load the value from a file."
|
||||
[options arguments option]
|
||||
(let [file-option (keyword (str (name option) "-file"))]
|
||||
(cond
|
||||
(contains? options :message)
|
||||
(:message options)
|
||||
(contains? options option)
|
||||
(option options)
|
||||
|
||||
(contains? options :message-file)
|
||||
(file/read-file (:message-file options))))
|
||||
(contains? options file-option)
|
||||
(file/read-file (file-option options)))))
|
||||
|
||||
(defn actions
|
||||
(defn set-option
|
||||
"Set a option in the option map."
|
||||
[options option value]
|
||||
(-> options
|
||||
(assoc option value)))
|
||||
|
||||
(defn set-symbols
|
||||
"Set defaults symbols for a language."
|
||||
[options]
|
||||
(set-option options :symbols
|
||||
(sf/default-symbol-factory (:language options))))
|
||||
|
||||
(defn load-and-set-option
|
||||
"Load the option and set it."
|
||||
[options option]
|
||||
(->> (get-or-load-option options nil option)
|
||||
(set-option options option)))
|
||||
|
||||
(defn load-all-options
|
||||
"Load all options."
|
||||
[options]
|
||||
(-> options
|
||||
(set-symbols)
|
||||
(load-and-set-option :message)))
|
||||
|
||||
(defn cracking-actions
|
||||
[options arguments action-type]
|
||||
(let [message (get-message options arguments)
|
||||
key (Integer/parseInt (:key options))
|
||||
symbols (:symbols options)]
|
||||
(println
|
||||
(let [message (:message options)
|
||||
symbols (:symbols options)
|
||||
frequencies symbol-frequences/english-letter-frequences]
|
||||
(cond
|
||||
(= action-type :cracking)
|
||||
(caesar-analyzers/get-plaintext message symbols symbol-frequences/english-letter-frequences)
|
||||
(caesar-analyzers/get-plaintext message symbols frequencies)
|
||||
)))
|
||||
|
||||
(defn crypt-actions
|
||||
[options arguments action-type]
|
||||
(let [message (:message options)
|
||||
key (Integer/parseInt (:key options))
|
||||
symbols (:symbols options)]
|
||||
(cond
|
||||
(= action-type :encrypt)
|
||||
(caesar/encrypt-message message key symbols)
|
||||
|
||||
(= action-type :decrypt)
|
||||
(caesar/decrypt-message message key symbols)))))
|
||||
(caesar/decrypt-message message key symbols))))
|
||||
|
||||
(defn actions
|
||||
[options arguments action-type]
|
||||
(let [options (load-all-options options)]
|
||||
(println
|
||||
(cond
|
||||
(contains? #{:encrypt :decrypt} action-type)
|
||||
(crypt-actions options arguments action-type)
|
||||
|
||||
(= action-type :cracking)
|
||||
(cracking-actions options arguments action-type)))))
|
||||
|
||||
|
||||
@@ -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}))))))
|
||||
|
||||
Reference in New Issue
Block a user