Last active
August 29, 2015 14:19
-
-
Save bernik/7f6ad2c7a32c83100514 to your computer and use it in GitHub Desktop.
Clojure bruteforce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns clj-brute.core | |
(:require [clojure.core.async :as async :refer [go chan <!! >!!]] | |
[digest :refer [md5]]) | |
(:gen-class)) | |
(def password-hash (md5 "zzzzz")) | |
(defn next-byte [^Byte b] | |
(case (char b) | |
\z (byte \0) | |
\9 (byte \a) | |
(inc b))) | |
(defn next-pass [b] | |
(cond | |
(empty? b) [] | |
(= (last b) (byte \z)) (concat [] | |
(next-pass (drop-last b)) | |
(vector (byte \0))) | |
:else (concat [] | |
(drop-last b) | |
(vector (next-byte (last b)))))) | |
(defrecord Part [start end]) | |
(defn worker [in-chan out-chan] | |
(loop [{:keys [start end] :as part} (<!! in-chan)] ; parts | |
(if (not= (first start) end) | |
(do | |
(when (= (md5 (byte-array start)) password-hash) | |
(>!! out-chan (->> start | |
(map char) | |
(apply str)))) | |
(recur (Part. (next-pass start) end))) | |
(recur (<!! in-chan))))) | |
(defn generator [in-chan] | |
(let [init-start (map byte "00000") | |
init-end (next-byte (first init-start))] | |
(loop [{:keys [start end] :as part} (Part. init-start init-end)] | |
(>!! in-chan part) | |
(recur (Part. (cons end (rest start)) | |
(next-byte end)))))) | |
(defn find-password [] | |
(let [in-chan (chan) | |
out-chan (chan) | |
proc-count (.. Runtime getRuntime availableProcessors)] | |
(go (generator in-chan)) | |
(dotimes [n (dec proc-count)] | |
(go (worker in-chan out-chan))) | |
(println "Password: " (<!! out-chan)))) | |
(defn -main [& args] | |
(println (time (find-password)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defproject clj-brute "0.1.0-SNAPSHOT" | |
:description "FIXME: write description" | |
:url "http://example.com/FIXME" | |
:license {:name "Eclipse Public License" | |
:url "http://www.eclipse.org/legal/epl-v10.html"} | |
:dependencies [[org.clojure/clojure "1.6.0"] | |
[org.clojure/core.async "0.1.346.0-17112a-alpha"] | |
[digest "1.4.4"]] | |
:main ^:skip-aot clj-brute.core | |
:target-path "target/%s") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment