generate checksums without ivy

This commit is contained in:
Herr Ritschwumm
2016-03-04 14:28:26 +01:00
parent 6a8ec18f9a
commit 413e75be5a
2 changed files with 42 additions and 11 deletions

View File

@@ -139,18 +139,15 @@ executableKey := {
val outputFile = workDir / warName val outputFile = workDir / warName
IO jar (contentMappings, outputFile, manifest) IO jar (contentMappings, outputFile, manifest)
// patch ChecksumHelper to allow sha256
val hack = classOf[ChecksumHelper] getDeclaredField "algorithms"
hack setAccessible true
val algos = (hack get null).asInstanceOf[java.util.Map[String,String]]
algos put ("sha256", "SHA-256")
// generate checksums // generate checksums
Seq("md5", "sha1", "sha256") foreach { algorithm => Seq(
IO.write( "md5" -> "MD5",
workDir / (warName + "." + algorithm), "sha1" -> "SHA-1",
ChecksumHelper computeAsString (outputFile, algorithm) "sha256" -> "SHA-256"
) )
.foreach { case (extension, algorithm) =>
val checksumFile = workDir / (warName + "." + extension)
Checksums generate (outputFile, checksumFile, algorithm)
} }
// done // done

34
project/Checksums.scala Normal file
View File

@@ -0,0 +1,34 @@
import java.security.MessageDigest;
import scala.annotation._
import sbt._
import sbt.Using._
object Checksums {
private val bufferSize = 2048
def generate(source:File, target:File, algorithm:String):Unit =
IO write (target, compute(source, algorithm))
def compute(file:File, algorithm:String):String =
hex(raw(file, algorithm))
def raw(file:File, algorithm:String):Array[Byte] =
(Using fileInputStream file) { is =>
val md = MessageDigest getInstance algorithm
val buf = new Array[Byte](bufferSize)
md.reset()
@tailrec
def loop() {
val len = is read buf
if (len != -1) {
md update (buf, 0, len)
loop()
}
}
loop()
md.digest()
}
def hex(bytes:Array[Byte]):String =
bytes map { it => "%02x" format (it.toInt & 0xff) } mkString ""
}