From 83e3d7975041e301559207a696078cd32462ef4a Mon Sep 17 00:00:00 2001 From: Youssef El Saadany Date: Sat, 22 Jul 2023 17:13:41 +0200 Subject: [PATCH] added postData to android app --- passport-reader/app/build.gradle | 1 + .../tananaev/passportreader/MainActivity.kt | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/passport-reader/app/build.gradle b/passport-reader/app/build.gradle index b2472c886..a2f379437 100644 --- a/passport-reader/app/build.gradle +++ b/passport-reader/app/build.gradle @@ -46,6 +46,7 @@ dependencies { implementation 'com.github.mhshams:jnbis:1.1.0' implementation 'org.bouncycastle:bcpkix-jdk15on:1.65' // do not update implementation 'commons-io:commons-io:2.8.0' + implementation 'com.squareup.okhttp3:okhttp:4.9.0' googleImplementation platform('com.google.firebase:firebase-bom:31.0.0') googleImplementation 'com.google.firebase:firebase-analytics-ktx' googleImplementation 'com.google.firebase:firebase-crashlytics' diff --git a/passport-reader/app/src/main/java/com/tananaev/passportreader/MainActivity.kt b/passport-reader/app/src/main/java/com/tananaev/passportreader/MainActivity.kt index 509a893fa..c6cc3d13f 100644 --- a/passport-reader/app/src/main/java/com/tananaev/passportreader/MainActivity.kt +++ b/passport-reader/app/src/main/java/com/tananaev/passportreader/MainActivity.kt @@ -77,6 +77,11 @@ import com.google.gson.Gson; import java.security.PublicKey import java.security.spec.X509EncodedKeySpec import javax.crypto.Cipher +import okhttp3.MediaType.Companion.toMediaType +import okhttp3.OkHttpClient +import okhttp3.Request +import okhttp3.RequestBody.Companion.toRequestBody +import java.io.IOException class Response(json: String) : JSONObject(json) { val type: String? = this.optString("type") @@ -100,6 +105,30 @@ abstract class MainActivity : AppCompatActivity() { private lateinit var mainLayout: View private lateinit var loadingLayout: View + data class Data(val id: String, val digest: String, val signature: String, val publicKey: String) + + fun postData(id: String, digest: String, signature: String, publicKey: String) { + val client = OkHttpClient() + + val data = Data(id, digest, signature, publicKey) + val gson = Gson() + val json = gson.toJson(data) + + val mediaType = "application/json; charset=utf-8".toMediaType() + val body = json.toRequestBody(mediaType) + + val request = Request.Builder() + .url("http://yourserver.com/api/endpoint") + .post(body) + .build() + + client.newCall(request).execute().use { response -> + if (!response.isSuccessful) throw IOException("Unexpected code $response") + + println(response.body?.string()) + } + } + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) @@ -357,6 +386,9 @@ abstract class MainActivity : AppCompatActivity() { Log.d(TAG, "sodFile.encryptedDigest: ${sodFile.encryptedDigest}") Log.d(TAG, "sodFile.encryptedDigest: ${gson.toJson(sodFile.encryptedDigest)}") Log.d(TAG, "sodFile.encryptedDigest: ${gson.toJson(sodFile.encryptedDigest.joinToString("") { "%02x".format(it) })}") + val publicKey: PublicKey = sodFile.docSigningCertificate.publicKey as PublicKey + + postData("69", gson.toJson(sodFile.eContent.joinToString("") { "%02x".format(it) }), gson.toJson(sodFile.encryptedDigest.joinToString("") { "%02x".format(it) }), sodFile.docSigningCertificate.publicKey.toString()) Log.d(TAG, "============LET'S VERIFY THE SIGNATURE=============")