Skip to content

Instantly share code, notes, and snippets.

@massie
Created September 12, 2014 23:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save massie/72209a3c15166eb4511f to your computer and use it in GitHub Desktop.
Save massie/72209a3c15166eb4511f to your computer and use it in GitHub Desktop.
Scala SHA-1 Example
package example
import java.io.FileInputStream
import java.security.MessageDigest
object Sha1Example {
def main(args: Array[String]): Unit = {
val start = System.currentTimeMillis()
val sha1 = MessageDigest.getInstance("SHA1")
val bytes = new Array[Byte](1024 * 1024)
val fis = new FileInputStream("/workspace/data/ALL.chr22.phase1_release_v3.20101123.snps_indels_svs.genotypes.vcf.gz")
Stream.continually(fis.read(bytes)).takeWhile(-1 !=).foreach(sha1.update(bytes, 0, _))
val hashBytes = sha1.digest() // finalize the hash
val sb = new StringBuffer()
hashBytes.foreach { a => sb.append(Integer.toString((a & 0xff) + 0x100, 16).substring(1))}
val end = System.currentTimeMillis()
println(sb.toString)
println("%d ms".format(end - start))
}
@massie
Copy link
Author

massie commented Sep 12, 2014

Output:

1a50d065799c4d32637dbe11eb66e5f1e8b35b89
9570 ms

which means my MacBook Pro, Mid 2012 2.3 GHz Intel Core i7 w/ flash storage handle ~180MB/s

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