Created
February 15, 2014 10:56
-
-
Save nmatsui/9017657 to your computer and use it in GitHub Desktop.
「数字6桁パスワードのMD5ハッシュ値の総当たり」Scalaは1.70秒だった ref: http://qiita.com/nmatsui/items/dd70fd57d81bdd935f79
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 => MD} | |
object MD5FindPassword_Single_WithFormatter { | |
def findPassword(salt:String, pw:String):Option[Int] = { | |
def hexdigest(s:String):String = { | |
val digester = MD.getInstance("MD5") | |
digester.update(s.getBytes) | |
digester.digest.toList.map(_&0xff).map("%02x".format(_)).mkString | |
} | |
(0 to 1000000) | |
.find(i => hexdigest("%s$%06d".format(salt, i)) == pw) | |
} | |
def main(args: Array[String]) { | |
findPassword(args(0), args(1)) match { | |
case Some(f) => printf("match [%06d]%n", f) | |
case None => printf("not match%n") | |
} | |
} | |
} |
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 => MD} | |
object MD5FindPassword_Parallel_NoFormatter { | |
def findPassword(salt:String, pw:String):Option[Int] = { | |
def hexformatter(x:Int):String = { | |
val h = Integer.toHexString(x) | |
"0"*(2 - h.length) + h.toString | |
} | |
def mkOrigin(x:Int):String = { | |
val xs = x.toString | |
salt + "$" + "0"*(6 - xs.length) + xs | |
} | |
def hexdigest(s:String):String = { | |
val digester = MD.getInstance("MD5") | |
digester.update(s.getBytes) | |
digester.digest.toList.map(_&0xff).map(hexformatter(_)).mkString | |
} | |
(0 to 1000000) | |
.par // 変更点はココだけ | |
.find(i => hexdigest(mkOrigin(i)) == pw) | |
} | |
def main(args: Array[String]) { | |
findPassword(args(0), args(1)) match { | |
case Some(f) => printf("match [%06d]%n", f) | |
case None => printf("not match%n") | |
} | |
} | |
} |
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 => MD} | |
object MD5FindPassword_Parallel_WithFormatter { | |
def findPassword(salt:String, pw:String):Option[Int] = { | |
def hexdigest(s:String):String = { | |
val digester = MD.getInstance("MD5") | |
digester.update(s.getBytes) | |
digester.digest.toList.map(_&0xff).map("%02x".format(_)).mkString | |
} | |
(0 to 1000000) | |
.par // 変更点はココだけ | |
.find(i => hexdigest("%s$%06d".format(salt, i)) == pw) | |
} | |
def main(args: Array[String]) { | |
findPassword(args(0), args(1)) match { | |
case Some(f) => printf("match [%06d]%n", f) | |
case None => printf("not match%n") | |
} | |
} | |
} |
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 => MD} | |
object MD5FindPassword_Single_NoFormatter { | |
def findPassword(salt:String, pw:String):Option[Int] = { | |
def hexformatter(x:Int):String = { | |
val h = Integer.toHexString(x) | |
"0"*(2 - h.length) + h.toString | |
} | |
def mkOrigin(x:Int):String = { | |
val xs = x.toString | |
salt + "$" + "0"*(6 - xs.length) + xs | |
} | |
def hexdigest(s:String):String = { | |
val digester = MD.getInstance("MD5") | |
digester.update(s.getBytes) | |
digester.digest.toList.map(_&0xff).map(hexformatter(_)).mkString | |
} | |
(0 to 1000000) | |
.find(i => hexdigest(mkOrigin(i)) == pw) | |
} | |
def main(args: Array[String]) { | |
findPassword(args(0), args(1)) match { | |
case Some(f) => printf("match [%06d]%n", f) | |
case None => printf("not match%n") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment