<?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: Vitor Vargas</title>
    <description>The latest articles on Forem by Vitor Vargas (@vitorvargas).</description>
    <link>https://forem.com/vitorvargas</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%2F1205864%2Fb5890d4d-2a62-4760-99f5-8b2b43c2cd02.jpeg</url>
      <title>Forem: Vitor Vargas</title>
      <link>https://forem.com/vitorvargas</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/vitorvargas"/>
    <language>en</language>
    <item>
      <title>Implementing symmetric and asymmetric encryption with NodeJS</title>
      <dc:creator>Vitor Vargas</dc:creator>
      <pubDate>Wed, 15 May 2024 15:48:55 +0000</pubDate>
      <link>https://forem.com/superviz/implementing-symmetric-and-asymmetric-encryption-with-nodejs-4efp</link>
      <guid>https://forem.com/superviz/implementing-symmetric-and-asymmetric-encryption-with-nodejs-4efp</guid>
      <description>&lt;p&gt;In a digital world, where data is transmitted everywhere, concerns about where this data ends up are growing fast. Many companies offer services without encryption, leaving sensitive data exposed to unnecessary risks. Every year, we witness personal data leaks, and that is why at &lt;a href="https://superviz.com/" rel="noopener noreferrer"&gt;SuperViz&lt;/a&gt;, we have done work to guarantee the security and privacy of our users' data.&lt;/p&gt;

&lt;p&gt;After much study and being responsible for implementing it internally in the company, I would like to share this article to help reduce such incidents. At the end of the article, you will be able to create encryptions using symmetric keys and using asymmetric keys.&lt;/p&gt;

&lt;h2&gt;
  
  
  Asymmetric and Symmetric Keys
&lt;/h2&gt;

&lt;p&gt;Before diving into encryption, it's extremely valuable to know the difference between asymmetric and symmetric keys.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Symmetric Encryption&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Uses a single key for both encrypting and decrypting data.&lt;/li&gt;
&lt;li&gt;The same key is shared between the sender and the receiver of the message.&lt;/li&gt;
&lt;li&gt;It is faster and more efficient in terms of processing than asymmetric encryption.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Asymmetric Encryption&lt;/strong&gt; (or public key encryption):

&lt;ul&gt;
&lt;li&gt;Uses a pair of keys: a public key and a private key;&lt;/li&gt;
&lt;li&gt;The public key is used to encrypt the data, while the private key is used to decrypt the data;&lt;/li&gt;
&lt;li&gt;Each recipient has their own private key and a public key, with only the public key being shared with other interested parties;&lt;/li&gt;
&lt;li&gt;It is more secure than symmetric encryption due to the separation of public and private keys;&lt;/li&gt;
&lt;li&gt;It is slower and requires more computational resources than symmetric encryption.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;In summary, the main difference between symmetric and asymmetric encryption lies in how the keys are generated and used to encrypt and decrypt data, as well as in the relative efficiency and security of each approach.&lt;/p&gt;

&lt;h2&gt;
  
  
  Encrypting with Symmetric Keys
&lt;/h2&gt;

&lt;p&gt;In this article we will use the &lt;code&gt;AES-256-CBC&lt;/code&gt; encryption, with &lt;code&gt;CBC&lt;/code&gt; being the acronym for Cipher Block Chaining.&lt;/p&gt;

&lt;p&gt;CBC is a way of encrypting information. Imagine you have a message that you want to keep secure, with CBC it works like this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Divide the message into small pieces, called blocks;&lt;/li&gt;
&lt;li&gt;Instead of simply encrypting each block separately, as is the case in some methods, in CBC each block is mixed with the previous block before being encrypted. It's as if each block depended on what came before it;&lt;/li&gt;
&lt;li&gt;This creates a kind of "chain" of blocks, where each block is linked to the previous one;&lt;/li&gt;
&lt;li&gt;This link between the blocks makes it more difficult for someone who does not have the correct key to decipher the message. Even if someone tries to tamper with one block, it will affect all subsequent blocks, making decipherment much more difficult.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Knowing how CBC works, let's create our first message encrypted with &lt;code&gt;AES-256-CBC&lt;/code&gt;! Imagine that we are going to create a function called &lt;code&gt;encrypt&lt;/code&gt;, it will be responsible for creating the symmetric key and performing the encryption, it will have the following parameters: &lt;code&gt;salt&lt;/code&gt;, &lt;code&gt;passphrase&lt;/code&gt;, &lt;code&gt;text&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;passphrase&lt;/code&gt; will be required to use in encryption to decrypt the content. Additionally, we will need a &lt;code&gt;salt&lt;/code&gt;, which is a random text used as a basis for encrypting the data. This &lt;code&gt;salt&lt;/code&gt; will increase the security of the encryption, making it more difficult for third parties to decipher protected content.&lt;/p&gt;

&lt;p&gt;In the first line of the function, we will generate the symmetric key. This key will be the only key used to both encrypt and decrypt the content. It will be shared between the sender and the recipient.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;crypto&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;crypto&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;salt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;randomBytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hex&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;passphrase&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;password&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;encrypt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;passphrase&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;salt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;scryptSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;passphrase&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;salt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In line 7, we create our symmetric key. In the next line, we will need to generate the &lt;code&gt;Initialization Vector&lt;/code&gt;. The Initialization Vector (IV) is like a secret ingredient in encryption, it is a random value that joins with the key to scramble the data, which makes encryption more secure and difficult to break.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;iv&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;crypto&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;randomBytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Now let's encrypt the message using our &lt;code&gt;key&lt;/code&gt; and &lt;code&gt;iv&lt;/code&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We will create our cipher passing 3 parameters: the algorithm, the &lt;code&gt;key&lt;/code&gt; and the &lt;code&gt;iv&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;We add the text to the cipher and change the text format from &lt;code&gt;utf-8&lt;/code&gt; to &lt;code&gt;hex&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;We obtain the final encrypted content.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cipher&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;crypto&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createCipheriv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;aes-256-cbc&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;iv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;encrypted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;cipher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;utf8&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hex&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nx"&gt;encrypted&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;cipher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;final&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hex&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are approaching the end, but a challenge arises. To decrypt the message content, we need the IV (Initialization Vector), however, it is crucial that each IV is unique and random. Storing the IV alongside the encrypted message would compromise security. A commonly adopted solution is to merge the IV with the encrypted message in strategic locations, keeping its position confidential.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;part1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;encrypted&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;17&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;part2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;encrypted&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;17&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;part1&lt;/span&gt;&lt;span class="p"&gt;}${&lt;/span&gt;&lt;span class="nx"&gt;iv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hex&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)}${&lt;/span&gt;&lt;span class="nx"&gt;part2&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is important to point out that, in parts 1 and 2, our encrypted text contains only 32 characters.&lt;/p&gt;

