Skip to content

Instantly share code, notes, and snippets.

@ponkotuy
Created February 11, 2014 08:24
Show Gist options
  • Save ponkotuy/8931093 to your computer and use it in GitHub Desktop.
Save ponkotuy/8931093 to your computer and use it in GitHub Desktop.
6桁数字をMD5で総当たりしてみた
import java.security.MessageDigest
val md5 = MessageDigest.getInstance("MD5")
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 bruteForce(salt: Array[Byte], digest: Array[Byte]): List[Int] = {
var i = 0
val builder = List.newBuilder[Int]
while(i != 1000000) {
val ary = salt ++ toBytes(i)
val di = md5.digest(ary)
if(equals(di, digest)) builder += i
i += 1
}
builder.result
}
val salt = "hoge".getBytes("UTF8")
val digest = md5.digest(salt ++ toBytes(567890))
println(bruteForce(salt, digest))
@ponkotuy
Copy link
Author

@ponkotuy
Copy link
Author

Result is 472 ms

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