Skip to content

Commit

Permalink
App signature modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Anandbotq committed Dec 17, 2024
1 parent ebc870d commit 0ee281b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 24 deletions.
47 changes: 47 additions & 0 deletions protect/src/main/java/com/webileapps/safeguard/SecurityChecker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ import androidx.core.content.ContextCompat
import com.webileapps.safeguard.NetworkUtils.isProxySet
import com.webileapps.safeguard.NetworkUtils.isVPNActive
import com.webileapps.safeguard.NetworkUtils.isWifiSecure
import java.io.File
import java.io.FileInputStream
import java.io.InputStream
import java.security.MessageDigest
import java.util.function.Consumer
import kotlin.system.exitProcess

Expand Down Expand Up @@ -641,4 +645,47 @@ class SecurityChecker(private val context: Context, private val config: Security
networkCallback = null
connectivityManager = null
}

fun generateFileChecksum(file: File, algorithm: String = "SHA-256"): String? {
try {
// Create a MessageDigest instance
val messageDigest = MessageDigest.getInstance(algorithm)

// Read file and update the digest
val inputStream: InputStream = FileInputStream(file)
val buffer = ByteArray(1024)
var bytesRead: Int
while (inputStream.read(buffer).also { bytesRead = it } != -1) {
messageDigest.update(buffer, 0, bytesRead)
}
inputStream.close()

// Convert the digest to a hexadecimal string
val hashBytes = messageDigest.digest()
return hashBytes.joinToString("") { "%02x".format(it) }
} catch (e: Exception) {
e.printStackTrace()
return null
}
}

fun validateFileChecksum(file: File, expectedChecksum: String, algorithm: String = "SHA-256"): Boolean {
val generatedChecksum = generateFileChecksum(file, algorithm)
return generatedChecksum != null && generatedChecksum.equals(expectedChecksum, ignoreCase = true)
}

fun checkFileIntegrity(filePath: String, expectedChecksum: String) {
val file = File(filePath)

if (file.exists()) {
val isValid = validateFileChecksum(file, expectedChecksum, "SHA-256")
if (isValid) {
Log.d("ChecksumValidation", "File is valid! Checksum matches.")
} else {
Log.e("ChecksumValidation", "File checksum mismatch! Possible tampering detected.")
}
} else {
Log.e("ChecksumValidation", "File does not exist: $filePath")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ class SignatureComparison {
return false
}

// Compute SHA-1 hash of the first signature
val appSignatureHash = getSHA1Hash(signatures[0].toByteArray())
if (appSignatureHash.isNullOrEmpty()) {
Log.e(TAG, "Error generating hash for the app's signature.")
return false
}

val md = MessageDigest.getInstance("SHA-1");
val originalDigest = md.digest(expectedSignatureHash.toByteArray())

val currentDigest = md.digest(signatures[0].toByteArray())


// Compare with the expected signature hash
val isValid = expectedSignatureHash.equals(appSignatureHash, ignoreCase = true)
val isValid = MessageDigest.isEqual(originalDigest, currentDigest)
if (isValid) {
Log.d(TAG, "App signature is valid.")
} else {
Expand All @@ -56,21 +56,4 @@ class SignatureComparison {
}
}


private fun getSHA1Hash(signatureBytes: ByteArray): String? {
try {
val digest = MessageDigest.getInstance("SHA-1")
val hash = digest.digest(signatureBytes)

// Convert hash to a hexadecimal string
val hexString = StringBuilder()
for (b in hash) {
hexString.append(String.format("%02x", b))
}
return hexString.toString()
} catch (e: Exception) {
Log.e(TAG, "Error generating SHA-1 hash", e)
return null
}
}
}

0 comments on commit 0ee281b

Please sign in to comment.