Skip to content

Instantly share code, notes, and snippets.

@ponkotuy
Created February 11, 2014 18:10
Show Gist options
  • Save ponkotuy/8940587 to your computer and use it in GitHub Desktop.
Save ponkotuy/8940587 to your computer and use it in GitHub Desktop.
MD5総当たりjasypt版
import scala.collection.GenSeq
import org.jasypt.util.digest.Digester
object Main {
val digester = new Digester("MD5")
def main(args: Array[String]): Unit = {
val salt = "hoge".getBytes("UTF8")
val digest = digester.digest(salt ++ toBytes(567890))
val single = timer { println(bruteForceSingle(salt, digest)) }
val multi = timer { println(bruteForceMulti(salt, digest)) }
println(s"SingleCore: $single")
println(s"MultiCore: $multi")
}
def equals[T](xs: Array[T], ys: Array[T]): Boolean = {
if(xs.size != ys.size) return false
var i = 0
while(i != xs.size) {
if(xs(i) != ys(i)) return false
i += 1
}
true
}
def toBytes(x: Int): Array[Byte] = {
var value = x
val b = new Array[Byte](4)
var i = 3
while(i != 0) {
b(i) = value.byteValue
value = value >>> 8
i -= 1
}
b(0) = value.byteValue
b
}
def bruteForceOne(salt: Array[Byte], digest: Array[Byte])(i: Int): Boolean = {
val ary = salt ++ toBytes(i)
val di = digester.digest(ary)
equals(di, digest)
}
def bruteForceMulti(salt: Array[Byte], digest: Array[Byte]): GenSeq[Int] =
(0 until 1000000).par.filter(bruteForceOne(salt, digest))
def bruteForceSingle(salt: Array[Byte], digest: Array[Byte]): GenSeq[Int] =
(0 until 1000000).filter(bruteForceOne(salt, digest))
def timer(f: => Any): Long = {
val start = System.currentTimeMillis
f
System.currentTimeMillis - start
}
}
@ponkotuy
Copy link
Author

これ https://gist.github.com/ponkotuy/8931093 のjasyptを用いたバージョン。
手元環境の結果は

  • Single - 343ms
  • Multi - 414ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment