Skip to content

Commit

Permalink
Optimize APQ hash checking (#2318)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyri-petrou authored Jul 18, 2024
1 parent 95d48d1 commit 3ab9d6a
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions core/src/main/scala/caliban/wrappers/ApolloPersistedQueries.scala
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,26 @@ object ApolloPersistedQueries {
case _ => None
}

private def hex(bytes: Array[Byte]): String = {
val builder = new mutable.StringBuilder(bytes.length * 2)
bytes.foreach(byte => builder.append(f"$byte%02x".toLowerCase))
builder.mkString
}
private val HexArray: Array[Char] = "0123456789abcdef".toCharArray

private def checkHash(hash: String, query: String): Boolean = {
val sha256 = java.security.MessageDigest.getInstance("SHA-256")
val digest = sha256.digest(query.getBytes(StandardCharsets.UTF_8))
hex(digest) == hash
digestMatchesHash(digest, hash)
}

private def digestMatchesHash(digest: Array[Byte], hash: String): Boolean = {
val chars = hash.toCharArray
val size = digest.length
var j = 0
var res = chars.length == size * 2
while (j < size && res) {
val v = digest(j) & 0xff
if (chars(j * 2) == HexArray(v >>> 4) && chars(j * 2 + 1) == HexArray(v & 0xf)) ()
else res = false
j += 1
}
res
}

}

0 comments on commit 3ab9d6a

Please sign in to comment.