<?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: S B Rakesh Rath</title>
    <description>The latest articles on DEV Community by S B Rakesh Rath (@sbrakeshrath).</description>
    <link>https://dev.to/sbrakeshrath</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2634670%2F15a109af-dd9d-4521-aac9-3af5277b821a.jpeg</url>
      <title>DEV Community: S B Rakesh Rath</title>
      <link>https://dev.to/sbrakeshrath</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sbrakeshrath"/>
    <language>en</language>
    <item>
      <title>Number Guessing Game (A fun Game)</title>
      <dc:creator>S B Rakesh Rath</dc:creator>
      <pubDate>Sun, 02 Feb 2025 05:02:03 +0000</pubDate>
      <link>https://dev.to/sbrakeshrath/number-guessing-game-a-fun-game-57im</link>
      <guid>https://dev.to/sbrakeshrath/number-guessing-game-a-fun-game-57im</guid>
      <description>&lt;p&gt;&lt;a href="https://gng.pixismith.com/" rel="noopener noreferrer"&gt;Link to the game&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I have created a Number guessing game. It is not some typical number guessing game where computer will generate a random number and you have to guess it. &lt;/p&gt;

&lt;p&gt;In this game user has to guess the number which is average of all the guessed numbers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Logic of the game
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Initially there is a number &lt;code&gt;x&lt;/code&gt; in the database.&lt;/li&gt;
&lt;li&gt;User has to guess the number &lt;code&gt;x&lt;/code&gt;, which should be in the range of &lt;code&gt;1-1000&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If the guessed number is correct then user wins the game.&lt;/li&gt;
&lt;li&gt;If the guessed number is wrong then a new number &lt;code&gt;y&lt;/code&gt; is generated which is &lt;code&gt;Round((x + guessed number)/2)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If that number ever falls between &lt;code&gt;0-10&lt;/code&gt; or &lt;code&gt;990-1000&lt;/code&gt; then a new unique number is generated.&lt;/li&gt;
&lt;li&gt;User can guess the number once in 5 seconds.&lt;/li&gt;
&lt;li&gt;Only first 5 users to guess the number will be considered as winners.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Tech Stack
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Frontend: ReactJS, Typescript, Vite and SASS (No Fancy frameworks)&lt;/li&gt;
&lt;li&gt;Backend: NodeJS, ExpressJS, Fire Store 
(You may ask why fireStore, because it is free and I am a student 😅)
(Literally I have used firestone for everything even for rate limiting the user requests 😂. And the score is a singe field in the database so I even used transaction in fireStore.)&lt;/li&gt;
&lt;li&gt;Authentication: Firebase (I am too lazy to write another authentication service from scratch and waste my 2-3 days and still it will have bugs 😅)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Why authentication ?? If not then how will I know which is winner&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hosting: Firebase Hosting, GCP cloud run (For the backend which is just a magic of Google) and API gateway (I will explain it another post why I used it)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Links
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://gng.pixismith.com/" rel="noopener noreferrer"&gt;Game&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/SBRakeshRath/guessing-number-game-backend" rel="noopener noreferrer"&gt;Backend Github&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/SBRakeshRath/guessing-number-game-frontend" rel="noopener noreferrer"&gt;Frontend Github&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Database less OTP- A concept</title>
      <dc:creator>S B Rakesh Rath</dc:creator>
      <pubDate>Thu, 02 Jan 2025 16:14:03 +0000</pubDate>
      <link>https://dev.to/sbrakeshrath/database-less-otp-a-concept-1gi9</link>
      <guid>https://dev.to/sbrakeshrath/database-less-otp-a-concept-1gi9</guid>
      <description>&lt;p&gt;Helllo, firstly I am not an expert, so please teach me if anything goes wrong in this post and please don't judge my english.&lt;/p&gt;

&lt;p&gt;Let's talk about the topic...&lt;/p&gt;

&lt;h2&gt;
  
  
  How a otp system works (as per my knowlwdge)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  First way
&lt;/h3&gt;

&lt;p&gt;First a user come to the authentication page, then he sees a form where he has to enter his email or phone number and request a otp to verify that email/phone and he recives a otp. Then he copies that otp and paste it in the box and now that email/phone is verified.&lt;/p&gt;

&lt;h3&gt;
  
  
  Second way
&lt;/h3&gt;

&lt;p&gt;In this way he recives a link in his mail, and when he clicks on it the email gets verified.&lt;/p&gt;

&lt;h3&gt;
  
  
  What happens in backend
&lt;/h3&gt;

&lt;p&gt;When a user requests for otp by sending his email to backend, The backend creates a random string and store it in redis cache whith a expiry life time along with its email. And when the user sends that otp, in backend that otp gets mathed with the stored one.&lt;/p&gt;

&lt;h2&gt;
  
  
  The concept
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Thought
&lt;/h3&gt;

&lt;p&gt;Everytime I have created this system for authentication I had a thought do I really need a database and a redish instance costs 1500 INR for a month and I think it's a pricy for a toy project as we don't really want OTP for any-other purpose. So I thought using a hashing kind of thing to eleminate the database completely.&lt;/p&gt;

&lt;h3&gt;
  
  
  Approach
&lt;/h3&gt;

&lt;p&gt;Lets divide the OTP into 2 parts&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The raw OTP string&lt;/li&gt;
&lt;li&gt;Hashed OTP string
The raw OTP string will be sent to the email/phone
The hashed OTP will be shared with the requested user (Browser)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And in the next request user will provide the OTP from email and stored hash OTP. And, in backend we can compare them and if it matches.&lt;/p&gt;

&lt;p&gt;But a user can change the hash and send it. So here comes the JSON web token we can add the hash and email to a JWT so that user can't change the hash and we can also add a JWT expiry time for the OTP.&lt;/p&gt;

&lt;p&gt;Normally a OTP is 6 digit number. So a person can create a rainbow table of all 6 digits hash and cand match it to know the OTP. To over come this we can use hmac who takes a starting key so brute force is not possibele.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;hashedOTP&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;otp&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="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&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;createHmac&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sha256&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;CRYPTO_SECRET&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;otp&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;digest&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Demo
&lt;/h2&gt;

&lt;p&gt;For this I have created a working Demo - &lt;a href="https://db-less-otp.netlify.app/" rel="noopener noreferrer"&gt;https://db-less-otp.netlify.app/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Backend Github Repo - &lt;a href="https://github.com/SBRakeshRath/DB-less-OTP-Backend" rel="noopener noreferrer"&gt;https://github.com/SBRakeshRath/DB-less-OTP-Backend&lt;/a&gt;&lt;br&gt;
Frontend Github Repo - &lt;a href="https://github.com/SBRakeshRath/DB-less-OTP-Frontend" rel="noopener noreferrer"&gt;https://github.com/SBRakeshRath/DB-less-OTP-Frontend&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;** I am Using a gmail account so I don't want you to spam this as you can send only 500 emails&lt;/p&gt;

&lt;p&gt;Please point-out the vulnerabilities and how to fix them. &lt;/p&gt;

</description>
      <category>webdev</category>
      <category>systemdesign</category>
      <category>programming</category>
      <category>discuss</category>
    </item>
  </channel>
</rss>
