All Downloads are FREE. Search and download functionalities are using the official Maven repository.

main.com.wisetrack.VerifyLibrary.kt Maven / Gradle / Ivy

There is a newer version: 1.5.3
Show newest version
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
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy