Mercurial > coderloop
diff 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 |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/decrypt.clj Tue Oct 18 01:17:49 2011 -0700 1.3 @@ -0,0 +1,43 @@ 1.4 +(ns coderloop.decrypt 1.5 + (:use [clojure.contrib 1.6 + [duck-streams :only [read-lines]] 1.7 + [seq :only [find-first]] 1.8 + [string :only [map-str]] 1.9 + [combinatorics :only [cartesian-product]]]) 1.10 + (:use rlm.shell-inspect) 1.11 + (:use [clojure [string :only [split]]]) 1.12 + (:import Crypt)) 1.13 + 1.14 +(def t "/home/r/coderloop-test/test.txt") 1.15 + 1.16 +(defn strcat [coll] 1.17 + (apply str coll)) 1.18 + 1.19 +(def numerals [0 2 4 8 ]) 1.20 + 1.21 +(defn crypt [salt s] 1.22 + (Crypt/crypt salt s)) 1.23 + 1.24 +(defn cross-text [text] 1.25 + (let [symbols (filter 1.26 + (fn [s] (<= (count s) 8)) 1.27 + (split (.toLowerCase text) #"[^a-z]+"))] 1.28 + (filter 1.29 + (fn [s] 1.30 + (let [len (count s)] 1.31 + (and 1.32 + (>= len 5) 1.33 + (<= len 8)))) 1.34 + (map (partial apply str) 1.35 + (cartesian-product symbols numerals symbols))))) 1.36 + 1.37 +(defn process-file [f] 1.38 + (let [file (read-lines f) 1.39 + [salt pass] (map (partial apply str) (split-at 2 (first file))) 1.40 + text (strcat (interleave (repeat \newline) (rest file)))] 1.41 + (find-first (fn [s] (= (str salt pass) (crypt salt s))) 1.42 + (cross-text text)))) 1.43 + 1.44 + 1.45 +(if (command-line?) 1.46 + (println (process-file (first *command-line-args*))))