-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bitcoin-core-binaries: Support SHA256 hash verification
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
Showing
2 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,5 @@ gradlePlugin { | |
|
||
dependencies { | ||
implementation(project(":gradle-tasks")) | ||
implementation(libs.google.guava) | ||
} |
50 changes: 50 additions & 0 deletions
50
...ic/bitcoin-core-binaries/src/main/kotlin/bisq/gradle/bitcoin_core/HashVerificationTask.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`.") | ||
} | ||
} |