Sha256: 00774a928242da7cf5e1c7d7a465256fafe9db5a8e3351a208418b3be4aa0bd9

Contents?: true

Size: 1.51 KB

Versions: 396

Compression:

Stored size: 1.51 KB

Contents

(ns crypto-square)

(defn normalize-plaintext [input]
  (.toLowerCase (clojure.string/replace input #"[^0-9a-zA-Z]" "")))

(defn square-size [input]
  (let [s (Math/sqrt (count input))
        si (Math/round (Math/floor s))]
    (if (== 0 (rem s si))
      si (inc si))))

(defn plaintext-segments [input]
  (let [text (normalize-plaintext input)
        size (square-size text)]
    (vec (map #(apply str %1)
              (partition size size nil text)))))

(defn ciphertext [input]
  (let [segments (plaintext-segments input)
        size (count (first segments))]
    (apply str (for [n (range size)
                     s segments]
                 (nth s n nil)))))

(defn normalize-ciphertext [input]
  (let [cipher                  (ciphertext input)
        cipher-length           (count cipher)
        square-max              (square-size cipher)
        square-longest          (int (Math/ceil (/ (count cipher) square-max)))
        square-shortest         (int (Math/floor (/ (count cipher) square-max)))
        square-count-long-sides (- cipher-length (* square-max square-shortest))]
    (apply str (interpose " "
                    (map #(apply str %1)
                         (concat (partition square-longest square-longest nil
                                    (take (* square-count-long-sides square-longest) cipher))
                                 (partition square-shortest square-shortest nil
                                    (take-last (- cipher-length (* square-count-long-sides square-longest)) cipher))))))))

Version data entries

396 entries across 396 versions & 1 rubygems

Version Path
trackler-2.2.1.139 tracks/clojure/exercises/crypto-square/src/example.clj
trackler-2.2.1.138 tracks/clojure/exercises/crypto-square/src/example.clj
trackler-2.2.1.137 tracks/clojure/exercises/crypto-square/src/example.clj
trackler-2.2.1.136 tracks/clojure/exercises/crypto-square/src/example.clj
trackler-2.2.1.135 tracks/clojure/exercises/crypto-square/src/example.clj
trackler-2.2.1.134 tracks/clojure/exercises/crypto-square/src/example.clj
trackler-2.2.1.133 tracks/clojure/exercises/crypto-square/src/example.clj
trackler-2.2.1.132 tracks/clojure/exercises/crypto-square/src/example.clj
trackler-2.2.1.131 tracks/clojure/exercises/crypto-square/src/example.clj
trackler-2.2.1.130 tracks/clojure/exercises/crypto-square/src/example.clj
trackler-2.2.1.129 tracks/clojure/exercises/crypto-square/src/example.clj
trackler-2.2.1.128 tracks/clojure/exercises/crypto-square/src/example.clj
trackler-2.2.1.127 tracks/clojure/exercises/crypto-square/src/example.clj
trackler-2.2.1.126 tracks/clojure/exercises/crypto-square/src/example.clj
trackler-2.2.1.125 tracks/clojure/exercises/crypto-square/src/example.clj
trackler-2.2.1.124 tracks/clojure/exercises/crypto-square/src/example.clj
trackler-2.2.1.123 tracks/clojure/exercises/crypto-square/src/example.clj
trackler-2.2.1.122 tracks/clojure/exercises/crypto-square/src/example.clj
trackler-2.2.1.121 tracks/clojure/exercises/crypto-square/src/example.clj
trackler-2.2.1.120 tracks/clojure/exercises/crypto-square/src/example.clj