/* sbt -- Simple Build Tool
 * Copyright 2009 Mark Harrah
 */
package sbt

import java.io.{ByteArrayInputStream, File, InputStream}

object object sbt.HashHash
{
	private val IntBufferSize = Int(8192)8192
	def (Array[Byte])StringtoHex(Array[Byte]bytes: Array[Byte]Array[Byte]): StringString =
	{
		val StringBuilderbuffer = (Int)StringBuildernew StringBuilderStringBuilder(Array[Byte]bytes.=> Intlength (Int)Int* Int(2)2)
		for(((Int) => Unit)Uniti <- implicit scala.Predef.intWrapper : (Int)scala.runtime.RichInt0 (Int)Rangeuntil Array[Byte]bytes.=> Intlength)
		{
			val Byteb = (Int)Bytebytes(Inti)
			val Intbi: IntInt = Intif(Byteb (Int)Boolean< Int(0)0) Byteb (Int)Int+ Int(256)256 else implicit scala.Predef.byte2int : (Byte)Intb
			StringBuilderbuffer (Char)StringBuilderappend (Byte)ChartoHex((Intbi (Int)Int>>> Int(4)4).ByteasInstanceOf[ByteByte])
			StringBuilderbuffer (Char)StringBuilderappend (Byte)ChartoHex((Intbi (Int)Int& Int(15)0x0F).ByteasInstanceOf[ByteByte])
		}
		StringBuilderbuffer.()StringtoString
	}
	def (String)Array[Byte]fromHex(Stringhex: StringString): Array[Byte]Array[Byte] =
	{
		(Boolean,=> Any)Unitrequire((Stringhex.()Intlength (Int)Int& Int(1)1) (Int)Boolean== Int(0)0, java.lang.String("Hex string must have length 2n.")"Hex string must have length 2n.")
		val Array[Byte]array = Array[Byte]new Array[Byte]Array[Byte](Stringhex.()Intlength (Int)Int>> Int(1)1)
		for(((Int) => Unit)Uniti <- implicit scala.Predef.intWrapper : (Int)scala.runtime.RichInt0 (Int)Rangeuntil Stringhex.()Intlength (Int)Rangeby Int(2)2)
		{
			val Charc1 = Stringhex.(Int)CharcharAt(Inti)
			val Charc2 = Stringhex.(Int)CharcharAt(Inti(Int)Int+Int(1)1)
			Array[Byte]array(Inti (Int)Int>> Int(1)1) = (((Char)IntfromHex(Charc1) (Int)Int<< Int(4)4) (Int)Int| (Char)IntfromHex(Charc2)).ByteasInstanceOf[ByteByte]
		}
		Array[Byte]array
	}
	/** Calculates the SHA-1 hash of the given String.*/
	def (String,sbt.Logger)Either[String,Array[Byte]]apply(Strings: StringString, sbt.Loggerlog: sbt.LoggerLogger): Either[String,Array[Byte]]Either[String, Array[Byte]] = (java.io.InputStream,sbt.Logger)Either[String,Array[Byte]]apply(java.io.ByteArrayInputStreamnew java.io.ByteArrayInputStreamByteArrayInputStream(Strings.(java.lang.String)Array[Byte]getBytes(java.lang.String("UTF-8")"UTF-8")), sbt.Loggerlog)
	/** Calculates the SHA-1 hash of the given file.*/
	def (sbt.Path,sbt.Logger)Either[String,Array[Byte]]apply(sbt.Pathpath: sbt.PathPath, sbt.Loggerlog: sbt.LoggerLogger): Either[String,Array[Byte]]Either[String, Array[Byte]] = (java.io.File,sbt.Logger)Either[String,Array[Byte]]apply(sbt.Pathpath.=> java.io.FileasFile, sbt.Loggerlog)
	/** Calculates the SHA-1 hash of the given file.*/
	def (java.io.File,sbt.Logger)Either[String,Array[Byte]]apply(java.io.Filefile: java.io.FileFile, sbt.Loggerlog: sbt.LoggerLogger): Either[String,Array[Byte]]Either[String, Array[Byte]] =
		object sbt.FileUtilitiesFileUtilities.(java.io.File,sbt.Logger)((java.io.InputStream) => Either[String,Array[Byte]])Either[String,Array[Byte]]readStreamValue(java.io.Filefile, sbt.Loggerlog) { java.io.InputStreamstream => (java.io.InputStream,sbt.Logger)Either[String,Array[Byte]]apply(java.io.InputStreamstream, sbt.Loggerlog) }
	/** Calculates the SHA-1 hash of the given stream, closing it when finished.*/
	def (java.io.InputStream,sbt.Logger)Either[String,Array[Byte]]apply(java.io.InputStreamstream: java.io.InputStreamInputStream, sbt.Loggerlog: sbt.LoggerLogger): Either[String,Array[Byte]]Either[String, Array[Byte]] =
	{
		import java.security.{MessageDigest, DigestInputStream}
		val java.security.MessageDigestdigest = object java.security.MessageDigestMessageDigest.(java.lang.String)java.security.MessageDigestgetInstance(java.lang.String("SHA")"SHA")
		object sbt.ControlControl.(=> String,sbt.Logger)(=> Either[String,Array[Byte]])(=> Unit)Either[String,Array[Byte]]trapAndFinally(java.lang.String("Error computing digest: ")"Error computing digest: ", sbt.Loggerlog)
		{
			val java.security.DigestInputStreamdis = java.security.DigestInputStreamnew java.security.DigestInputStreamDigestInputStream(java.io.InputStreamstream, java.security.MessageDigestdigest)
			val Array[Byte]buffer = Array[Byte]new Array[Byte]Array[Byte](=> IntBufferSize)
			Unitwhile(java.security.DigestInputStreamdis.(Array[Byte])Intread(Array[Byte]buffer) (Int)Boolean>= Int(0)0) Unit{}
			java.security.DigestInputStreamdis.()Unitclose()
			(Array[Byte])Right[Nothing,Array[Byte]]Right(java.security.MessageDigestdigest.()Array[Byte]digest)
		}
		{ java.io.InputStreamstream.()Unitclose() }
	}

