Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6833ade696 | ||
|
|
9e4c46814c |
@@ -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]
|
||||
(cond
|
||||
(contains? options :message)
|
||||
(:message options)
|
||||
(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 option)
|
||||
(option options)
|
||||
|
||||
(contains? options :message-file)
|
||||
(file/read-file (:message-file options))))
|
||||
(contains? options file-option)
|
||||
(file/read-file (file-option options)))))
|
||||
|
||||
(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 (:message options)
|
||||
symbols (:symbols options)
|
||||
frequencies symbol-frequences/english-letter-frequences]
|
||||
(cond
|
||||
(= action-type :cracking)
|
||||
(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))))
|
||||
|
||||
(defn actions
|
||||
[options arguments action-type]
|
||||
(let [message (get-message options arguments)
|
||||
key (Integer/parseInt (:key options))
|
||||
symbols (:symbols options)]
|
||||
(let [options (load-all-options options)]
|
||||
(println
|
||||
(cond
|
||||
(contains? #{:encrypt :decrypt} action-type)
|
||||
(crypt-actions options arguments action-type)
|
||||
|
||||
(= action-type :cracking)
|
||||
(caesar-analyzers/get-plaintext message symbols symbol-frequences/english-letter-frequences)
|
||||
|
||||
(= action-type :encrypt)
|
||||
(caesar/encrypt-message message key symbols)
|
||||
|
||||
(= action-type :decrypt)
|
||||
(caesar/decrypt-message message key symbols)))))
|
||||
(cracking-actions options arguments action-type)))))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user