Newer
Older
gitbucket_jkp / project / Checksums.scala
@Naoki Takezoe Naoki Takezoe on 12 Jul 2017 877 bytes Remove unnecessary semicolon
  1. import java.security.MessageDigest
  2. import scala.annotation._
  3. import sbt._
  4.  
  5. object Checksums {
  6. private val bufferSize = 2048
  7.  
  8. def generate(source:File, target:File, algorithm:String):Unit =
  9. IO write (target, compute(source, algorithm))
  10.  
  11. def compute(file:File, algorithm:String):String =
  12. hex(raw(file, algorithm))
  13.  
  14. def raw(file:File, algorithm:String):Array[Byte] =
  15. (Using fileInputStream file) { is =>
  16. val md = MessageDigest getInstance algorithm
  17. val buf = new Array[Byte](bufferSize)
  18. md.reset()
  19. @tailrec
  20. def loop() {
  21. val len = is read buf
  22. if (len != -1) {
  23. md update (buf, 0, len)
  24. loop()
  25. }
  26. }
  27. loop()
  28. md.digest()
  29. }
  30.  
  31. def hex(bytes:Array[Byte]):String =
  32. bytes map { it => "%02x" format (it.toInt & 0xff) } mkString ""
  33. }