<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Rodrigo Silva</title>
    <description>The latest articles on Forem by Rodrigo Silva (@rodrassilva).</description>
    <link>https://forem.com/rodrassilva</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F144104%2Faccedc02-02c1-4c98-a808-e9cb4a323ef0.jpeg</url>
      <title>Forem: Rodrigo Silva</title>
      <link>https://forem.com/rodrassilva</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/rodrassilva"/>
    <language>en</language>
    <item>
      <title>Kotlin in the backend, Spring with Kotlin Part 2 — Exception Handling</title>
      <dc:creator>Rodrigo Silva</dc:creator>
      <pubDate>Thu, 01 Apr 2021 20:08:31 +0000</pubDate>
      <link>https://forem.com/rodrassilva/kotlin-in-the-backend-spring-with-kotlin-part-2-exception-handling-3fde</link>
      <guid>https://forem.com/rodrassilva/kotlin-in-the-backend-spring-with-kotlin-part-2-exception-handling-3fde</guid>
      <description>&lt;p&gt;Handling exceptions is a crucial part when developing any sort of application, certain errors occur by calling a method of a library with invalid arguments, others happen just for doing something wrong. However, some exceptions can be thrown by ourselves because what was asked to be done by us is not possible.&lt;/p&gt;

&lt;p&gt;In the previous post, we developed a simple Web API using Kotlin and Spring, this API allowed us to request and alter information about books in our system, however, when errors are thrown, the information sent to the API client is not the clearest, who becomes unaware of what really went wrong.&lt;/p&gt;

&lt;p&gt;Rest of this article:&lt;br&gt;
&lt;a href="https://rodrigo-silva96.medium.com/kotlin-in-the-backend-spring-with-kotlin-part-2-exception-handling-55524414e154"&gt;https://rodrigo-silva96.medium.com/kotlin-in-the-backend-spring-with-kotlin-part-2-exception-handling-55524414e154&lt;/a&gt;&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>spring</category>
      <category>backend</category>
      <category>exceptions</category>
    </item>
    <item>
      <title>Kotlin in the backend, Spring with Kotlin</title>
      <dc:creator>Rodrigo Silva</dc:creator>
      <pubDate>Tue, 23 Mar 2021 16:28:57 +0000</pubDate>
      <link>https://forem.com/rodrassilva/kotlin-in-the-backend-spring-with-kotlin-52ah</link>
      <guid>https://forem.com/rodrassilva/kotlin-in-the-backend-spring-with-kotlin-52ah</guid>
      <description>&lt;p&gt;For those of you who don’t know, Kotlin is a statically typed programming language designed by Jetbrains, it’s the official language for the development of native android applications, but what you may not know is that you can use Kotlin to develop your server-side applications.&lt;br&gt;
There are many libraries and frameworks which allow you to create your backend using Kotlin, such as Ktor and Spring.&lt;br&gt;
In this article, we will take a look into using Kotlin together with the Spring framework.&lt;/p&gt;

&lt;p&gt;Rest of this article:&lt;br&gt;
&lt;a href="https://rodrigo-silva96.medium.com/kotlin-in-the-backend-spring-with-kotlin-7a6b25e3a771"&gt;https://rodrigo-silva96.medium.com/kotlin-in-the-backend-spring-with-kotlin-7a6b25e3a771&lt;/a&gt;&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>spring</category>
      <category>software</category>
      <category>programming</category>
    </item>
    <item>
      <title>Android: Repository pattern using Room, Retrofit and Coroutines</title>
      <dc:creator>Rodrigo Silva</dc:creator>
      <pubDate>Sun, 07 Feb 2021 21:06:07 +0000</pubDate>
      <link>https://forem.com/rodrassilva/android-repository-pattern-using-room-retrofit-and-coroutines-58kb</link>
      <guid>https://forem.com/rodrassilva/android-repository-pattern-using-room-retrofit-and-coroutines-58kb</guid>
      <description>&lt;p&gt;The repository pattern is an abstraction used to hide the multiple data sources we may have in our application, data in an application may come from an internal database, or, an external service such as a Web API.&lt;br&gt;
