Created
October 28, 2021 15:31
-
-
Save Spationaute/7b654ae6036acce8f348623f93dad372 to your computer and use it in GitHub Desktop.
Parallel sha256 checksum with kotlin
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
import java.io.File | |
import java.security.MessageDigest | |
import kotlinx.coroutines.* | |
val digester = MessageDigest.getInstance("SHA-256") | |
suspend fun check(file_path: String, checksum_in: String) = withContext(Dispatchers.Default){ | |
val target_file = File(file_path) | |
if(target_file.isFile){ | |
val byte_array = target_file.readBytes() | |
val checksum_out_raw = digester.digest(byte_array) | |
val string_builder = StringBuilder(); | |
checksum_out_raw.asUByteArray().forEach{ | |
string_builder.append( it.toString(16).padStart(2,'0')) | |
} | |
val checksum_out = string_builder.toString() | |
if(checksum_in==checksum_out) println("$file_path [OK]") else println("$file_path [FAILED]") | |
} | |
} | |
fun main(args : Array<String>) { | |
if(args.size<1){ | |
println("Missing the checksum file.") | |
return | |
} | |
val checksum_file = File(args[0]) | |
if(!checksum_file.isFile){ | |
println("$checksum_file is not a file.") | |
return | |
} | |
runBlocking { | |
checksum_file.reader().readLines().forEach { line -> | |
val split = line.split(" ") | |
val checksum_in = split[0] | |
val file_path = split[1] | |
check(file_path, checksum_in) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment