ArdaCode uygulamaları çalışma anında yeni sürüm olup olmadığını /api/v1/update-check uç noktasından sorgular, yanıtın HMAC-SHA256 imzasını doğrular ve yeni sürüm mevcutsa kullanıcıya şeffaf güncelleme bildirimi gösterir.
Her yanıt X-ArdaCode-Signature başlığı ile HMAC-SHA256 imzalıdır; araya girme (MITM) manipülasyonlarını engeller.
İndirilen APK dosyasının SHA-256 özeti kurulum tetiklenmeden önce cihazda hesaplanıp sunucu manifestiyle karşılaştırılır.
Kullanıcı onayı olmadan sessiz kurulum varsayılmaz; Android sistem PackageInstaller onay diyaloğu ve FileProvider kullanılır.
sdk/android/ArdaCodeUpdateClient.kt — Coroutines + SHA-256 + FileProvider Kullanıcı Onaylı Kurulum
package tr.com.ardacode.updatesdk
import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.provider.Settings
import androidx.core.content.FileProvider
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.json.JSONObject
import java.io.File
import java.net.HttpURLConnection
import java.net.URL
import java.security.MessageDigest
import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec
data class UpdateRelease(
val versionCode: Int,
val versionName: String,
val isMandatory: Boolean,
val sha256: String,
val certSha256: String,
val releaseNotes: String,
val downloadUrl: String,
val storePageUrl: String
)
object ArdaCodeUpdateClient {
private const val BASE_ENDPOINT = "https://apps.ardacode.com.tr/api/v1/update-check"
/**
* 1. Güvenli sürüm sorgulama ve HMAC-SHA256 yanıt imza kontrolü
*/
suspend fun checkForUpdate(
packageName: String,
currentVersionCode: Int,
hmacVerificationKeyHex: String? = null
): UpdateRelease? = withContext(Dispatchers.IO) {
val uri = "$BASE_ENDPOINT?packageName=$packageName&versionCode=$currentVersionCode&sdk=${Build.VERSION.SDK_INT}"
val conn = (URL(uri).openConnection() as HttpURLConnection).apply {
requestMethod = "GET"
connectTimeout = 8000
readTimeout = 8000
setRequestProperty("Accept", "application/json")
}
if (conn.responseCode != 200) return@withContext null
val rawBody = conn.inputStream.bufferedReader().use { it.readText() }
// İsteğe bağlı HMAC-SHA256 sunucu imza doğrulaması
if (!hmacVerificationKeyHex.isNullOrBlank()) {
val headerSig = conn.getHeaderField("X-ArdaCode-Signature")?.removePrefix("sha256=") ?: return@withContext null
val expectedSig = hmacSha256(hmacVerificationKeyHex, rawBody)
if (!MessageDigest.isEqual(headerSig.toByteArray(), expectedSig.toByteArray())) {
throw SecurityException("Güncelleme yanıt imzası doğrulanamadı!")
}
}
val json = JSONObject(rawBody)
if (!json.optBoolean("updateAvailable", false)) return@withContext null
val rel = json.getJSONObject("latestRelease")
UpdateRelease(
versionCode = rel.getInt("versionCode"),
versionName = rel.getString("versionName"),
isMandatory = rel.getBoolean("isMandatory"),
sha256 = rel.getString("sha256"),
certSha256 = rel.optString("certificateSha256", ""),
releaseNotes = rel.getString("releaseNotes"),
downloadUrl = rel.getString("downloadUrl"),
storePageUrl = rel.getString("storePageUrl")
)
}
/**
* 2. İndirilen APK dosyasının kurulum öncesi SHA-256 bütünlük doğrulaması
*/
fun verifyApkSha256(apkFile: File, expectedSha256: String): Boolean {
val digest = MessageDigest.getInstance("SHA-256")
apkFile.inputStream().buffered().use { input ->
val buf = ByteArray(64 * 1024)
var read: Int
while (input.read(buf).also { read = it } != -1) {
digest.update(buf, 0, read)
}
}
val actualHex = digest.digest().joinToString("") { "%02x".format(it) }
return actualHex.equals(expectedSha256, ignoreCase = true)
}
/**
* 3. KULLANICI ONAYLI KURULUM BAŞLATICI
* Sessiz arka plan kurulumu varsayılmaz; kullanıcıya resmi Android kurulum diyaloğu açılır.
*/
fun promptUserConsentInstall(activity: Activity, verifiedApkFile: File, expectedSha256: String) {
check(verifyApkSha256(verifiedApkFile, expectedSha256)) {
"SHA-256 bütünlük hatası: Bozuk veya değiştirilmiş APK kurulumu engellendi."
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && !activity.packageManager.canRequestPackageInstalls()) {
// Kullanıcıyı açık izin ekranına yönlendir
val permIntent = Intent(
Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES,
Uri.parse("package:${activity.packageName}")
)
activity.startActivity(permIntent)
return
}
val contentUri = FileProvider.getUriForFile(
activity,
"${activity.packageName}.fileprovider",
verifiedApkFile
)
val installIntent = Intent(Intent.ACTION_VIEW).apply {
setDataAndType(contentUri, "application/vnd.android.package-archive")
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_ACTIVITY_NEW_TASK)
}
activity.startActivity(installIntent)
}
private fun hmacSha256(hexKey: String, data: String): String {
val keyBytes = hexKey.chunked(2).map { it.toInt(16).toByte() }.toByteArray()
val mac = Mac.getInstance("HmacSHA256")
mac.init(SecretKeySpec(keyBytes, "HmacSHA256"))
return mac.doFinal(data.toByteArray(Charsets.UTF_8)).joinToString("") { "%02x".format(it) }
}
}