Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Show comment
Hide comment
@ponkotuy

ponkotuy Feb 11, 2014

Result is 472 ms

Owner

ponkotuy commented Feb 11, 2014

Result is 472 ms

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