Skip to content

Commit

Permalink
bitcoin-core-binaries: Support SHA256 hash verification
Browse files Browse the repository at this point in the history
The hash verification task looks up the expected hash and verifies that
the provided file has that hash. To look up the expected hash, the
Gradle task parses the input hash list file of the format
'<sha256hash> filename.tar.gz' (per line). Most projects use this format.
  • Loading branch information
alvasw committed Sep 2, 2024
1 parent 68ac51f commit b5be65d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions build-logic/bitcoin-core-binaries/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ gradlePlugin {

dependencies {
implementation(project(":gradle-tasks"))
implementation(libs.google.guava)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package bisq.gradle.bitcoin_core

import com.google.common.hash.Hashing
import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction

abstract class HashVerificationTask : DefaultTask() {
@get:InputFile
abstract val inputFile: RegularFileProperty

@get:InputFile
abstract val sha256File: RegularFileProperty

@get:OutputFile
abstract val resultFile: RegularFileProperty

@TaskAction
fun verify() {
val input = inputFile.get().asFile.readBytes()
val hash: String = Hashing.sha256()
.hashBytes(input)
.toString()

val expectedHash = getExpectedHash()
if (hash != expectedHash) {
throw GradleException(
"Hash verification failed for `${inputFile.get().asFile.absolutePath}`. " +
"Expected: `$expectedHash, Actual: `$hash`"
)
}

resultFile.get().asFile.writeText(hash)
}

private fun getExpectedHash(): String {
val inputFileName = inputFile.get().asFile.name
for (line in sha256File.get().asFile.readLines()) {
if (line.endsWith(inputFileName)) {
// '<sha256hash> filename.tar.gz'
return line.split(" ").first()
}
}

throw GradleException("Couldn't find expected hash for `$inputFile`.")
}
}

0 comments on commit b5be65d

Please sign in to comment.