Skip to content

Instantly share code, notes, and snippets.

@Spationaute
Created October 28, 2021 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Spationaute/7b654ae6036acce8f348623f93dad372 to your computer and use it in GitHub Desktop.
Save Spationaute/7b654ae6036acce8f348623f93dad372 to your computer and use it in GitHub Desktop.
Parallel sha256 checksum with kotlin
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