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.180 tracks/clojure/exercises/crypto-square/src/example.clj
trackler-2.2.1.179 tracks/clojure/exercises/crypto-square/src/example.clj
trackler-2.2.1.178 tracks/clojure/exercises/crypto-square/src/example.clj
trackler-2.2.1.177 tracks/clojure/exercises/crypto-square/src/example.clj
trackler-2.2.1.176 tracks/clojure/exercises/crypto-square/src/example.clj
trackler-2.2.1.175 tracks/clojure/exercises/crypto-square/src/example.clj
trackler-2.2.1.174 tracks/clojure/exercises/crypto-square/src/example.clj
trackler-2.2.1.173 tracks/clojure/exercises/crypto-square/src/example.clj
trackler-2.2.1.172 tracks/clojure/exercises/crypto-square/src/example.clj
trackler-2.2.1.171 tracks/clojure/exercises/crypto-square/src/example.clj
trackler-2.2.1.170 tracks/clojure/exercises/crypto-square/src/example.clj
trackler-2.2.1.169 tracks/clojure/exercises/crypto-square/src/example.clj
trackler-2.2.1.167 tracks/clojure/exercises/crypto-square/src/example.clj
trackler-2.2.1.166 tracks/clojure/exercises/crypto-square/src/example.clj
trackler-2.2.1.165 tracks/clojure/exercises/crypto-square/src/example.clj
trackler-2.2.1.164 tracks/clojure/exercises/crypto-square/src/example.clj
trackler-2.2.1.163 tracks/clojure/exercises/crypto-square/src/example.clj
trackler-2.2.1.162 tracks/clojure/exercises/crypto-square/src/example.clj
trackler-2.2.1.161 tracks/clojure/exercises/crypto-square/src/example.clj
trackler-2.2.1.160 tracks/clojure/exercises/crypto-square/src/example.clj