Mercurial > coderloop
annotate src/decrypt.clj @ 0:307a81e46071 tip
initial committ
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Tue, 18 Oct 2011 01:17:49 -0700 |
parents | |
children |
rev | line source |
---|---|
rlm@0 | 1 (ns coderloop.decrypt |
rlm@0 | 2 (:use [clojure.contrib |
rlm@0 | 3 [duck-streams :only [read-lines]] |
rlm@0 | 4 [seq :only [find-first]] |
rlm@0 | 5 [string :only [map-str]] |
rlm@0 | 6 [combinatorics :only [cartesian-product]]]) |
rlm@0 | 7 (:use rlm.shell-inspect) |
rlm@0 | 8 (:use [clojure [string :only [split]]]) |
rlm@0 | 9 (:import Crypt)) |
rlm@0 | 10 |
rlm@0 | 11 (def t "/home/r/coderloop-test/test.txt") |
rlm@0 | 12 |
rlm@0 | 13 (defn strcat [coll] |
rlm@0 | 14 (apply str coll)) |
rlm@0 | 15 |
rlm@0 | 16 (def numerals [0 2 4 8 ]) |
rlm@0 | 17 |
rlm@0 | 18 (defn crypt [salt s] |
rlm@0 | 19 (Crypt/crypt salt s)) |
rlm@0 | 20 |
rlm@0 | 21 (defn cross-text [text] |
rlm@0 | 22 (let [symbols (filter |
rlm@0 | 23 (fn [s] (<= (count s) 8)) |
rlm@0 | 24 (split (.toLowerCase text) #"[^a-z]+"))] |
rlm@0 | 25 (filter |
rlm@0 | 26 (fn [s] |
rlm@0 | 27 (let [len (count s)] |
rlm@0 | 28 (and |
rlm@0 | 29 (>= len 5) |
rlm@0 | 30 (<= len 8)))) |
rlm@0 | 31 (map (partial apply str) |
rlm@0 | 32 (cartesian-product symbols numerals symbols))))) |
rlm@0 | 33 |
rlm@0 | 34 (defn process-file [f] |
rlm@0 | 35 (let [file (read-lines f) |
rlm@0 | 36 [salt pass] (map (partial apply str) (split-at 2 (first file))) |
rlm@0 | 37 text (strcat (interleave (repeat \newline) (rest file)))] |
rlm@0 | 38 (find-first (fn [s] (= (str salt pass) (crypt salt s))) |
rlm@0 | 39 (cross-text text)))) |
rlm@0 | 40 |
rlm@0 | 41 |
rlm@0 | 42 (if (command-line?) |
rlm@0 | 43 (println (process-file (first *command-line-args*)))) |