diff --git a/src/cipher_analytical_machine/caesar.clj b/src/cipher_analytical_machine/caesar.clj new file mode 100644 index 0000000..7ac2285 --- /dev/null +++ b/src/cipher_analytical_machine/caesar.clj @@ -0,0 +1,37 @@ +(ns cipher-analytical-machine.caesar + (:require [clojure.string :as cs]) + (:gen-class)) + +(defn calculate-char-index + "Calculate the index of a char." + [char symbols] + (cs/index-of symbols char)) + +(defn encrypt-index + "Calculates new index of a character. Uses its index, the key and max-index (exclusive)." + [index key max-index] + (-> index + (+ key) + (mod max-index))) + +(defn encrypt-char + "Calculates new index of a character. Uses a char from symbols, the key and max-index (exclusive)." + [char key symbols max-index] + (get symbols + (-> char + (calculate-char-index symbols) + (encrypt-index key max-index)))) + +(defn encrypt-message + "Encrypt a message using the Caesar cipher." + [message key symbols] + (let [max-index (count symbols)] + (cs/join + (map (fn [char] (encrypt-char char key symbols max-index)) + message)))) + +(defn decrypt-message + "Decrypt the ciphtext using the Caesar cipher." + [ciphertext key symbols] + (encrypt-message ciphertext (- key) symbols)) + diff --git a/test/cipher_analytical_machine/caesar_test.clj b/test/cipher_analytical_machine/caesar_test.clj new file mode 100644 index 0000000..bdb4b8d --- /dev/null +++ b/test/cipher_analytical_machine/caesar_test.clj @@ -0,0 +1,64 @@ +(ns cipher-analytical-machine.caesar-test + (:require + [clojure.test :refer :all] + [cipher-analytical-machine.caesar :refer :all] + )) + +(deftest calculate-char-index-test + (let [symbols "abc123%$"] + (testing "Letter 'b' in symbols" + (is (= 1 (calculate-char-index \b symbols)))) + + (testing "Letter 'd' does not in symbols" + (is (= nil (calculate-char-index \d symbols)))) + + (testing "Digit '1' in symbols" + (is (= 3 (calculate-char-index \1 symbols)))) + + (testing "Digit '0' does not in symbols" + (is (= nil (calculate-char-index \0 symbols)))) + + (testing "Symbol '?' does not in symbols" + (is (= nil (calculate-char-index \? symbols)))))) + +(deftest encrypt-index-test + (testing "The new index is in the range." + (is (= 2 (encrypt-index 0 2 33)))) + + (testing "The new index that equal to max-index." + (is (= 0 (encrypt-index 30 3 33)))) + + (testing "The new index that greater than max-index." + (is (= 1 (encrypt-index 30 4 33)))) + + (testing "The new index that lower than 0." + (is (= 32 (encrypt-index 0 -1 33))))) + +(deftest encrypt-char-test + (let [symbols "abc" + max-index (count symbols)] + (testing "Letter 'a' is encrypted to 'b'" + (is (= \b (encrypt-char \a 1 symbols max-index)))) + + (testing "Letter 'b' is encrypted to 'a'" + (is (= \a (encrypt-char \b 2 symbols max-index)))) + + (testing "Letter 'b' is decryped to 'a'" + (is (= \a (encrypt-char \b -1 symbols max-index)))))) + +(deftest encrypt-message-text + (let [symbols "abc"] + (testing "The message 'abc' is encrypted to 'bca'" + (is (= "bca" (encrypt-message "abc" 1 symbols)))) + + (testing "The message 'bca' is decrypted to 'abc'" + (is (= "abc" (encrypt-message "bca" -1 symbols)))))) + +(deftest decrypt-message-text + (let [symbols "abc"] + (testing "The message 'bca' is decrypted to 'abc'" + (is (= "abc" (decrypt-message "bca" 1 symbols))) + + (testing "The message 'abc' is encrypted to 'bca'" + (is (= "bca" (decrypt-message "abc" -1 symbols))))))) + diff --git a/test/cipher_analytical_machine/core_test.clj b/test/cipher_analytical_machine/core_test.clj index 8141c86..cfc3ab0 100644 --- a/test/cipher_analytical_machine/core_test.clj +++ b/test/cipher_analytical_machine/core_test.clj @@ -2,6 +2,3 @@ (:require [clojure.test :refer :all] [cipher-analytical-machine.core :refer :all])) -(deftest a-test - (testing "FIXME, I fail." - (is (= 0 1))))