This pattern is adopted and widely used when developing Android applications, it's also the recommended approach to creating an application.&lt;br&gt;
The following diagram displays a generic mobile application architecture on Android.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fyxk8akawka6rctphk0cq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fyxk8akawka6rctphk0cq.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What we can take from this diagram is the following:&lt;br&gt;
Our Activity/Fragment may or may not have one, or multiple instances of different ViewModels, each view model has a dependency to a specific repository, this repository can be shared by multiple view models.&lt;br&gt;
The repository knows the data sources from where to retrieve information, in this case, the repository knows the Model, which is Room, a layer on top of SQLite, and a service interface, which is provided by Retrofit in order to communicate to a web service.&lt;br&gt;
Each layer only knows the layer below. The ViewModel doesn't know who the repository interacts with.&lt;br&gt;
Let's give a concrete implementation example.&lt;br&gt;
We will start with a sample activity, an EditText to write a reminder and a button to add the reminder.&lt;/p&gt;

&lt;h3&gt;
  
  
  CreateReminderActivity
&lt;/h3&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

class CreateReminderActivity : AppCompatActivity() {

    val viewModel: CreateReminderViewModel by lazy {
        val app = application as ReminderApp
        val viewModelProviderFactory =
            CreateReminderViewModelProviderFactory(
                app,
                intent
            )
        ViewModelProvider(
            this,
            viewModelProviderFactory
        )[CreateReminderViewModel::class.java]
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.createreminderactivity)

        val reminderEditText: EditText = findViewById(R.id.reminderEditTextView)
        val createReminderButton: Button = findViewById(R.id.createReminderButton)


        createReminderButton.setOnClickListener {
            createReminder(
                text = reminderEditText.text.toString()
            )
        }
    }

    private fun createReminder(text: String) {
        if (text.isEmpty()) {
            showToast(message = "Reminder text field is empty")
        } else {
            viewModel.createReminder(text = text)
        }
    }

    private fun showToast(message: String) {
        ...
    }
}



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  ReminderApp
&lt;/h3&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

class ReminderApp : Application() {

    companion object {
        lateinit var retrofit: Retrofit
        lateinit var reminderDb: ReminderDb
    }

