<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Nishchal Sharma on Medium]]></title>
        <description><![CDATA[Stories by Nishchal Sharma on Medium]]></description>
        <link>https://medium.com/@nishchalsharma?source=rss-850ee4a73a92------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*SWJNfmeImamT927Dc5DMKA@2x.jpeg</url>
            <title>Stories by Nishchal Sharma on Medium</title>
            <link>https://medium.com/@nishchalsharma?source=rss-850ee4a73a92------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 10:43:06 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nishchalsharma/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[ How to Use SSL Securely in Android Applications]]></title>
            <link>https://medium.com/@nishchalsharma/how-to-use-ssl-securely-in-android-applications-0ae29723ff18?source=rss-850ee4a73a92------2</link>
            <guid isPermaLink="false">https://medium.com/p/0ae29723ff18</guid>
            <category><![CDATA[ssl-pinning]]></category>
            <category><![CDATA[mobile-app-security]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Nishchal Sharma]]></dc:creator>
            <pubDate>Sat, 07 Jun 2025 07:57:57 GMT</pubDate>
            <atom:updated>2025-06-07T08:03:25.889Z</atom:updated>
            <content:encoded><![CDATA[<p>As Android developers building banking or fintech apps, security isn’t optional — it’s a requirement. Among the most critical pieces of security is <strong>SSL/TLS</strong>, which ensures your app’s data in transit remains private and tamper-proof.</p><p>But here’s the catch: <strong>just using </strong><strong>https:// isn&#39;t enough.</strong> You need to enforce best practices, protect against man-in-the-middle attacks, and make it hard for attackers to intercept or spoof traffic.</p><p>This post will walk you through <strong>how to securely implement SSL in Android apps</strong>, with a focus on banking-level protection.</p><h3>🚨 Why SSL Is Critical in Banking Apps</h3><p>Banking apps transmit:</p><ul><li>User credential</li><li>Payment details</li><li>Account and transaction data</li></ul><p>A breach of that data can lead to <strong>identity theft</strong>, <strong>fraud</strong>, and <strong>regulatory penalties</strong>.</p><p><strong>SSL/TLS (Secure Sockets Layer / Transport Layer Security)</strong> encrypts this traffic and ensures:</p><ul><li>Data confidentiality</li><li>Server authenticity</li><li>Integrity of communication</li></ul><h3>✅ Step 1: Use HTTPS for All API Calls</h3><p><strong>Rule #1: Never use HTTP</strong>.</p><p>Android 9 (API 28) and above blocks HTTP by default, but for older devices, enforce this explicitly.</p><pre>&lt;application<br>    android:networkSecurityConfig=&quot;@xml/network_security_config&quot;&gt;</pre><h3>🛡️ Step 2: Create a Secure network_security_config.xml</h3><p>This config lets you:</p><ul><li>Block cleartext traffic</li><li>Trust only system or custom CAs</li><li>Enable SSL pinning</li></ul><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br>&lt;network-security-config&gt;<br>    &lt;base-config cleartextTrafficPermitted=&quot;false&quot;&gt;<br>        &lt;trust-anchors&gt;<br>            &lt;certificates src=&quot;system&quot; /&gt;<br>        &lt;/trust-anchors&gt;<br>    &lt;/base-config&gt;<br>&lt;domain-config&gt;<br>        &lt;domain includeSubdomains=&quot;true&quot;&gt;xyz.com&lt;/domain&gt;<br>        &lt;pin-set expiration=&quot;2026-01-01&quot;&gt;<br>            &lt;pin digest=&quot;SHA-256&quot;&gt;your_cert_public_key_hash==&lt;/pin&gt;<br>        &lt;/pin-set&gt;<br>    &lt;/domain-config&gt;<br>&lt;/network-security-config&gt;</pre><h3>🔐 Step 3: Implement SSL Pinning in Android</h3><h3>Use OkHttp + CertificatePinner</h3><pre>val certificatePinner = CertificatePinner.Builder()<br>    .add(&quot;xyz.com&quot;, &quot;sha256/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=&quot;)<br>    .build()<br><br>val client = OkHttpClient.Builder()<br>    .certificatePinner(certificatePinner)<br>    .build()<br>val request = Request.Builder()<br>    .url(https://medium.com/&quot;https://xyz.com/api/secure&quot;)<br>    .build()<br>client.newCall(request).enqueue(object : Callback {<br>    override fun onResponse(call: Call, response: Response) {<br>        // Handle secure response<br>    }<br>    override fun onFailure(call: Call, e: IOException) {<br>        // Handle failure<br>    }<br>})</pre><p>You can extract your SSL public key hash using openssl:</p><pre>openssl x509 -pubkey -noout -in certificate.pem | \<br>openssl pkey -pubin -outform DER | \<br>openssl dgst -sha256 -binary | \<br>openssl base64</pre><h3>🔄 Step 4: Secure Your API Communication with Retrofit</h3><h3>Add Retrofit and OkHttp:</h3><pre>implementation &#39;com.squareup.retrofit2:retrofit:2.9.0&#39;<br>implementation &#39;com.squareup.retrofit2:converter-gson:2.9.0&#39;</pre><h3>Set up Retrofit with SSL pinning:</h3><pre>val retrofit = Retrofit.Builder()<br>    .baseUrl(&quot;https://xyz.com/&quot;)<br>    .client(client)<br>    .addConverterFactory(GsonConverterFactory.create())<br>    .build()</pre><h3>🗱 Step 5: Avoid Insecure Practices</h3><p>❌ Never do this:</p><pre>val trustAllCerts = arrayOf&lt;TrustManager&gt;(object : X509TrustManager {<br>    override fun checkClientTrusted(...) {}<br>    override fun checkServerTrusted(...) {}<br>    override fun getAcceptedIssuers(): Array&lt;X509Certificate&gt; = arrayOf()<br>})</pre><p>This disables SSL validation entirely — a <em>huge security risk</em>.</p><h3>🔄 Bonus: Support TLS 1.2 on Legacy Devices</h3><p>For Android &lt; Lollipop, TLS 1.2 is disabled by default.</p><pre>if (Build.VERSION.SDK_INT &lt; Build.VERSION_CODES.LOLLIPOP) {<br>    val sslContext = SSLContext.getInstance(&quot;TLSv1.2&quot;)<br>    sslContext.init(null, null, null)<br>    val client = OkHttpClient.Builder()<br>        .sslSocketFactory(Tls12SocketFactory(sslContext.socketFactory), trustManager)<br>        .build()<br>}</pre><blockquote><em>You can use the </em><a href="https://gist.github.com/atooma/aa94ae6b69b8e3e8fcd10e8d3a5d6e3e"><em>Tls12SocketFactory</em></a><em> helper to force-enable TLS 1.2.</em></blockquote><h3>🔬 Testing Your SSL Implementation</h3><ul><li>Use <strong>Charles Proxy</strong> or <strong>Wireshark</strong> to inspect SSL behavior.</li><li>Use <a href="https://github.com/MobSF/Mobile-Security-Framework-MobSF"><strong>MobSF</strong></a><strong> </strong>to scan APKs for SSL/TLS issues.</li><li>Check for proper pinning and block MITM attempts.</li></ul><h3>✅ Final Checklist</h3><p>✅Item Status Only HTTPS used <br>✅ Cleartext blocked via network config<br>✅ SSL pinning implemented<br>✅ OkHttp + Retrofit secured <br>✅ TLS 1.2 support for older Android<br>✅ Insecure TrustManagers avoided</p><h3>🔺 Final Thoughts</h3><p>SSL is your first line of defense in keeping banking transactions secure. But it’s not just about <em>using HTTPS</em>. It’s about ensuring <strong>authenticity, encryption, and integrity</strong> — the cornerstones of safe digital banking.</p><p>By combining network_security_config, SSL pinning, Retrofit, and rigorous testing, you can ship Android banking apps that users (and regulators) can trust.</p><p>📝 <em>If you found this article helpful, follow me for more mobile security deep dives and Android development tips.</em></p><p>🔐 <em>Security is not a feature. It’s a foundation.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0ae29723ff18" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[## Efficiently Using Coroutines in Android with MVVM and Use Case Pattern Architecture]]></title>
            <link>https://medium.com/@nishchalsharma/efficiently-using-coroutines-in-android-with-mvvm-and-use-case-pattern-architecture-77754b93e82f?source=rss-850ee4a73a92------2</link>
            <guid isPermaLink="false">https://medium.com/p/77754b93e82f</guid>
            <category><![CDATA[android-development]]></category>
            <category><![CDATA[mvvm]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[coroutine]]></category>
            <dc:creator><![CDATA[Nishchal Sharma]]></dc:creator>
            <pubDate>Sat, 14 Sep 2024 08:29:51 GMT</pubDate>
            <atom:updated>2024-09-21T09:49:30.927Z</atom:updated>
            <content:encoded><![CDATA[<h3>Efficiently Using Coroutines in Android with MVVM and Use Case Pattern Architecture</h3><p>In modern Android app development, coroutines are a powerful tool for managing background tasks and asynchronous programming. When combined with the MVVM (Model-View-ViewModel) pattern and a clean architecture using the Use Case pattern, coroutines provide a clean, efficient, and maintainable way to handle asynchronous operations such as network requests or database queries.</p><p>This article will walk you through how to efficiently use coroutines in Android following the MVVM and Use Case pattern architecture.</p><p><strong>1. Understanding MVVM and Use Case Pattern Architecture</strong></p><ul><li>MVVM (Model-View-ViewModel) separates concerns into three parts:</li><li>Model: The data layer, which manages the business logic and interacts with the network or local storage.</li><li>View: The UI layer that interacts with the user, typically implemented via `Fragment` or `Activity`.</li><li>ViewModel: The middle layer that holds and processes data for the UI. It communicates with the Model and provides data for the View.</li><li>Use Case Pattern: This pattern introduces an additional layer between the ViewModel and the repository (or data source). Each use case represents a specific business rule or functionality. It helps enforce the Single Responsibility Principle and ensures that each class or function has a well-defined role.</li></ul><p><strong>2. Why Use Coroutines?</strong></p><p>Coroutines offer several advantages when dealing with background tasks such as:</p><ul><li>Simplifying complex async code.</li><li>Avoiding callback hell.</li><li>Lightweight and efficient threading compared to traditional `Thread` or `AsyncTask`.</li><li>Handling concurrency and long-running operations seamlessly.</li></ul><p>Coroutines work perfectly with MVVM and the Use Case pattern because they help avoid blocking the main thread while keeping the UI responsive.</p><p><strong>3. Setting Up Coroutines in an MVVM Architecture</strong></p><p><strong>Step 1: Add Coroutine Dependencies</strong></p><p>First, add the necessary dependencies to your `build.gradle`:</p><pre>dependencies {<br>implementation &quot;org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0&quot;<br>implementation &quot;org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.0&quot;<br>implementation &quot;androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0&quot;<br>}</pre><p><strong>Step 2: Implementing the Use Case</strong></p><p>Create a simple use case that encapsulates a specific functionality. For example, fetching a list of items from a repository:</p><pre>class FetchItemsUseCase(private val repository: ItemRepository) {<br>    suspend operator fun invoke(): List&lt;Item&gt; {<br>        return repository.getItems()<br>    }<br>}</pre><p>Here, `suspend` keyword tells the coroutine that this function is a long-running task and should be called within a coroutine context.</p><p><strong>Step 3: Implementing the Repository</strong></p><p>The repository interacts with the data source (e.g., a network or database) and is responsible for providing data to the use case.</p><pre>class ItemRepository(private val apiService: ApiService) {<br>    suspend fun getItems(): List&lt;Item&gt; {<br>        return apiService.fetchItems()<br>    }<br>}</pre><p>Here, `apiService.fetchItems()` is a network call that is performed asynchronously using coroutines.</p><p><strong>Step 4: ViewModel</strong></p><p>The `ViewModel` is responsible for fetching data from the use case and providing it to the View. Coroutines are usually launched in the `ViewModel` scope.</p><pre>class ItemsViewModel(private val fetchItemsUseCase: FetchItemsUseCase) : ViewModel() {<br>    private val _items = MutableLiveData&lt;List&lt;Item&gt;&gt;()<br>    val items: LiveData&lt;List&lt;Item&gt;&gt; get() = _items<br>    fun fetchItems() {<br>        viewModelScope.launch {<br>            try {<br>                val result = fetchItemsUseCase()<br>                _items.postValue(result)<br>            } catch (e: Exception) {<br>                // Handle the error, e.g., by showing a message in the UI }<br>            }<br>        }<br>    }</pre><p><strong>In this example:</strong></p><ul><li>`viewModelScope` is used to manage the coroutine’s lifecycle. It automatically cancels coroutines when the `ViewModel` is cleared to avoid memory leaks.</li><li>`launch {}` creates a coroutine that runs on the main thread, suitable for UI updates.</li><li>The `fetchItemsUseCase()` function is called within the coroutine, ensuring the network call is performed asynchronously.</li></ul><p><strong>Step 5: Connecting the View to the ViewModel</strong></p><p>In your `Fragment` or `Activity`, you observe the `LiveData` from the `ViewModel` to update the UI:</p><pre>class ItemsFragment : Fragment() {<br>    private lateinit var viewModel: ItemsViewModel<br>    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {<br>        super.onViewCreated(view, savedInstanceState)<br>        viewModel = ViewModelProvider(this).get(ItemsViewModel::class.java)<br>        viewModel.items.observe(viewLifecycleOwner) { items -&gt;<br>        // Update UI with the items list<br><br>        }<br>        viewModel.fetchItems() // Fetch items when the view is created<br>    }<br>}}</pre><p><strong>4. Using Dispatchers for Efficient Coroutine Management</strong></p><p>To efficiently manage background tasks like network or database operations, it’s essential to use the appropriate coroutine dispatcher. Kotlin coroutines provide several dispatchers:</p><ul><li>Dispatchers.Main: For UI-related tasks (like updating the UI).</li><li>Dispatchers.IO: For I/O operations, such as network requests and database interactions.</li><li>Dispatchers.Default: For CPU-intensive tasks.</li></ul><p><strong>In the example above</strong>, we’re using the `viewModelScope` which runs on `Dispatchers.Main` by default. However, when making a network request or reading from a database, you should explicitly switch to `Dispatchers.IO`:</p><pre>fun fetchItems() {<br>    viewModelScope.launch {<br>        try {<br>            val result = withContext(Dispatchers.IO) {<br>                fetchItemsUseCase()<br>            }<br>            _items.postValue(result)<br>        } catch (e: Exception) {<br>            // Handle the error<br>        }<br>    }<br>}</pre><p>Here, `withContext(Dispatchers.IO)` ensures that the use case is executed in the I/O thread, preventing any UI-blocking operations.</p><p><strong>5. Handling Errors with Coroutines</strong></p><p>Error handling in coroutines can be done using `try-catch` blocks. Additionally, coroutine scopes provide mechanisms to handle uncaught exceptions globally using `CoroutineExceptionHandler`:</p><pre>val exceptionHandler = CoroutineExceptionHandler { _, throwable -&gt;<br>// Handle the exception globally<br>    Log.e(&quot;ViewModel&quot;, &quot;Error: ${throwable.message}&quot;)<br>}<br>viewModelScope.launch(exceptionHandler) {<br>    val result = fetchItemsUseCase()<br>    _items.postValue(result)<br>}</pre><p><strong>6. Testing Coroutines with Use Case and MVVM Architecture</strong></p><p>Unit testing coroutines in a ViewModel or Use Case is straightforward. You can use `TestCoroutineDispatcher` and `TestCoroutineScope` to control coroutine execution in tests.</p><p>For example, testing a `ViewModel`:</p><pre>@Test<br>fun `test fetchItems success`() = runBlockingTest {<br>    // Mock the repository response<br>    val mockItems = listOf(Item(&quot;Test Item&quot;))<br>    whenever(mockRepository.getItems()).thenReturn(mockItems)<br>    // Call the ViewModel function<br>    viewModel.fetchItems()<br>    // Assert the result<br>    assertEquals(mockItems, viewModel.items.getOrAwaitValue())<br>}</pre><p>In this test, `runBlockingTest` ensures the coroutine executes immediately, making it easy to test ViewModel logic.</p><p><strong>Conclusion</strong></p><p>Using coroutines with MVVM and Use Case pattern architecture makes your Android app’s architecture more maintainable, efficient, and robust. Coroutines help manage background tasks without blocking the main thread, while the Use Case pattern promotes clean code that separates business logic from UI logic.</p><p>By structuring your app with these principles, you can ensure that your code is scalable, testable, and easier to debug and maintain.</p><blockquote><strong>If you enjoyed this article, remember to like, share, and share your opinions in the comments!</strong></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=77754b93e82f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>