<?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: Krishnan</title>
    <description>The latest articles on DEV Community by Krishnan (@iam_krishnan).</description>
    <link>https://dev.to/iam_krishnan</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%2F384107%2Fa0dbdf76-1bb1-47eb-86a1-b27deec2530b.jpg</url>
      <title>DEV Community: Krishnan</title>
      <link>https://dev.to/iam_krishnan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/iam_krishnan"/>
    <language>en</language>
    <item>
      <title>Use salt and pepper to safely store the hashed passwords in the database</title>
      <dc:creator>Krishnan</dc:creator>
      <pubDate>Mon, 02 Oct 2023 09:25:41 +0000</pubDate>
      <link>https://dev.to/iam_krishnan/use-salt-and-pepper-to-safely-store-the-hashed-passwords-in-the-database-f18</link>
      <guid>https://dev.to/iam_krishnan/use-salt-and-pepper-to-safely-store-the-hashed-passwords-in-the-database-f18</guid>
      <description>&lt;p&gt;As developers we all pretty much know that passwords should not be stored in the table as plain text. So we all have pretty much figured our own way to hash the passwords before storing it in a database table. Some use the built in capabilities of the framework of a third party library that we picked from Github. &lt;/p&gt;

&lt;p&gt;But in reality storing passwords in hash does not mean that it is totally secure and safe. These hashed passwords are prone to rainbow table attacks. Also if multiple users have the same password their hash is also going to be the same. So for an attacker they have to crack one hashed to get the password of multiple users. &lt;/p&gt;

&lt;p&gt;To counter this security loop hole we can use salt and pepper to strengthen our hashed passwords and protect them against rainbow table attacks. &lt;/p&gt;

&lt;p&gt;Before we move forward let us address some definitions. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Salt&lt;/strong&gt; - Salt is a randomly generated string of alphanumeric characters. The minimum length of this string should be 8 characters &lt;em&gt;(there is no point in using a short salt)&lt;/em&gt;. It is best to regenerate the salt every time the user resets the password. The salt string is different for different users.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pepper&lt;/strong&gt; - It is a common string that is the same for all users and is defined in the config or env file. Pepper remains constant for the lifetime of the application unless a logic is implemented by the developers to bring in new pepper for future passwords. The pepper string is the same for different users.&lt;/p&gt;

&lt;p&gt;With the definitions behind us let us look into storing hashed passwords the right way. I have not used any code as I wanted to keep this article programming language agnostic.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Storing hashed password with salt and pepper&lt;/strong&gt;&lt;br&gt;
The workflow starts from the point where the user provides the password.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;user_password = &amp;lt;obtained from the end user&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the user provides the password the first step is to validate the password for length and any other predefined rules like use of uppercase and symbols. If validity fails throw an error here else move forward to the next step.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if user_password length &amp;gt;= 8 is true
else throw error
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Randomly generate an alphanumeric string to act as the salt and prepend it to the password provided by the user.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;salt = &amp;lt;random alphanumeric string&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The pepper is obtained from the env file and is appended to the password.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pepper = ENV(PASSWORD_PEPPER)
salted_preppered_password = salt + password + pepper
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a hash of this new string &lt;code&gt;(salted_preppered_password)&lt;/code&gt; and save it to the table along with the salt.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;hashed_password = hash-&amp;gt;make(salted_password)

insert into users (user_id, user_password, password_salt) values (1, hashed_password, salt);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Verifying user password with stored hashed password&lt;/strong&gt;&lt;br&gt;
The workflow for verifying the user entered password starts from the point where the user provides the password at the time of login.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;user_password = &amp;lt;obtained from the end user&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the user provides the password the first step like above is to validate the password for length and any other predefined rules like use of uppercase and symbols. If validity fails throw an error here else move forward to the next step.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if user_password length &amp;gt;= 8 is true
else throw error
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Get the &lt;code&gt;salt&lt;/code&gt; and the &lt;code&gt;hashed password&lt;/code&gt; from the database table and store it in &lt;code&gt;salt_from_db&lt;/code&gt; and &lt;code&gt;hashed_password&lt;/code&gt; respectively.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;select user_password, salt from users where user_name = provided_user_name;

salt_from_db = 
hashed_password_from_db = 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a salted and peppered version of the password. Otherwise the user authentication will fail.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pepper = ENV(PASSWORD_PEPPER)
salted_preppered_password = salt + password + pepper
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Compare the salted user provided password &lt;code&gt;(salted_preppered_password)&lt;/code&gt; with the hashed password the table and if the result is true the user is successfully authenticated. If not, throw an error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;If Hash::check(salted_preppered_password, hashed_password_from_db) is true
else. throw error
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
This process will drastically increase the strength of the hashed password that is stored in the database table. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;a href="https://www.krish.website/blog/post/use-salt-and-pepper-to-safely-store-the-hashed-passwords-in-the-database"&gt;Originally published on my blog&lt;/a&gt;.&lt;/em&gt; &lt;/p&gt;

</description>
      <category>webdev</category>
      <category>security</category>
      <category>programming</category>
    </item>
    <item>
      <title>Running a PHP script or Worker as a Systemd Service</title>
      <dc:creator>Krishnan</dc:creator>
      <pubDate>Sun, 24 Sep 2023 14:03:27 +0000</pubDate>
      <link>https://dev.to/iam_krishnan/running-a-php-script-or-worker-as-a-systemd-service-pf7</link>
      <guid>https://dev.to/iam_krishnan/running-a-php-script-or-worker-as-a-systemd-service-pf7</guid>
      <description>&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Systemd"&gt;Systemd&lt;/a&gt; is one of the best ways to run a PHP script as a service or to configure a worker. I have used it to configure multiple workers on the same server and it works seamlessly. You do not need to install, supervisor anymore to run your workers. &lt;/p&gt;

&lt;p&gt;In this article I have covered the step by step process to configure a service on Linux. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Choose a name for your service. Choose the name in relation to the application or the use case so that you remember it in the future. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Create a service file with the chosen name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo touch /etc/systemd/system/worker-name-service.service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; Edit the file and add the service information to it and save the file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo nano /etc/systemd/system/worker-name-service.service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[Unit]
Description=My App Notification Worker
After=network.target

[Service]
User=root
Group=www-data
Restart=always
WorkingDirectory=/var/www/html/path-to-worker
ExecStart=/usr/bin/php artisan queue:work

[Install]
WantedBy=multi-user.target
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; Reload the systemd daemon so that the new service is recognised.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl daemon-reload
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt; Enable the new service.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl enable worker-name-service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 6:&lt;/strong&gt; Start the new service.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl start worker-name-service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 7:&lt;/strong&gt; Check the status of the service to ensure it is up and running.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl status worker-name-service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you see the green light then you have successfully configured your worker or php script as a systemd service.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Originally posted on my &lt;a href="https://www.krish.website/blog/post/running-a-php-script-or-worker-as-a-systemd-service"&gt;personal blog&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>linux</category>
      <category>php</category>
      <category>worker</category>
    </item>
  </channel>
</rss>
