<?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>DEV Community: luizfnguimaraes</title>
    <description>The latest articles on DEV Community by luizfnguimaraes (@luizfnguimaraes).</description>
    <link>https://dev.to/luizfnguimaraes</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%2F831871%2F51d0d677-f78e-4a89-bca3-5c58ecaea367.png</url>
      <title>DEV Community: luizfnguimaraes</title>
      <link>https://dev.to/luizfnguimaraes</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/luizfnguimaraes"/>
    <language>en</language>
    <item>
      <title>C# Application Security - Password Hash using BCript</title>
      <dc:creator>luizfnguimaraes</dc:creator>
      <pubDate>Thu, 17 Mar 2022 00:32:36 +0000</pubDate>
      <link>https://dev.to/luizfnguimaraes/application-security-password-hash-using-bcript-4p4o</link>
      <guid>https://dev.to/luizfnguimaraes/application-security-password-hash-using-bcript-4p4o</guid>
      <description>&lt;p&gt;&lt;strong&gt;Hey there! How's it going? :)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In order to promote application security and develop my skills in .NET applications, I'm starting this series of posts about tools that can protect your application against cyber attacks and data leakage!&lt;/p&gt;

&lt;p&gt;In this first article, I will talk about password hash and how you can apply this functionality in your application using BCript! &lt;/p&gt;

&lt;p&gt;So without futher ado, let's get started!&lt;/p&gt;

&lt;h3&gt;
  
  
  First of all, what is Password Hash and why is it important?
&lt;/h3&gt;

&lt;p&gt;Imagine you just found the best application for ice cream delivery, and in order to use this application you have to sign up on the app. You go to the sign up page, type a login and a password, and after submitting your credentials, you're ready to order your favorite vanilla ice cream with chocolate and caramel coating.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr3re4oquro1qvpemmz20.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr3re4oquro1qvpemmz20.gif" alt=" " width="480" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But couple weeks later, you just found out that a bunch of people using the &lt;a href="https://static.turbosquid.com/Preview/2020/05/05__01_14_01/s1.jpgD034F452-8CF8-47F4-A057-C68175D840E2Large.jpg" rel="noopener noreferrer"&gt;Guy Fawkes mask&lt;/a&gt; just leaked the IceCreamApp database, with all the logins and passwords, which means that they know that your login is &lt;code&gt;ryanRaynolds98&lt;/code&gt; and your password is &lt;code&gt;iceCreamL0v3r&lt;/code&gt;. Besides that, now all services you use are compromised since you use the same credentials for all of them. Pretty sad, isn't it? &lt;/p&gt;

&lt;p&gt;You know that it's not safe using the same credentials for different services, but you keep doing it because it's easier to remember, and I'm not gonna judge you for doing that (ok, I'll judge you a little bit). But in case you're really lazy and have memory problems, I have a solution for you:&lt;/p&gt;

&lt;p&gt;You could use a &lt;code&gt;password manager!&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;A password manager is a software that helps you generating strong and different passwords for each service you use, and keep them in a safe place! So, problem solved, right?&lt;/p&gt;

&lt;p&gt;Unfortunatelly, there's no much people that knows about this type of software...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi40bj5li95og6exe1qdt.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi40bj5li95og6exe1qdt.gif" alt=" " width="480" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Having said that, what can we do as developers to protect our users from this type of attack? Is it possible to keep the users information in the database in a way that even if it's leaked, it's not readable for the hackers?&lt;/p&gt;

&lt;p&gt;The answer is YES, and it's called Password Hash!&lt;/p&gt;

&lt;p&gt;Instead of saving the password in plain text in the database like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ryanRaynolds98&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;you can hash the password and save it like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$2a$12$MaFASh9BC4tagsX2KH90Gu3om.Y.pwWqulHlh8CLeAV5a9VHcMJLW&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This sequence of random numbers and letters represents the same &lt;code&gt;ryanRaynolds98&lt;/code&gt;, but in a 'hidden' way, and even if I change a single letter on it, the hash is going to be completely different! Check this out:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ryanRaynoldz98&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;turns into&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$2a$12$RInj41FxAUtWKO6uCBz/9OOJLZToStW5NzU9UhaP4f6KodxpnaU66&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Cool, right?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9pm4vdlcu6yrqf02lz02.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9pm4vdlcu6yrqf02lz02.gif" alt=" " width="478" height="266"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hashing a password means use cryptography for turning the password readable only for the ones who have authorization for it!&lt;/p&gt;

&lt;p&gt;There are any different types of cryptography. One of them is known as &lt;em&gt;"Caesar cipher"&lt;/em&gt;, in which each letter of the text is replaced by the letter some fixed positions distant from it. &lt;/p&gt;

