Our great sponsors
-
WorkOS
The modern identity platform for B2B SaaS. The APIs are flexible and easy-to-use, supporting authentication, user identity, and complex enterprise features like SSO and SCIM provisioning.
KotlinUsings, first google result: https://github.com/FelixEngl/KotlinUsings
import java.io.Closeable import java.io.File import java.io.OutputStream import java.math.BigInteger import java.security.DigestOutputStream import java.security.MessageDigest /** Create MD5 checksum for [file] */ fun md5(file: File): String { val md5 = MessageDigest.getInstance("MD5") // create an inline object to hold the resources val resources = object : Closeable { // implement Closeable so `use {}` is available // define the resources as fields val fileStream = file.inputStream().buffered() val digestStream = DigestOutputStream(OutputStream.nullOutputStream(), md5) // manually close the resources override fun close() { fileStream.close() digestStream.close() } } resources.useToRun { // the resources are available as properties of the `resources` object fileStream.transferTo(digestStream) } return "%032x".format(BigInteger(1, md5.digest())) } /** Combined [use] and [run] */ // copied from Gradle Kotlin DSL util // https://github.com/gradle/gradle/blob/1c0c3ddb71f67a303b30c5c5c2e2f75fff83703f/subprojects/kotlin-dsl/src/main/kotlin/org/gradle/kotlin/dsl/support/IO.kt#L26-L27 inline fun T.useToRun(action: T.() -> U): U = use { run(action) }