&lt;p&gt;And in the end, our encryption function looked like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;crypto&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;crypto&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;salt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;crypto&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;randomBytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hex&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;passphrase&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;password&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;encrypt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;passphrase&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;salt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;crypto&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;scryptSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;passphrase&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;salt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;iv&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;crypto&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;randomBytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cipher&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;crypto&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createCipheriv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;aes-256-cbc&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;iv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;encrypted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;cipher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;utf8&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hex&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="nx"&gt;encrypted&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;cipher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;final&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hex&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;part1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;encrypted&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;17&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;part2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;encrypted&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;17&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;part1&lt;/span&gt;&lt;span class="p"&gt;}${&lt;/span&gt;&lt;span class="nx"&gt;iv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hex&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)}${&lt;/span&gt;&lt;span class="nx"&gt;part2&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Decrypting with Symmetric Keys
&lt;/h2&gt;

&lt;p&gt;Now that we have our content encrypted at some point, we will need to decrypt the content. Some steps will be like what we did when encrypting the content. First let's recover our &lt;code&gt;key&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;crypto&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;crypto&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;salt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;crypto&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;randomBytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hex&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;passphrase&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;password&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;decrypt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;passphrase&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;salt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;crypto&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;scryptSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;passphrase&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;salt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Next, to define the positions that we place when encrypting our content, which in the case of this article we are using position 17:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ivPosition&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;start&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;17&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;end&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;17&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;32&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;iv&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ivPosition&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ivPosition&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;end&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hex&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Having the IV in hand, just get the encrypted content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;part1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ivPosition&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;start&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;part2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ivPosition&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;encryptedText&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;part1&lt;/span&gt;&lt;span class="p"&gt;}${&lt;/span&gt;&lt;span class="nx"&gt;part2&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we have both the encrypted content and the IV, we can then begin the process of deciphering the content. The process is similar to encryption:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We will create our decryptor passing three parameters: the algorithm, the &lt;code&gt;key&lt;/code&gt; and the &lt;code&gt;iv&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;we add the cipher text and change the text format from &lt;code&gt;hex&lt;/code&gt; to &lt;code&gt;utf-8&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;We obtain the final deciphered content.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;decipher&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;crypto&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createDecipheriv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;aes-256-cbc&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;iv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;decrypted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;decipher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;encryptedText&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hex&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;utf8&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;decrypted&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;decipher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;final&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;utf8&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;decrypted&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In the end, we have a code similar to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;crypto&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;crypto&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;salt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;crypto&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;randomBytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hex&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;passphrase&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;password&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;decrypt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;passphrase&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;salt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;crypto&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;scryptSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;passphrase&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;salt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ivPosition&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;start&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;17&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;end&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;17&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;32&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;iv&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ivPosition&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ivPosition&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;end&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hex&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="na"&gt;part1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ivPosition&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;start&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="na"&gt;part2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ivPosition&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;encryptedText&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;part1&lt;/span&gt;&lt;span class="p"&gt;}${&lt;/span&gt;&lt;span class="nx"&gt;part2&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;decipher&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;crypto&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createDecipheriv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;aes-256-cbc&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;iv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;decrypted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;decipher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;encryptedText&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hex&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;utf8&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nx"&gt;decrypted&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;decipher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;final&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;utf8&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;decrypted&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Creating Asymmetric Keys
&lt;/h2&gt;

&lt;p&gt;Asymmetric keys, or public key cryptography, have transformed the way we protect sensitive information online. This guarantees extra security, although it is slower and requires more resources.&lt;/p&gt;

&lt;p&gt;NodeJS supports a variety of asymmetric key types to meet diverse encryption needs. These include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;RSA&lt;/strong&gt;: A widely used algorithm for asymmetric encryption, known for its security and efficiency;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DSA&lt;/strong&gt;: Digital Signature Algorithm, often used for digital signatures and authentication;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;EC (Elliptic Curve)&lt;/strong&gt;: Elliptic curve encryption algorithm, which offers a high level of security with smaller keys compared to RSA;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ed25519&lt;/strong&gt;: A high-performance digital signature system based on elliptic curves;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ed448&lt;/strong&gt;: Similar to ed25519, but with a higher level of security due to the larger key size;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;x25519&lt;/strong&gt; and &lt;strong&gt;x448&lt;/strong&gt;: Key exchange protocols based on elliptic curves, offering security and efficiency in restricted environments.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This variety of options allows developers to choose the algorithm best suited to their specific needs, balancing security, performance, and efficiency.&lt;/p&gt;

&lt;p&gt;For this article, we will use the implementation of the RSA algorithm, given its wide use. In the initial stage, it is crucial to generate the asymmetric keys: the Public Key and the Private Key. To do this, it is essential to specify the types and formats of these keys. We will opt for the PEM (Privacy-Enhanced Mail) format for both keys, thus ensuring a consistent and secure approach.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nf"&gt;generateKeyPair&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;passphrase&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;KeyPair&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;publicKey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;privateKey&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;crypto&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;generateKeyPairSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;rsa&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;modulusLength&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4096&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;publicKeyEncoding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;spki&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;format&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;pem&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;privateKeyEncoding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;pkcs8&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;format&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;pem&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;cipher&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;aes-256-cbc&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;passphrase&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;passphrase&lt;/span&gt; 
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;publicKey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;privateKey&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Firstly, it is important to understand the &lt;code&gt;generateKeyPairSync&lt;/code&gt; function. It is responsible for generating a pair of keys: one public and one private.&lt;/p&gt;

&lt;p&gt;When generating these keys, some parameters are essential:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;modulusLength&lt;/code&gt;: This parameter defines the length of the module. In the case of RSA, the minimum length is 4096;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;publicKeyEncoding&lt;/code&gt;: This object establishes the type and format of the public key. The type is defined as &lt;code&gt;spki&lt;/code&gt; (Subject Public Key Info), a standard format for public keys. The format is &lt;code&gt;pem&lt;/code&gt;, which is a &lt;code&gt;base64&lt;/code&gt; encoding type;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;privateKeyEncoding&lt;/code&gt;: This object defines the type, format, cipher, and passphrase of the private key. The type is defined as &lt;code&gt;pkcs8&lt;/code&gt;, also a standard format for private keys. The format is &lt;code&gt;pem&lt;/code&gt;, similar to the public key. The cipher is defined as &lt;code&gt;aes-256-cbc&lt;/code&gt;, an encryption algorithm as we saw earlier. The secret phrase, our &lt;code&gt;passphrase&lt;/code&gt;, is the password used to encrypt the private key.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In NodeJS, there are two types of public key (&lt;code&gt;pkcs1&lt;/code&gt; and &lt;code&gt;spki&lt;/code&gt;) and two types of private key (&lt;code&gt;pkcs1&lt;/code&gt; and &lt;code&gt;pkcs8&lt;/code&gt;) when using RSA.&lt;/p&gt;

&lt;p&gt;Here, we chose the &lt;code&gt;spki/pkcs8&lt;/code&gt; format. To add an extra layer of security, we also use the &lt;code&gt;AES-256-CBC&lt;/code&gt; (Cipher Block Chaining) algorithm as mentioned previously.&lt;/p&gt;

&lt;p&gt;As a result, the &lt;code&gt;generateKeyPairSync&lt;/code&gt; function returns an object containing the generated public and private keys.&lt;/p&gt;

&lt;h3&gt;
  
  
  Differences between PKCS1 and PKCS8
&lt;/h3&gt;

&lt;p&gt;PKCS is the acronym for "Public Key Cryptography Standards", which are a set of standards developed to assist the implementation of public key cryptography. Among these standards, we have PKCS1 and PKCS8, which describe formats for encoding private keys.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PKCS1&lt;/strong&gt; is the first PKCS standard, used specifically for encoding RSA keys. It details the process of encoding an RSA private key, which is relatively simple as an RSA key only requires a few integers.&lt;/p&gt;

&lt;p&gt;In contrast, &lt;strong&gt;PKCS8&lt;/strong&gt; is a more comprehensive standard used to encode any type of private key. It can be used not only for RSA keys but also for other types of keys such as DSA or EC (Elliptic Curve). Thus, PKCS8 is a more general format compared to PKCS1 as it includes information about the key type in the private key data structure.&lt;/p&gt;

&lt;p&gt;In short, the difference between PKCS1 and PKCS8 is that PKCS1 is specific to RSA keys, while PKCS8 can be used to encode any type of private key.&lt;/p&gt;

&lt;h2&gt;
  
  
  Encrypting with Asymmetric Keys
&lt;/h2&gt;

&lt;p&gt;With our Public Key at your disposal, it is very easy to encrypt any message.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First, we create a function that requires two parameters: &lt;code&gt;text&lt;/code&gt; and &lt;code&gt;publicKey&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;Next, we use the &lt;code&gt;publicEncrypt&lt;/code&gt; function, which will be used to encrypt the text;&lt;/li&gt;
&lt;li&gt;After that, we use &lt;code&gt;Buffer.from(text, 'utf8')&lt;/code&gt; to convert the text into a UTF-8 encoded buffer;&lt;/li&gt;
&lt;li&gt;Finally, we use &lt;code&gt;toString('base64')&lt;/code&gt; to convert the buffer to base64.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;encrypt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;publicKey&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;publicEncrypt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;publicKey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;utf8&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;base64&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;With this, we have our encrypted message ready to be stored in a database or something similar.&lt;/p&gt;

&lt;h3&gt;
  
  
  Decrypting with asymmetric keys
&lt;/h3&gt;

&lt;p&gt;To decrypt content, we only need our Private Key and the passphrase specified when creating the asymmetric keys.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First, we create a function that requires two parameters: &lt;code&gt;text&lt;/code&gt; and &lt;code&gt;PrivateKey&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;Next, we use the &lt;code&gt;privateDecrypt&lt;/code&gt; function, which will be used to decrypt the text;&lt;/li&gt;
&lt;li&gt;We pass the &lt;code&gt;PrivateKey&lt;/code&gt; and &lt;code&gt;Passphrase&lt;/code&gt; to the function;&lt;/li&gt;
&lt;li&gt;After that, we use &lt;code&gt;Buffer.from(text, 'base64')&lt;/code&gt; to convert the text to a base64 buffer;&lt;/li&gt;
&lt;li&gt;Lastly we use &lt;code&gt;.toString('utf8')&lt;/code&gt; to convert all content to utf8
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;decrypt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;encryptedText&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;privateKey&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;privateDecrypt&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;privateKey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;passphrase&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;encryptedText&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;base64&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;utf8&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;After these steps, we have our original message back. With this, we complete the full cycle of encryption and decryption using asymmetric keys.&lt;/p&gt;

&lt;p&gt;In conclusion, encryption plays a crucial role in protecting sensitive data and information in today's digital era.&lt;/p&gt;

&lt;p&gt;I hope that, through this article, you have gained a deeper understanding of the differences and applications between symmetric and asymmetric encryption. Furthermore, with the code examples provided, you should be able to implement your own encryption and decryption functions in NodeJS.&lt;/p&gt;

&lt;p&gt;Remember, data security is an important responsibility and encryption is one of the most effective tools we can use to ensure that security.&lt;/p&gt;

</description>
      <category>security</category>
      <category>node</category>
    </item>
    <item>
      <title>Como instalar o driver da NVIDIA no Arch Linux</title>
      <dc:creator>Vitor Vargas</dc:creator>
      <pubDate>Tue, 14 Nov 2023 13:00:00 +0000</pubDate>
      <link>https://forem.com/vitorvargas/como-instalar-o-driver-da-nvidia-no-archlinux-2oca</link>
      <guid>https://forem.com/vitorvargas/como-instalar-o-driver-da-nvidia-no-archlinux-2oca</guid>
      <description>&lt;p&gt;Por vezes, a documentação do Arch Linux pode parecer um labirinto, repleto de informações abrangentes, mas tão extenso que é fácil se perder. Este artigo tem como objetivo simplificar o processo de configuração do driver da NVIDIA no Arch Linux, proporcionando um guia mais acessível para mim e para outros que possam enfrentar desafios semelhantes no futuro.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Verificação da Arquitetura do GPU NVIDIA&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Antes de prosseguir, é crucial verificar a arquitetura exata da GPU NVIDIA instalada no seu sistema. Isso garantirá a compatibilidade correta com o driver adequado. Execute o seguinte comando para identificar a arquitetura da sua GPU:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="sb"&gt;`&lt;/span&gt;lspci | &lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;-E&lt;/span&gt; &lt;span class="s2"&gt;"VGA|3D"&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Certifique-se de pesquisar as informações da GPU NVIDIA no &lt;a href="https://www.nvidia.com/Download/index.aspx" rel="noopener noreferrer"&gt;site oficial da NVIDIA&lt;/a&gt; para garantir a seleção correta do driver.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Passo 1: Atualização do Sistema&lt;/strong&gt;&lt;br&gt;
Certifique-se de que seu sistema está atualizado com o seguinte comando:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;pacman &lt;span class="nt"&gt;-Syu&lt;/span&gt; &lt;span class="nt"&gt;--noconfirm&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Passo 2: Instalação dos Pacotes Necessários&lt;/strong&gt;&lt;br&gt;
Instale os pacotes necessários para o driver da NVIDIA com o comando:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;pacman &lt;span class="nt"&gt;-S&lt;/span&gt; &lt;span class="nt"&gt;--noconfirm&lt;/span&gt; nvidia nvidia-utils nvidia-settings opencl-nvidia xorg-server-devel
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Dica: caso estiver usando kernel LTS, instale &lt;code&gt;nvidia-lts&lt;/code&gt; ao invés de &lt;code&gt;nvidia&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Passo 3: Configuração do Xorg&lt;/strong&gt;&lt;br&gt;
Crie o diretório necessário e configure o arquivo para o Xorg:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;mkdir&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; /etc/X11/xorg.conf.d

&lt;span class="nb"&gt;cat&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; /etc/X11/xorg.conf.d/10-nvidia-drm-outputclass.conf&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;EOF&lt;/span&gt;&lt;span class="sh"&gt;
Section "OutputClass"
    Identifier "intel"
    MatchDriver "i915"
    Driver "modesetting"
EndSection

Section "OutputClass"
    Identifier "nvidia"
    MatchDriver "nvidia-drm"
    Driver "nvidia"
    Option "AllowEmptyInitialConfiguration"
    Option "PrimaryGPU" "yes"
    ModulePath "/usr/lib/nvidia/xorg"
    ModulePath "/usr/lib/xorg/modules"
EndSection
&lt;/span&gt;&lt;span class="no"&gt;EOF
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Neste passo, estamos criando e configurando o arquivo &lt;code&gt;10-nvidia-drm-outputclass.conf&lt;/code&gt; no diretório &lt;code&gt;/etc/X11/xorg.conf.d/&lt;/code&gt;. Este arquivo é essencial para definir as configurações de saída do Xorg para as GPUs Intel e NVIDIA. Ele usa as seções "OutputClass" para associar o driver &lt;code&gt;modesetting&lt;/code&gt; à GPU Intel e o driver &lt;code&gt;nvidia&lt;/code&gt; à GPU NVIDIA.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Passo 4: Configuração do GDM (GNOME Display Manager)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;mkdir&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; /usr/share/gdm/greeter/autostart
&lt;span class="nb"&gt;mkdir&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; /etc/xdg/autostart

&lt;span class="nb"&gt;cat&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; /usr/share/gdm/greeter/autostart/optimus.desktop&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;EOF&lt;/span&gt;&lt;span class="sh"&gt;
[Desktop Entry]
Type=Application
Name=Optimus
Exec=sh -c "xrandr --setprovideroutputsource modesetting NVIDIA-0; xrandr --auto"
NoDisplay=true
X-GNOME-Autostart-Phase=DisplayServer
&lt;/span&gt;&lt;span class="no"&gt;EOF

&lt;/span&gt;&lt;span class="nb"&gt;cat&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; /etc/xdg/autostart/optimus.desktop&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;EOF&lt;/span&gt;&lt;span class="sh"&gt;
[Desktop Entry]
Type=Application
Name=Optimus
Exec=sh -c "xrandr --setprovideroutputsource modesetting NVIDIA-0; xrandr --auto"
NoDisplay=true
X-GNOME-Autostart-Phase=DisplayServer
&lt;/span&gt;&lt;span class="no"&gt;EOF
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Aqui, estamos criando dois arquivos &lt;code&gt;.desktop&lt;/code&gt; para integrar a otimização do Optimus ao GDM. O GDM é configurado para executar automaticamente o script &lt;code&gt;xrandr&lt;/code&gt; para alocar as saídas de vídeo corretamente entre as GPUs Intel e NVIDIA durante a inicialização do servidor gráfico.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;O que é NVIDIA Optimus?&lt;/strong&gt;&lt;br&gt;
NVIDIA Optimus é uma tecnologia que gerencia automaticamente o uso da placa de vídeo integrada e dedicada em laptops para otimizar o desempenho e economizar energia. Em resumo, ajuda a equilibrar o poder de processamento conforme necessário.&lt;/p&gt;

&lt;p&gt;Caso esteja usando um Gerenciador de Login diferente do GDM &lt;a href="https://wiki.archlinux.org/title/NVIDIA_Optimus#Display_managers" rel="noopener noreferrer"&gt;siga a documentação do ArchLinux aqui&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Passo 5: Configuração do Modprobe&lt;/strong&gt;&lt;br&gt;
Configure o Modprobe para o driver da NVIDIA:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;mkdir&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; /etc/modprobe.d

&lt;span class="nb"&gt;cat&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; /etc/modprobe.d/nvidia-drm-nomodeset.conf&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;EOF&lt;/span&gt;&lt;span class="sh"&gt;
options nvidia-drm modeset=1
&lt;/span&gt;&lt;span class="no"&gt;EOF
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Este passo configura o Modprobe para o driver da NVIDIA, especificamente para o &lt;code&gt;nvidia-drm&lt;/code&gt;. Estamos adicionando uma opção ao arquivo &lt;code&gt;nvidia-drm-nomodeset.conf&lt;/code&gt; para habilitar o modo &lt;code&gt;modeset&lt;/code&gt;, que é essencial para uma integração suave do driver NVIDIA com o ambiente gráfico.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Passo 6: Reconstrução do Initramfs&lt;/strong&gt;&lt;br&gt;
Reconstrua o Initramfs para garantir que as alterações sejam aplicadas:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;mkinitcpio &lt;span class="nt"&gt;-P&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A reconstrução do Initramfs é crucial para aplicar as alterações feitas nos módulos do kernel, garantindo que o sistema reconheça corretamente o novo ambiente gráfico com o driver NVIDIA.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Passo 7: Execução de nvidia-xconfig&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;nvidia-xconfig
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;O comando &lt;code&gt;nvidia-xconfig&lt;/code&gt; é utilizado para gerar um arquivo de configuração Xorg específico para o driver da NVIDIA. Isso ajuda a garantir que as configurações necessárias estejam presentes e otimizadas para a GPU NVIDIA instalada.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Passo 8: Teste e Verificação do Driver NVIDIA&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Reinicie o sistema para aplicar todas as configurações. Após a reinicialização, verifique se o driver NVIDIA está carregado corretamente usando o comando: &lt;code&gt;nvidia-smi&lt;/code&gt;&lt;br&gt;
Além disso, abra o NVIDIA X Server Settings ou utilize o comando &lt;code&gt;nvidia-settings&lt;/code&gt; para acessar a interface gráfica e verificar as configurações específicas da GPU.&lt;/p&gt;

&lt;p&gt;Com a conclusão da configuração do driver NVIDIA no Arch Linux, espero sinceramente que este guia tenha sido valioso para você! Agradeço sinceramente por investir seu tempo na leitura deste artigo.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fontes
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://wiki.archlinux.org/title/NVIDIA" rel="noopener noreferrer"&gt;ArchLinux Docs: NVIDIA&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://wiki.archlinux.org/title/NVIDIA_Optimus#Display_managers" rel="noopener noreferrer"&gt;ArchLinux Docs: NVIDIA Optimus&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>How to install the NVIDIA driver on Arch Linux.</title>
      <dc:creator>Vitor Vargas</dc:creator>
      <pubDate>Mon, 13 Nov 2023 13:00:00 +0000</pubDate>
      <link>https://forem.com/vitorvargas/how-to-install-the-nvidia-driver-on-archlinux-5bgc</link>
      <guid>https://forem.com/vitorvargas/how-to-install-the-nvidia-driver-on-archlinux-5bgc</guid>
      <description>&lt;p&gt;Sometimes, the Arch Linux documentation may feel like a maze, filled with comprehensive information but so extensive that it's easy to get lost. This article aims to simplify the process of configuring the NVIDIA driver on Arch Linux, providing a more accessible guide for myself and others who may face similar challenges in the future.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Checking the NVIDIA GPU Architecture&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Before proceeding, it is crucial to check the exact architecture of the NVIDIA GPU installed on your system. This will ensure correct compatibility with the appropriate driver. Run the following command to identify your GPU's architecture:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;lspci | &lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;-E&lt;/span&gt; &lt;span class="s2"&gt;"VGA|3D"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make sure to look up the NVIDIA GPU information on the &lt;a href="https://www.nvidia.com/Download/index.aspx" rel="noopener noreferrer"&gt;official NVIDIA website&lt;/a&gt; to ensure the correct driver selection.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: System Update&lt;/strong&gt;&lt;br&gt;
Make sure your system is up to date with the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;pacman &lt;span class="nt"&gt;-Syu&lt;/span&gt; &lt;span class="nt"&gt;--noconfirm&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2: Installation of Required Packages&lt;/strong&gt;&lt;br&gt;
Install the necessary packages for the NVIDIA driver with the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;pacman &lt;span class="nt"&gt;-S&lt;/span&gt; &lt;span class="nt"&gt;--noconfirm&lt;/span&gt; nvidia nvidia-utils nvidia-settings opencl-nvidia xorg-server-devel
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Tip: If you are using the LTS kernel, install &lt;code&gt;nvidia-lts&lt;/code&gt; instead of &lt;code&gt;nvidia&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Step 3: Xorg Configuration&lt;/strong&gt;&lt;br&gt;
Create the necessary directory and configure the file for Xorg:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;mkdir&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; /etc/X11/xorg.conf.d

&lt;span class="nb"&gt;cat&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; /etc/X11/xorg.conf.d/10-nvidia-drm-outputclass.conf&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;EOF&lt;/span&gt;&lt;span class="sh"&gt;
Section "OutputClass"
    Identifier "intel"
    MatchDriver "i915"
    Driver "modesetting"
EndSection

Section "OutputClass"
    Identifier "nvidia"
    MatchDriver "nvidia-drm"
    Driver "nvidia"
    Option "AllowEmptyInitialConfiguration"
    Option "PrimaryGPU" "yes"
    ModulePath "/usr/lib/nvidia/xorg"
    ModulePath "/usr/lib/xorg/modules"
EndSection
&lt;/span&gt;&lt;span class="no"&gt;EOF
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this step, we are creating and configuring the &lt;code&gt;10-nvidia-drm-outputclass.conf&lt;/code&gt; file in the &lt;code&gt;/etc/X11/xorg.conf.d/&lt;/code&gt; directory. This file is essential for defining Xorg output settings for Intel and NVIDIA GPUs. It uses the "OutputClass" sections to associate the &lt;code&gt;modesetting&lt;/code&gt; driver with the Intel GPU and the &lt;code&gt;nvidia&lt;/code&gt; driver with the NVIDIA GPU.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4: GDM (GNOME Display Manager) Configuration&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;mkdir&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; /usr/share/gdm/greeter/autostart
&lt;span class="nb"&gt;mkdir&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; /etc/xdg/autostart

&lt;span class="nb"&gt;cat&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; /usr/share/gdm/greeter/autostart/optimus.desktop&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;EOF&lt;/span&gt;&lt;span class="sh"&gt;
[Desktop Entry]
Type=Application
Name=Optimus
Exec=sh -c "xrandr --setprovideroutputsource modesetting NVIDIA-0; xrandr --auto"
NoDisplay=true
X-GNOME-Autostart-Phase=DisplayServer
&lt;/span&gt;&lt;span class="no"&gt;EOF

&lt;/span&gt;&lt;span class="nb"&gt;cat&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; /etc/xdg/autostart/optimus.desktop&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;EOF&lt;/span&gt;&lt;span class="sh"&gt;
[Desktop Entry]
Type=Application
Name=Optimus
Exec=sh -c "xrandr --setprovideroutputsource modesetting NVIDIA-0; xrandr --auto"
NoDisplay=true
X-GNOME-Autostart-Phase=DisplayServer
&lt;/span&gt;&lt;span class="no"&gt;EOF
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we are creating two &lt;code&gt;.desktop&lt;/code&gt; files to integrate Optimus optimization with GDM. GDM is configured to automatically run the &lt;code&gt;xrandr&lt;/code&gt; script to allocate video outputs correctly between Intel and NVIDIA GPUs during the graphical server startup.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is NVIDIA Optimus?&lt;/strong&gt;&lt;br&gt;
NVIDIA Optimus is a technology that automatically manages the use of integrated and dedicated graphics cards in laptops to optimize performance and save energy. In summary, it helps balance processing power as needed.&lt;/p&gt;

&lt;p&gt;If you are using a different Login Manager than GDM, &lt;a href="https://wiki.archlinux.org/title/NVIDIA_Optimus#Display_managers" rel="noopener noreferrer"&gt;follow the ArchLinux documentation here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5: Modprobe Configuration&lt;/strong&gt;&lt;br&gt;
Configure Modprobe for the NVIDIA driver:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;mkdir&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; /etc/modprobe.d

&lt;span class="nb"&gt;cat&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; /etc/modprobe.d/nvidia-drm-nomodeset.conf&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;EOF&lt;/span&gt;&lt;span class="sh"&gt;
options nvidia-drm modeset=1
&lt;/span&gt;&lt;span class="no"&gt;EOF
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This step configures Modprobe for the NVIDIA driver, specifically for &lt;code&gt;nvidia-drm&lt;/code&gt;. We are adding an option to the &lt;code&gt;nvidia-drm-nomodeset.conf&lt;/code&gt; file to enable the &lt;code&gt;modeset&lt;/code&gt; mode, which is essential for smooth integration of the NVIDIA driver with the graphical environment.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6: Initramfs Reconstruction&lt;/strong&gt;&lt;br&gt;
Rebuild Initramfs to ensure the changes are applied:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;mkinitcpio &lt;span class="nt"&gt;-P&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Initramfs reconstruction is crucial to apply changes made to kernel modules, ensuring the system correctly recognizes the new graphical environment with the NVIDIA driver.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 7: Running nvidia-xconfig&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;nvidia-xconfig
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;nvidia-xconfig&lt;/code&gt; command is used to generate a specific Xorg configuration file for the NVIDIA driver. This helps ensure that the necessary settings are present and optimized for the installed NVIDIA GPU.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 8: Testing and Verifying the NVIDIA Driver&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Restart the system to apply all configurations. After the restart, check if the NVIDIA driver is loaded correctly using the command: &lt;code&gt;nvidia-smi&lt;/code&gt;. Additionally, open the NVIDIA X Server Settings or use the &lt;code&gt;nvidia-settings&lt;/code&gt; command to access the graphical interface and check specific GPU settings.&lt;/p&gt;

&lt;p&gt;With the completion of the NVIDIA driver configuration on Arch Linux, I sincerely hope this guide has been valuable to you! Thank you sincerely for investing your time in reading this article.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://wiki.archlinux.org/title/NVIDIA" rel="noopener noreferrer"&gt;ArchLinux Docs: NVIDIA&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://wiki.archlinux.org/title/NVIDIA_Optimus#Display_managers" rel="noopener noreferrer"&gt;ArchLinux Docs: NVIDIA Optimus&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Como usar hot reload em Go</title>
      <dc:creator>Vitor Vargas</dc:creator>
      <pubDate>Sun, 12 Nov 2023 11:00:00 +0000</pubDate>
      <link>https://forem.com/vitorvargas/como-usar-hot-reload-em-go-1np5</link>
      <guid>https://forem.com/vitorvargas/como-usar-hot-reload-em-go-1np5</guid>
      <description>&lt;p&gt;Quando dei os primeiros passos no estudo de Golang, optei por usar o Chi para gerenciar as rotas da minha primeira API. Contudo, me deparei com a ausência de um recurso essencial: o hot reload. Em busca de aprimorar meu processo de desenvolvimento, cai de cabeça em pesquisas na internet. Infelizmente, não encontrei soluções simples, mas achei algumas alternativas como executar o Go em um nodemon, o que me causou certo desconforto. Afinal, por que misturar Go com Node?&lt;/p&gt;

&lt;p&gt;Foi então que me deparei com o &lt;a href="https://taskfile.dev/" rel="noopener noreferrer"&gt;taskfile.dev&lt;/a&gt;. O Task é uma ferramenta de automação projetada para ser mais acessível do que outras opções, como o &lt;a href="https://www.gnu.org/software/make/" rel="noopener noreferrer"&gt;GNU Make&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Sendo desenvolvido em &lt;a href="https://go.dev/" rel="noopener noreferrer"&gt;Go&lt;/a&gt;, o Task é um binário simples, sem nenhuma outra dependência. Isso significa que você não enfrentará complicações durante o processo de instalação, tornando a utilização desta ferramenta de automação uma experiência descomplicada.&lt;/p&gt;

&lt;p&gt;Para começar, é crucial realizar a instalação do &lt;code&gt;taskfile&lt;/code&gt;. Siga as instruções do manual de instalação disponível em &lt;a href="https://taskfile.dev/installation/" rel="noopener noreferrer"&gt;https://taskfile.dev/installation/&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Após a instalação, precisamos criar um arquivo para configurar o taskfile em seu projeto. Portanto, no diretório raiz do seu projeto crie o arquivo &lt;code&gt;taskfile.yml&lt;/code&gt; com o seguinte conteúdo:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;3'&lt;/span&gt;

&lt;span class="na"&gt;tasks&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;:build&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;cmds&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;go&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;build&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;-o&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;dist/main&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;./main.go'&lt;/span&gt;
    &lt;span class="na"&gt;sources&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;./*.go,&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;./**/*.go&lt;/span&gt;

  &lt;span class="na"&gt;:start&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;cmds&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;task&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;:build&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;./dist/main'&lt;/span&gt;
    &lt;span class="na"&gt;sources&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; 
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;./*.go,&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;./**/*.go&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;O arquivo &lt;code&gt;taskfile.yml&lt;/code&gt; contém instruções para a ferramenta de automação &lt;code&gt;task&lt;/code&gt; (ou &lt;code&gt;go-task&lt;/code&gt;) sobre como executar diferentes tarefas relacionadas ao desenvolvimento do projeto em Go. Vamos analisar o conteúdo do arquivo:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;3'&lt;/span&gt;

&lt;span class="na"&gt;tasks&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;:build&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;cmds&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;go&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;build&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;-o&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;dist/main&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;-mod=vendor&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;./cmd/main.go'&lt;/span&gt;
    &lt;span class="na"&gt;sources&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;./*.go&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;./**/*.go&lt;/span&gt;

  &lt;span class="na"&gt;:start&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;cmds&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;task&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;:build&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;./dist/main'&lt;/span&gt;
    &lt;span class="na"&gt;sources&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;./*.go&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;./**/*.go&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Versão do Taskfile:&lt;/strong&gt; A primeira linha especifica a versão do &lt;code&gt;taskfile&lt;/code&gt; que está sendo usada.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Definição de Tarefas:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;:build&lt;/code&gt;: Esta tarefa compila o projeto Go. Ela usa o comando &lt;code&gt;go build&lt;/code&gt; para compilar o código-fonte localizado em &lt;code&gt;./main.go&lt;/code&gt; e salva o executável resultante em &lt;code&gt;dist/main&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;:start&lt;/code&gt;: Esta tarefa depende da tarefa &lt;code&gt;:build&lt;/code&gt; e, em seguida, executa o executável gerado (&lt;code&gt;./dist/main&lt;/code&gt;). Isso proporciona uma maneira de compilar e iniciar o aplicativo com um único comando.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Sources:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ambas as tarefas têm uma lista de sources que indicam quais arquivos ou diretórios devem ser observados para acionar a execução da tarefa quando houver alterações. Isso é útil para usar o hot reload, pois permite que o &lt;code&gt;task&lt;/code&gt; detecte mudanças nos arquivos e execute automaticamente as tarefas relacionadas.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Comando para Execução:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Os comandos sob a chave &lt;code&gt;cmds&lt;/code&gt; são os comandos reais executados pela tarefa. Eles são escritos como se fossem comandos de terminal padrão.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Depois de configurar esse &lt;code&gt;taskfile.yml&lt;/code&gt; e instalar o &lt;code&gt;task&lt;/code&gt;, você pode usar o comando &lt;code&gt;go-task :start -w --interval=500ms&lt;/code&gt; para compilar e iniciar seu aplicativo, com a capacidade de hot reload sempre que houver alterações nos arquivos especificados. Este processo simplifica o ciclo de desenvolvimento.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Dica: se isso não funcionar, tente substituir &lt;code&gt;go-task&lt;/code&gt; por &lt;code&gt;task&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Em resumo, a integração do &lt;code&gt;taskfile&lt;/code&gt; em projetos Go oferece uma solução elegante para a gestão de tarefas de desenvolvimento, como compilação e execução do aplicativo, especialmente quando se trata da necessidade crucial de hot reload. Ao adotar o &lt;code&gt;taskfile&lt;/code&gt;, você obtém uma ferramenta de automação simples, baseada em Go, eliminando a necessidade de soluções mais complexas e mistas. A configuração apresentada no &lt;code&gt;taskfile.yml&lt;/code&gt; permite compilar e iniciar o aplicativo com facilidade, enquanto o monitoramento contínuo das alterações nos arquivos facilita a atualização automática, proporcionando um fluxo de desenvolvimento mais eficiente e livre de complicações. Ao incorporar essa abordagem em seu fluxo de trabalho, você maximiza a produtividade e mantém o foco no desenvolvimento de qualidade em vez de lidar com tarefas manuais repetitivas.&lt;/p&gt;

</description>
      <category>go</category>
      <category>backend</category>
      <category>tutorial</category>
      <category>tooling</category>
    </item>
    <item>
      <title>How to use hot reload in Go</title>
      <dc:creator>Vitor Vargas</dc:creator>
      <pubDate>Sat, 11 Nov 2023 17:06:26 +0000</pubDate>
      <link>https://forem.com/vitorvargas/go-development-with-hot-reload-using-taskfile-5cdj</link>
      <guid>https://forem.com/vitorvargas/go-development-with-hot-reload-using-taskfile-5cdj</guid>
      <description>&lt;p&gt;When I took my first steps into studying Golang, I chose to use Chi to manage the routes of my first API. However, I faced the absence of an essential feature: hot reload. In an effort to enhance my development process, I delved into extensive research on the internet. Unfortunately, I didn't find simple solutions and encountered alternatives, such as running Go with nodemon, which made me somewhat uncomfortable. After all, why mix Go with Node?&lt;/p&gt;

&lt;p&gt;That's when I came across &lt;a href="https://taskfile.dev/" rel="noopener noreferrer"&gt;taskfile.dev&lt;/a&gt;. Task is an automation tool designed to be more accessible than other options, such as &lt;a href="https://www.gnu.org/software/make/" rel="noopener noreferrer"&gt;GNU Make&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Developed in &lt;a href="https://go.dev/" rel="noopener noreferrer"&gt;Go&lt;/a&gt;, Task is a simple binary with no other dependencies. This means you won't face complications during the installation process, making the use of this automation tool a straightforward experience.&lt;/p&gt;

&lt;p&gt;To get started, it's crucial to perform the installation of taskfile. Follow the instructions in the manual available at &lt;a href="https://taskfile.dev/installation/" rel="noopener noreferrer"&gt;https://taskfile.dev/installation/&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;After a successful installation, we need to create a file to configure taskfile in your project. So, in the root directory of your project create the &lt;code&gt;taskfile.yml&lt;/code&gt; file with the following content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;3'&lt;/span&gt;

&lt;span class="na"&gt;tasks&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;:build&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;cmds&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;go&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;build&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;-o&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;dist/main&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;./main.go'&lt;/span&gt;
    &lt;span class="na"&gt;sources&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;./*.go,&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;./**/*.go&lt;/span&gt;

  &lt;span class="na"&gt;:start&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;cmds&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;task&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;:build&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;./dist/main'&lt;/span&gt;
    &lt;span class="na"&gt;sources&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; 
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;./*.go,&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;./**/*.go&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Taskfile Version:&lt;/strong&gt; The first line specifies the version of the &lt;code&gt;taskfile&lt;/code&gt; being used.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Task Definitions:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;:build&lt;/code&gt;: This task compiles the Go project. It uses the &lt;code&gt;go build&lt;/code&gt; command to compile the source code located in &lt;code&gt;./main.go&lt;/code&gt; and saves the resulting executable in &lt;code&gt;dist/main&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;:start&lt;/code&gt;: This task depends on the &lt;code&gt;:build&lt;/code&gt; task and then executes the generated executable (&lt;code&gt;./dist/main&lt;/code&gt;). This provides a way to compile and start the application with a single command.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Sources:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Both tasks have a list of sources indicating which files or directories should be watched to trigger task execution when changes occur. This is useful for the hot reload feature, allowing &lt;code&gt;task&lt;/code&gt; to detect changes in source files and automatically execute related tasks.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Execution Command:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The commands under the &lt;code&gt;cmds&lt;/code&gt; key are the actual commands executed by the task. They are written as if they were standard terminal commands.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;After configuring this &lt;code&gt;taskfile.yml&lt;/code&gt; and installing &lt;code&gt;task&lt;/code&gt;, you can use the command &lt;code&gt;go-task :start -w --interval=500ms&lt;/code&gt; to compile and start your application, with the ability to hot reload whenever changes occur in the specified files. This process simplifies the development cycle.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;TIP: If this doesn't work, try replacing &lt;code&gt;go-task&lt;/code&gt; with &lt;code&gt;task&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In summary, the integration of &lt;code&gt;taskfile&lt;/code&gt; in Go projects provides an elegant solution for managing development tasks such as application compilation and execution, especially when it comes to the crucial need for hot reload. By adopting &lt;code&gt;taskfile&lt;/code&gt;, you gain a straightforward automation tool based on Go, eliminating the need for more complex and mixed solutions. The configuration presented in &lt;code&gt;taskfile.yml&lt;/code&gt; allows for easy compilation and startup of the application, while continuous monitoring of changes in source files facilitates automatic updates, providing a more efficient and complication-free development flow. By incorporating this approach into your workflow, you maximize productivity and maintain a focus on quality development rather than dealing with repetitive manual tasks.&lt;/p&gt;

</description>
      <category>go</category>
      <category>backend</category>
      <category>tutorial</category>
      <category>tooling</category>
    </item>
  </channel>
</rss>