&lt;p&gt;For example, the password &lt;code&gt;"icecream"&lt;/code&gt; encrypted using the Caesar cipher could be like &lt;code&gt;"lfhfuhdp"&lt;/code&gt; if you replace each letter by the third letter just after it in the alphabet.&lt;/p&gt;

&lt;p&gt;But it's kinda easy to decrypt a password like this... A hacker could find it out in seconds! Look at their face if you use a hashing like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwfb0vagxmblhtggidwii.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwfb0vagxmblhtggidwii.gif" alt=" " width="337" height="263"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, what type of cryptography would be better for this situation and avoid Hannibal to steal your credentials?&lt;/p&gt;

&lt;p&gt;Coding your own hash is really complex and besides taking you hours of hard work, your final job can be prone to errors and vulnerabilities. So, it's recommended to use an existent library for helping you with that!&lt;/p&gt;

&lt;p&gt;The BCript pattern is a really good tool you can use to help you with hashing, and it's supported by lots of different programing languages!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F31w5exsfn2av31ztfc6i.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F31w5exsfn2av31ztfc6i.gif" alt=" " width="320" height="240"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;By the way, it doesn't just hash your original password, but it adds a different "salt" on it in order to have a different hash even if you use the same password! For example:&lt;/p&gt;

&lt;p&gt;If your password is &lt;code&gt;icecream&lt;/code&gt;, BCript is going to add a "Salt" like &lt;code&gt;sX4VDN6iRH&lt;/code&gt; before hashing, turning it into &lt;code&gt;sX4VDN6iRHicecream&lt;/code&gt;. After hashing, it's gonna be like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$2a$12$Uf1nLbgEPgkIBGKAjK4/peJ4hJuFFT/XOD6elOkT9/wWQF/PI.kIe&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;But if someone uses the same password as you, BCript is going to add another "Salt" like &lt;code&gt;Z5zmBaJanH&lt;/code&gt; and the password is going to be hashed like &lt;code&gt;Z5zmBaJanHicecream&lt;/code&gt;. The result is that below:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$2a$12$457.RA6A15Xl8nDgXbFJVuA0RQRzZj6pJmaJnqTqn7vP4jiCZBYq.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Okay, but now that you know it, you may ask yourself: &lt;strong&gt;&lt;em&gt;OMG, how can I use BCript in my project?&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Just like the Swedish House Mafia used to say, &lt;em&gt;"Don't you worry, child..."&lt;/em&gt;, I'm here to guide you through this!&lt;/p&gt;

&lt;p&gt;So put on your Avril Lavigne playlist and &lt;strong&gt;let's get the hands dirty!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo8bxgy15abyvje4enqbj.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo8bxgy15abyvje4enqbj.gif" alt=" " width="480" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Coding part!
&lt;/h1&gt;

&lt;p&gt;To get things started, I'm gonna use Visual Studio and create a Web Api on .NET 5 just for educational purposes ;)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdfc8agya33k2iv8ttjym.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdfc8agya33k2iv8ttjym.png" alt=" " width="528" height="458"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once it's created, I'm gonna delete all those "weather forecast" classes from the default application, because we ain't gonna use them. &lt;/p&gt;

&lt;p&gt;After that, I'll create a new &lt;strong&gt;&lt;em&gt;Model&lt;/em&gt;&lt;/strong&gt; folder and add an User class only with &lt;em&gt;Id&lt;/em&gt;, &lt;em&gt;Login&lt;/em&gt; and &lt;em&gt;Password&lt;/em&gt; parameters, just like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4q7gfy8e50iuojzfunlo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4q7gfy8e50iuojzfunlo.png" alt=" " width="508" height="260"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I'm also using the &lt;strong&gt;[Key]&lt;/strong&gt; data annotation on the &lt;em&gt;Id&lt;/em&gt; paramenter because it's needed for the ORM. In this case, I'm using a relational database and this data annotation tells the ORM that the &lt;em&gt;Id&lt;/em&gt; is the primary key.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/1279613/what-is-an-orm-how-does-it-work-and-how-should-i-use-one" rel="noopener noreferrer"&gt;&lt;em&gt;If you don't know what an ORM is, you can take a look at this post on Stack Overflow! Just click here ;)&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The ORM I'm using is the Entity Framework Core and, just like most of ORMs, it's gonna create a new &lt;em&gt;Id&lt;/em&gt; for every user I post to the database, so since I don't want the user to input any value to that paramenter, I'm also gonna create an UserDto which is going to present to the user only the parameters they have to input.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcxc5reycnzvknxowgeta.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcxc5reycnzvknxowgeta.png" alt=" " width="584" height="184"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;I ain't gonna focus on the ORM configuration and connection to the Database, but if you're interested on it, just leave a comment and I might create a post about it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4vu46hi1zh0n45jzmuwy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4vu46hi1zh0n45jzmuwy.gif" alt=" " width="480" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Okay, now that we have an User model, let's jump to the &lt;strong&gt;Controller&lt;/strong&gt;. I'm gonna create an User Controller and a &lt;em&gt;POST&lt;/em&gt; request on it. This POST request will take all the information from the body and save it in the database.&lt;/p&gt;

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

