Wednesday, November 9, 2011

md5sum in scala

How to compute the md5sum of a file (to get the same result as the one given by linux md5sum command)
Inspired from MD5Sum in Scala and modified to work with input streams.

 def md5sum(input:InputStream):String = {
    val bis = new BufferedInputStream(input)
    val buf = new Array[Byte](1024)
    val md5 ="MD5")
    Stream.continually( != -1).foreach(md5.update(buf, 0, _))
    md5.digest().map(0xFF & _).map { "%02x".format(_) }.foldLeft(""){_ + _}

No comments:

Post a Comment