Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ef87a719a3 | ||
|
|
2f0254f677 | ||
|
|
6339774bd1 | ||
|
|
a44fc24bee |
@@ -5,32 +5,11 @@
|
|||||||
[cipher-analytical-machine.analyzers.caesar :as caesar-analyzers]
|
[cipher-analytical-machine.analyzers.caesar :as caesar-analyzers]
|
||||||
[cipher-analytical-machine.symbols.factories :as sf]
|
[cipher-analytical-machine.symbols.factories :as sf]
|
||||||
[cipher-analytical-machine.symbols.frequencies :as symbol-frequencies]
|
[cipher-analytical-machine.symbols.frequencies :as symbol-frequencies]
|
||||||
|
[cipher-analytical-machine.cli.options :as options]
|
||||||
[clojure.string :as cs]
|
[clojure.string :as cs]
|
||||||
[clojure.tools.cli :refer [parse-opts]])
|
[clojure.tools.cli :refer [parse-opts]])
|
||||||
(:gen-class))
|
(:gen-class))
|
||||||
|
|
||||||
(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."]
|
|
||||||
["-k" "--key KEY" "The key will be used to encrypt or decrypt."]
|
|
||||||
["-c" "--cipher CIPHER" "The cipher will be used to encrypt or decrypt a message."
|
|
||||||
:default "Caesar"]
|
|
||||||
["-e" "--encrypt" "Encrypt the message."]
|
|
||||||
["-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."]
|
|
||||||
["-O" "--output-file OUTPUT-FILE" "Save the program output to a file."]
|
|
||||||
["-h" "--help"]])
|
|
||||||
|
|
||||||
(defn usage [options-summary]
|
(defn usage [options-summary]
|
||||||
(->> ["This is cipher-analytical-machine. It can help you to learn how ciphers works."
|
(->> ["This is cipher-analytical-machine. It can help you to learn how ciphers works."
|
||||||
""
|
""
|
||||||
@@ -38,6 +17,12 @@
|
|||||||
""
|
""
|
||||||
"Options:"
|
"Options:"
|
||||||
options-summary
|
options-summary
|
||||||
|
""
|
||||||
|
"Available values"
|
||||||
|
""
|
||||||
|
(options/get-available-cipher-options-as-string)
|
||||||
|
""
|
||||||
|
(options/get-available-analyzers-options-as-string)
|
||||||
""]
|
""]
|
||||||
(cs/join \newline)))
|
(cs/join \newline)))
|
||||||
|
|
||||||
@@ -55,7 +40,7 @@
|
|||||||
should exit (with an error message, and optional ok status), or a map
|
should exit (with an error message, and optional ok status), or a map
|
||||||
indicating the action the program should take and the options provided."
|
indicating the action the program should take and the options provided."
|
||||||
[args]
|
[args]
|
||||||
(let [{:keys [options arguments errors summary]} (parse-opts args cli-options)]
|
(let [{:keys [options arguments errors summary]} (parse-opts args options/cli-options)]
|
||||||
(cond
|
(cond
|
||||||
(:help options)
|
(:help options)
|
||||||
{:exit-message (usage summary) :ok? true}
|
{:exit-message (usage summary) :ok? true}
|
||||||
@@ -77,6 +62,16 @@
|
|||||||
(file/is-file-exists? (:output-file options)))
|
(file/is-file-exists? (:output-file options)))
|
||||||
{:exit-message (error-msg ["Please, choose another file!!! Overwriting was prevented!!!"])}
|
{:exit-message (error-msg ["Please, choose another file!!! Overwriting was prevented!!!"])}
|
||||||
|
|
||||||
|
(false? (contains? options/cipher-options (:cipher options)))
|
||||||
|
{:exit-message (error-msg ["Please, choose an available cipher!!!\n"
|
||||||
|
(options/get-available-cipher-options-as-string)])}
|
||||||
|
|
||||||
|
(and (map-has-keys? options [:cipher, :cracking, :analyzer])
|
||||||
|
(false? (contains?
|
||||||
|
(get options/analyzers-options (:cipher options)) (:analyzer options))))
|
||||||
|
{:exit-message (error-msg ["Please, choose an available analyzer!!!\n"
|
||||||
|
(options/get-available-analyzers-options-as-string)])}
|
||||||
|
|
||||||
(and (map-has-keys? options [:cipher, :key])
|
(and (map-has-keys? options [:cipher, :key])
|
||||||
(or (contains? options :message)
|
(or (contains? options :message)
|
||||||
(contains? options :message-file))
|
(contains? options :message-file))
|
||||||
@@ -153,14 +148,26 @@
|
|||||||
(save-output output options))
|
(save-output output options))
|
||||||
(println output))
|
(println output))
|
||||||
|
|
||||||
(defn cracking-actions
|
(defn cracking-caesar
|
||||||
[options arguments action-type]
|
[options arguments]
|
||||||
(let [message (:message options)
|
(let [analyzer (:analyzer options)
|
||||||
|
message (:message options)
|
||||||
symbols (:symbols options)
|
symbols (:symbols options)
|
||||||
frequencies symbol-frequencies/english-letter-frequencies]
|
frequencies symbol-frequencies/english-letter-frequencies]
|
||||||
(cond
|
(cond
|
||||||
(= action-type :cracking)
|
(= analyzer "Chi^2")
|
||||||
(caesar-analyzers/get-plaintext message symbols frequencies)
|
(caesar-analyzers/get-plaintext message symbols frequencies)
|
||||||
|
|
||||||
|
(= analyzer "Frequency")
|
||||||
|
(caesar-analyzers/frequency-analizer-get-plaintext message symbols
|
||||||
|
(symbol-frequencies/map-to-string frequencies)))))
|
||||||
|
|
||||||
|
(defn cracking-actions
|
||||||
|
[options arguments action-type]
|
||||||
|
(let [cipher (:cipher options)]
|
||||||
|
(cond
|
||||||
|
(= cipher "Caesar")
|
||||||
|
(cracking-caesar options arguments)
|
||||||
)))
|
)))
|
||||||
|
|
||||||
(defn crypt-actions
|
(defn crypt-actions
|
||||||
|
|||||||
@@ -0,0 +1,64 @@
|
|||||||
|
(ns cipher-analytical-machine.cli.options
|
||||||
|
(:require
|
||||||
|
[cipher-analytical-machine.symbols.factories :as sf]
|
||||||
|
[clojure.string :as cs])
|
||||||
|
(:gen-class))
|
||||||
|
|
||||||
|
(defn option-values-as-row
|
||||||
|
"Return the formatted string of values for an option."
|
||||||
|
[option values]
|
||||||
|
(str option ": " (cs/join ", " values)))
|
||||||
|
|
||||||
|
(defn option-values-as-list
|
||||||
|
"Return the formatted string of values for an option as a multiline list."
|
||||||
|
[option values]
|
||||||
|
(str option ": \n - " (cs/join "\n - " values)))
|
||||||
|
|
||||||
|
(defn map-of-option-value-as-list
|
||||||
|
"Return the formatted string of values for an option as a multiline list.
|
||||||
|
The values are a map of pairs (suboption and its options)."
|
||||||
|
[option values]
|
||||||
|
(let [values (map #(apply option-values-as-row %) values)]
|
||||||
|
(option-values-as-list option values)))
|
||||||
|
|
||||||
|
(def cipher-options
|
||||||
|
#{"Caesar"})
|
||||||
|
|
||||||
|
(def default-cipher "Caesar")
|
||||||
|
|
||||||
|
(defn get-available-cipher-options-as-string
|
||||||
|
"Return the formatted string of analyzers options."
|
||||||
|
[]
|
||||||
|
(option-values-as-row "Ciphers" cipher-options))
|
||||||
|
|
||||||
|
(def analyzers-options
|
||||||
|
{"Caesar" #{"Chi^2" "Frequency"}})
|
||||||
|
|
||||||
|
(defn get-available-analyzers-options-as-string
|
||||||
|
"Return the formatted string of analyzers options."
|
||||||
|
[]
|
||||||
|
(map-of-option-value-as-list "Analyzers" analyzers-options))
|
||||||
|
|
||||||
|
(def language-options
|
||||||
|
#{"en", "uk"})
|
||||||
|
|
||||||
|
(def default-symbols
|
||||||
|
(sf/default-symbol-factory "en"))
|
||||||
|
|
||||||
|
(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."]
|
||||||
|
["-k" "--key KEY" "The key will be used to encrypt or decrypt."]
|
||||||
|
["-c" "--cipher CIPHER" "The cipher will be used to encrypt or decrypt a message."
|
||||||
|
:default default-cipher]
|
||||||
|
["-e" "--encrypt" "Encrypt the message."]
|
||||||
|
["-d" "--decrypt" "Decrypt the message."]
|
||||||
|
["-s" "--symbols SYMBOLS" "The string will be used as a set of symbols for a cipher."
|
||||||
|
:default default-symbols]
|
||||||
|
["-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."]
|
||||||
|
["-a" "--analyzer ANALYZER" "The way of cracking."]
|
||||||
|
["-O" "--output-file OUTPUT-FILE" "Save the program output to a file."]
|
||||||
|
["-h" "--help"]])
|
||||||
|
|
||||||
Reference in New Issue
Block a user