&lt;p&gt;I'm also going to create a &lt;em&gt;GET&lt;/em&gt; request, so we can take all users on the database.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fip6zp4nog2epq3rjzn9k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fip6zp4nog2epq3rjzn9k.png" alt=" " width="607" height="158"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Before configuring BCript, let's see how the database is saving the users. I'll run the application and set up the information on Swagger just like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F65orvel20dayphnfl8vx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F65orvel20dayphnfl8vx.png" alt=" " width="528" height="168"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The response body returns the 201 code, which means that the user was created!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwvzpcrqf5gahp83wv1n4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwvzpcrqf5gahp83wv1n4.png" alt=" " width="523" height="129"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But the password on the database is still saved like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fupt17c24t0sd65pw8zm9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fupt17c24t0sd65pw8zm9.png" alt=" " width="358" height="254"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So let's fix this!&lt;/p&gt;

&lt;h2&gt;
  
  
  Time to BCript!
&lt;/h2&gt;

&lt;p&gt;Okay, to use BCript we have to install the nuget package first! To do this, go to &lt;em&gt;Tools&lt;/em&gt;, then to &lt;em&gt;NuGet Package Maneger&lt;/em&gt; and finally click on &lt;em&gt;Package Manager Console&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpnrslj5ndnwftz7qrfcn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpnrslj5ndnwftz7qrfcn.png" alt=" " width="680" height="382"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the console, you're gonna write the command below and press &lt;em&gt;Enter&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Install-Package BCrypt.Net-Next&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Just like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fehazrssefyojjzuca2rh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fehazrssefyojjzuca2rh.png" alt=" " width="679" height="177"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, we go back to the POST request and, before saving the password itself on the Database, we're going to hash it using the comand &lt;/p&gt;

&lt;p&gt;&lt;code&gt;BCrypt.Net.BCrypt.HashPassword(user.password)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It's enough return the password hash just as expected! &lt;/p&gt;

&lt;p&gt;After hashing it, you can save it on the database normally. The instance of User class is gonna be like this:&lt;/p&gt;

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

&lt;p&gt;and the database after sending a new request with the same credentials as before is gonna be like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbkkxenkh8ntgd9dbcbio.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbkkxenkh8ntgd9dbcbio.png" alt=" " width="592" height="305"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Magical, isn't it?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz3gqt26vvhv7w4l5q0ab.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz3gqt26vvhv7w4l5q0ab.gif" alt=" " width="480" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Okay, you know that everything works as expected, but now you may ask yourself: &lt;em&gt;&lt;strong&gt;How can I compare the user password with the hash???&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;So, in order to authenticate the user, I'm gonna create a new POST request with an Authorize method and use BCript to validate the password submited by the user and the hash on the database.&lt;/p&gt;

&lt;p&gt;To do it, we can use the comand below:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;BCrypt.Net.BCrypt.Verify(password, hash)&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;The first parameter is the password submited and the second one is the hash from the database. If the password matches with the hash, the method will return &lt;code&gt;True&lt;/code&gt;. Otherwise, it's gonna return &lt;code&gt;False&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So, in order to wrap everything up, you can write your code like this:&lt;/p&gt;

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

&lt;p&gt;and after running the application, it's gonna work perfectly!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvytq171sjm64eikljm3r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvytq171sjm64eikljm3r.png" alt=" " width="414" height="172"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8ddy45qte6tw0fu5i8yo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8ddy45qte6tw0fu5i8yo.png" alt=" " width="413" height="122"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's it! Easy peasy lemon squeezy &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsjgfmyxe76jvdy5kxo0b.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsjgfmyxe76jvdy5kxo0b.gif" alt=" " width="480" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now you know how to implement password hash in your application!&lt;/p&gt;

&lt;p&gt;If you have any question or suggestions, don't hesitate to leave a comment or send me a message on &lt;a href="https://www.linkedin.com/in/luizfnguimaraes/" rel="noopener noreferrer"&gt;linkedin&lt;/a&gt;!&lt;/p&gt;

&lt;p&gt;You can also check all the code here on my &lt;a href="https://github.com/luizfngui/PasswordHash_BCript" rel="noopener noreferrer"&gt;GitHub repository&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;See you next time!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxdroyjcwbgp5qvzfxc8x.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxdroyjcwbgp5qvzfxc8x.gif" alt=" " width="306" height="498"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>bcript</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
