main.com.wisetrack.VerifyLibrary.kt Maven / Gradle / Ivy
package com.wisetrack
import com.wisetrack.sdk.WiseTrack
import java.io.InputStream
import java.security.MessageDigest
class VerifyLibrary {
fun InputStream.calculateSHA256(): String {
val buffer = ByteArray(1024)
val sha256Digest = MessageDigest.getInstance("SHA-256")
var bytesRead: Int
while (this.read(buffer).also { bytesRead = it } != -1) {
sha256Digest.update(buffer, 0, bytesRead)
}
val hashBytes = sha256Digest.digest()
println("hash -> ${hashBytes.joinToString("") { "%02x".format(it) }}")
return hashBytes.joinToString("") { "%02x".format(it) }
}
fun verifyLibraryIntegrity(expectedChecksum: String): Boolean {
val classLoader = VerifyLibrary::class.java.classLoader
val resourceName = VerifyLibrary::class.java.protectionDomain.codeSource.location.path
val libraryStream: InputStream? = classLoader?.getResourceAsStream(resourceName)
return if (libraryStream != null) {
val actualChecksum = libraryStream.calculateSHA256()
actualChecksum == expectedChecksum
} else {
false
}
}
}