Skip to content

Commit

Permalink
Hash the action input usgin sha256 at the end
Browse files Browse the repository at this point in the history
  • Loading branch information
eed3si9n committed Dec 23, 2023
1 parent 72fe04c commit 066a1eb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
4 changes: 2 additions & 2 deletions util-cache-resolver/src/main/scala/sbt/util/ActionCache.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ object ActionCache:
)(
config: CacheConfiguration
): ActionResult[O] =
val hash: Long = otherInputs * 13L + Hasher.hashUnsafe[I](key)
val input = ActionInput(hash.toHexString)
val hashInput: Array[Long] = Array(otherInputs, Hasher.hashUnsafe[I](key))
val input = ActionInput(HashUtil.sha256HashStr(hashInput))
val store = config.store
val outputDirectory = config.outputDirectory
val result: Option[ActionResult[O]] = store.get[O](input)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package sbt.util
/**
* An action input is a wrapper around hash.
*/
class ActionInput(hash: String):
private[sbt] class ActionInput(hash: String):
def inputHash: String = hash
override def equals(o: Any): Boolean =
o match {
Expand Down
22 changes: 22 additions & 0 deletions util-cache-resolver/src/main/scala/sbt/util/HashUtil.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package sbt.util

import java.nio.ByteBuffer
import java.nio.file.{ Files, Path }
import java.security.MessageDigest
import net.openhft.hashing.LongHashFunction
import scala.util.Try

Expand All @@ -22,4 +24,24 @@ object HashUtil:

private[sbt] def toFarmHashString(digest: Long): String =
s"farm64-${digest.toHexString}"

def sha256Hash(bytes: Array[Byte]): Array[Byte] =
val digest = MessageDigest.getInstance("SHA-256")
digest.digest(bytes)

def sha256Hash(longs: Array[Long]): Array[Byte] =
sha256Hash(longsToBytes(longs))

def sha256HashStr(longs: Array[Long]): String =
"sha256-" + toHexString(sha256Hash(longs))

def toHexString(bytes: Array[Byte]): String =
val sb = new StringBuilder
for b <- bytes do sb.append(f"${b & 0xff}%02x")
sb.toString

def longsToBytes(longs: Array[Long]): Array[Byte] =
val buffer = ByteBuffer.allocate(longs.length * java.lang.Long.BYTES)
for l <- longs do buffer.putLong(l)
buffer.array()
end HashUtil

0 comments on commit 066a1eb

Please sign in to comment.