Created
February 11, 2014 08:24
-
-
Save ponkotuy/8931093 to your computer and use it in GitHub Desktop.
6桁数字をMD5で総当たりしてみた
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
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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Inspired by http://qiita.com/kawasima/items/ef75f317605ce800a839 and http://z.tokumaru.org/2014/02/6php025.html