	private def (Byte)ChartoHex(Byteb: ByteByte): CharChar =
	{
		(Boolean,=> Any)Unitrequire(Byteb (Int)Boolean>= Int(0)0 (Boolean)Boolean&& Byteb (Int)Boolean<= Int(15)15, java.lang.String("Byte ")"Byte " (Any)java.lang.String+ Byteb (Any)java.lang.String+ java.lang.String(" was not between 0 and 15")" was not between 0 and 15")
		Charif(Byteb (Int)Boolean< Int(10)10)
			(Char('0')'0'.IntasInstanceOf[IntInt] (Byte)Int+ Byteb).CharasInstanceOf[CharChar]
		else
			(Char('a')'a'.IntasInstanceOf[IntInt] (Int)Int+ (Byteb(Int)Int-Int(10)10)).CharasInstanceOf[CharChar]
	}
	private def (Char)IntfromHex(Charc: CharChar): IntInt =
	{
		val Intb =
			Intif(Charc (Char)Boolean>= Char('0')'0' (Boolean)Boolean&& Charc (Char)Boolean<= Char('9')'9')
				(Charc (Char)Int- Char('0')'0')
			else Intif(Charc (Char)Boolean>= Char('a')'a' (Boolean)Boolean&& Charc (Char)Boolean<= Char('f')'f')
				(Charc (Char)Int- Char('a')'a') (Int)Int+ Int(10)10
			else Intif(Charc (Char)Boolean>= Char('A')'A' (Boolean)Boolean&& Charc (Char)Boolean<= Char('F')'F')
				(Charc (Char)Int- Char('A')'A') (Int)Int+ Int(10)10
			else
				Nothingthrow (java.lang.String)java.lang.RuntimeExceptionnew java.lang.RuntimeExceptionRuntimeException(java.lang.String("Invalid hex character: '")"Invalid hex character: '" (Any)java.lang.String+ Charc (Any)java.lang.String+ java.lang.String("'.")"'.")
		Intb
	}
}