    override fun onCreate() {
        super.onCreate()
        reminderDb = Room
            .databaseBuilder(
                applicationContext,
                ReminderDb::class.java,
                "Reminder-Db"
            )
            .build()

        retrofit = Retrofit.Builder()
            .baseUrl(REMINDERS_API_HOST)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  ViewModelFactory 
&lt;/h3&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

class CreateReminderViewModelProviderFactory(val app: ReminderApp, val intent: Intent) :
    ViewModelProvider.Factory {
    override fun &amp;lt;T : ViewModel?&amp;gt; create(modelClass: Class&amp;lt;T&amp;gt;): T {

        val reminderDao = ReminderApp.reminderDb.reminderDao()
        val reminderService = ReminderApp.retrofit.create(ReminderService::class.java)
        val reminderRepository = ReminderRepository(
            reminderDao = reminderDao,
            reminderService = reminderService
        )
        val viewModel = CreateReminderViewModel(reminderRepository)
        return viewModel as T
    }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  ViewModel
&lt;/h3&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

class CreateReminderViewModel(private val reminderRepository: ReminderRepository) : ViewModel() {

    fun createReminder(text: String) =
        viewModelScope.launch {
            reminderRepository.createReminder(text = text)
        }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;code&gt;createReminder&lt;/code&gt; uses viewModelScope in order to launch a coroutine.&lt;br&gt;
Any coroutine launched in this scope is automatically cancelled if the ViewModel is cleared. Coroutines are useful here for when you have work that needs to be done only if the ViewModel is active. For example, if you are computing some data for a layout, you should scope the work to them ViewModel so that if the ViewModel is cleared, the work is cancelled automatically to avoid consuming resources.&lt;/p&gt;

&lt;h3&gt;
  
  
  Repository
&lt;/h3&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

class ReminderRepository(
    private val reminderService: ReminderService,
    private val reminderDao: ReminderDao
) {

    suspend fun getReminders(): LiveData&amp;lt;List&amp;lt;Programme&amp;gt;&amp;gt; = liveData {
        emitSource(reminderDao.getReminders())
        val reminders = reminderService.getReminders().toReminders()
        reminderDao.createReminder(*reminders.toTypedArray())
    }
    ...

    suspend fun createReminders(text: String): Reminder {
        val reminderToBeCreated = ReminderInputModelDto(text = text)
        val reminder = reminderService.createReminder(reminderToBeCreated).toReminder()
        reminderDao.createReminder(reminder)
        return reminder
    }


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  ReminderDao(Room)
&lt;/h3&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

@Dao
interface ReminderDao {

    @Query("SELECT * FROM REMINDER")
    fun getAllReminders(): LiveData&amp;lt;List&amp;lt;Reminder&amp;gt;&amp;gt;

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    suspend fun createReminder(vararg reminders: Reminder)

}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  ReminderService(Retrofit)
&lt;/h3&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

interface ReminderService {

    @GET("reminders")
    suspend fun getReminders(): RemindersOutputModelDto

    @POST("reminders")
    suspend fun createReminder(@Body reminder: ReminderInputModelDto): ReminderOutputModelDto

}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This article tries to give a possible implementation of the recommended approach when designing an Android application.&lt;/p&gt;

</description>
      <category>android</category>
      <category>kotlin</category>
      <category>retrofit</category>
      <category>room</category>
    </item>
    <item>
      <title>Message Integrity, Authentication, and Non-Repudiation</title>
      <dc:creator>Rodrigo Silva</dc:creator>
      <pubDate>Sat, 03 Oct 2020 19:10:18 +0000</pubDate>
      <link>https://forem.com/rodrassilva/message-integrity-authentication-and-non-repudiation-3d98</link>
      <guid>https://forem.com/rodrassilva/message-integrity-authentication-and-non-repudiation-3d98</guid>
      <description>&lt;p&gt;So let's say you send messages to your friends through your own personal communication channel, you won't send confidential information so you really don't care if someone else sees it, however, you want to receive exactly what your friends send to you, you don't want to receive something else. And sometimes, you really want to guarantee that it was that specific friend who sent you that message.&lt;/p&gt;

&lt;p&gt;The code used in the examples was written in &lt;code&gt;Java&lt;/code&gt;, using the &lt;code&gt;JCA&lt;/code&gt; (Java Cryptography Architecture).&lt;/p&gt;

&lt;h2&gt;
  
  
  What's integrity?
&lt;/h2&gt;

&lt;p&gt;Integrity is the absence of an invalid system or data modifications by non-authorized parties.&lt;/p&gt;

&lt;p&gt;How can we guarantee the integrity of the data that we sent through our communication channel?&lt;/p&gt;

&lt;p&gt;Let's split the procedure into two major parts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The sender should protect the data to send;&lt;/li&gt;
&lt;li&gt;The receiver must verify if the received data was not tampered.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Integrity
&lt;/h2&gt;

&lt;p&gt;Let's say we just want to guarantee that the data was no tampered(we don't care about the identity of the sender for now).&lt;/p&gt;

&lt;p&gt;Let's introduce the idea of hash functions.&lt;/p&gt;

&lt;p&gt;A.k.a. cryptographic hash functions or collision-resistant hash functions&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Do not confuse with hash functions used in hash tables data structures;&lt;/li&gt;
&lt;li&gt;They are cryptographic, but not cyphers.

&lt;ul&gt;
&lt;li&gt;Are not used to encrypt;&lt;/li&gt;
&lt;li&gt;Example: SHA2.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hashes don't require a key, as we can see in the following code, we are using &lt;code&gt;SHA-256&lt;/code&gt;, which will generate a hash with a fixed size(256 bits).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="nf"&gt;hash&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;dataBytes&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;NoSuchAlgorithmException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;MessageDigest&lt;/span&gt; &lt;span class="n"&gt;sha256&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;MessageDigest&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getInstance&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"SHA-256"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;        
    &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;dataHash&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sha256&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;digest&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dataBytes&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;dataHash&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After generating the hash, what the sender should do is send the message with the hash.&lt;/p&gt;

&lt;p&gt;Let's say M is the data, and H is the hash of M.&lt;br&gt;
The sender should send MH through the communication channel.&lt;/p&gt;

&lt;p&gt;The receiver on the other end received M'H. M' means the received message bytes, which could have been tampered.&lt;/p&gt;

&lt;p&gt;The receiver should hash M', generating H', then he should compare H with H', if they both match, the message was not tampered.&lt;/p&gt;
&lt;h2&gt;
  
  
  Authentication
&lt;/h2&gt;

&lt;p&gt;If we want to guarantee authentication, there must be a certain something which can authenticate the sender of a message.&lt;/p&gt;

&lt;p&gt;That something is a symmetric key, a key which can be used to cypher and decypher in symmetrical ciphering algorithms.&lt;/p&gt;

&lt;p&gt;In the previous example, we used a hash, which didn't require any sort of key, however, now we want to authenticate the sender, and for this, we'll use a MAC (Message Authentication Code).&lt;/p&gt;

&lt;p&gt;As in the previous example, we send the message + MAC.&lt;/p&gt;

&lt;p&gt;For this, we'll use a Hash-based MAC (hash of data and secret value).&lt;br&gt;
It requires only a hash – more efficient&lt;br&gt;
– Example: HMAC-SHA2&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="nf"&gt;mac&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;dataBytes&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;NoSuchAlgorithmException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="nc"&gt;Key&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;getKey&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;keyFilePath&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
      &lt;span class="c1"&gt;// get a MAC object and print the provider&lt;/span&gt;
      &lt;span class="nc"&gt;Mac&lt;/span&gt; &lt;span class="n"&gt;mac&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Mac&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getInstance&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"HmacSHA256"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;            
      &lt;span class="c1"&gt;//Creating Mac ...&lt;/span&gt;
      &lt;span class="n"&gt;mac&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;init&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;mac&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;doFinal&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dataBytes&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's say M is the data, and H is the MAC of M, using the key K. &lt;br&gt;
The sender should send MH through the communication channel, the receiver on the other end received M'H. M' means the received message bytes, which could have been tampered.&lt;/p&gt;

&lt;p&gt;The receiver should produce the MAC of M', using the same key K, generating H', then he should compare H with H', if they both match, the message was not tampered.&lt;/p&gt;

&lt;p&gt;With this, we can authenticate the user, and check if the sent data was not changed.&lt;/p&gt;
&lt;h2&gt;
  
  
  Non-repudian
&lt;/h2&gt;

&lt;p&gt;We want to guarantee all the previous properties, and also, guarantee that the sender cannot say that he wasn't the one who sent that message.&lt;br&gt;
So we want:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Authenticate the content of a document;&lt;/li&gt;
&lt;li&gt;Authenticate its signer;&lt;/li&gt;
&lt;li&gt;Being able to assure authentication towards a third party;&lt;/li&gt;
&lt;li&gt;Signer cannot repudiate the signature.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With this comes the notion of Digital Signature.&lt;/p&gt;

&lt;p&gt;Digital Signature uses asymmetrical keys, a public key and a private key.&lt;/p&gt;

&lt;p&gt;The public key can be public and shared among other people, however, only the owner of the private key can have it.&lt;/p&gt;

&lt;p&gt;We use the private key to sign the hash of the data, which allows saying that only the owner of the private key could sign that data, meaning that that person can't deny signing.&lt;/p&gt;
&lt;h3&gt;
  
  
  Get Signature
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="nf"&gt;generateDigitalSignature&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;privateKeyFilePath&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;Key&lt;/span&gt; &lt;span class="n"&gt;privateKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;readPrivateKey&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;privateKeyFilePath&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="nc"&gt;Signature&lt;/span&gt; &lt;span class="n"&gt;signature&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Signature&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getInstance&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"SHA256withRSA"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;signature&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;initSign&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;privateKey&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;signature&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;update&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;sign&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;signature&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sign&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;sign&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The person who wishes to verify the integrity, authentication and signature of the received message, must know the public key, as shown in the following example : &lt;/p&gt;
&lt;h3&gt;
  
  
  Verify Signature
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt; &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;verifySignature&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;messageBytes&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;dataSignature&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;publicKeyFilePath&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;Key&lt;/span&gt; &lt;span class="n"&gt;publicKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;readPublicKey&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;publicKeyFilePath&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="nc"&gt;Signature&lt;/span&gt; &lt;span class="n"&gt;signature&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Signature&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getInstance&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"SHA256withRSA"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;signature&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;initVerify&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;publicKey&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;signature&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;update&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;messageBytes&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;signature&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;verify&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dataSignature&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;The are many properties we may want to preserve while sending messages through the many communication channels. They are usually incremental, which means if we want to add a new property, we usually guarantee the previous ones.&lt;/p&gt;

&lt;p&gt;The following table is a summary of the whole post.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Cryptographic primitive | Hash |    MAC    | Digital
Security Goal           |      |           | signature
-----------------------------+------+-----------+-------------
Integrity               |  Yes |    Yes    |   Yes
Authentication          |  No  |    Yes    |   Yes
Non-repudiation         |  No  |    No     |   Yes
-----------------------------+------+-----------+-------------
Kind of keys            | none | symmetric | asymmetric
                        |      |    keys   |    keys
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This was my first post, so I hope that you enjoyed it! &lt;br&gt;
If you find something wrong please say so in the comments :).&lt;br&gt;
You can find me at &lt;a href="https://twitter.com/rodrasilva96"&gt;twitter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>java</category>
      <category>jca</category>
      <category>integrity</category>
      <category>security</category>
    </item>
  </channel>
</rss>
