Skip to content

Instantly share code, notes, and snippets.

@k2nr
Created February 11, 2014 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k2nr/8931653 to your computer and use it in GitHub Desktop.
Save k2nr/8931653 to your computer and use it in GitHub Desktop.
(ns md5.core
(:require [clojure.core.reducers :as r]
[clojure.string :as s])
(:import [java.security MessageDigest]
[javax.xml.bind DatatypeConverter]))
(defn ^String hexdigest [^String s]
(let [digester (MessageDigest/getInstance "MD5")]
(. digester update (.getBytes s))
(DatatypeConverter/printHexBinary (.digest digester))))
(defn ^Boolean match? [^String salt ^String pass ^String digest]
(= digest (hexdigest (str salt "$" pass))))
(defn ^String pad [^Integer n x]
(if (> n (.length (str x)))
(recur n (str 0 x))
(str x)))
(defn find-password [salt pw]
(->> (range 1000000)
(map #(pad 6 %))
(filter #(match? salt % pw))))
(time (dotimes [n 1000000]
(pad 6 n)))
(time (into [] (find-password "hoge" (s/upper-case "4b364677946ccf79f841114e73ccaf4f"))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment