<?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: Jean-Paul Rustom</title>
    <description>The latest articles on DEV Community by Jean-Paul Rustom (@jaypmedia).</description>
    <link>https://dev.to/jaypmedia</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%2F489234%2F1f8750d5-95fd-4b08-8650-0ba196d1e908.png</url>
      <title>DEV Community: Jean-Paul Rustom</title>
      <link>https://dev.to/jaypmedia</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jaypmedia"/>
    <language>en</language>
    <item>
      <title>OAuth 2.0 Explained In Simple Terms (With Visuals)</title>
      <dc:creator>Jean-Paul Rustom</dc:creator>
      <pubDate>Fri, 15 Mar 2024 22:08:58 +0000</pubDate>
      <link>https://dev.to/jaypmedia/oauth-20-explained-in-simple-terms-with-visuals-30he</link>
      <guid>https://dev.to/jaypmedia/oauth-20-explained-in-simple-terms-with-visuals-30he</guid>
      <description>&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/iEXD7DB3QRQ"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h1&gt;
  
  
  1- Introduction
&lt;/h1&gt;

&lt;p&gt;In the early stages of human civilization, users would provide their username and password into a web application, and they would get access to anything they wanted.&lt;/p&gt;

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

&lt;p&gt;Today, in our web apps, we have ways to let our users access information in other applications, without sharing credentials.&lt;/p&gt;

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

&lt;p&gt;OAuth 2.0 is like finding a secret door in the back of your favorite website, guarded by a bouncer. &lt;/p&gt;

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

&lt;p&gt;Instead of just handing over your username and password like it's no big deal, OAuth 2.0 insists on a  exchanging tokens, redirect URLs, and authorization codes.&lt;/p&gt;

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

&lt;p&gt;In this article we’ll break down OAuth 2.0 into digestible explanations.&lt;/p&gt;

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

&lt;h1&gt;
  
  
  2- Concepts
&lt;/h1&gt;

&lt;p&gt;Okay so let’s explain core concepts for this OAuth thing, and for that we’ll refer to the spec’s example.&lt;/p&gt;

&lt;p&gt;Let’s say we have JayP, a printing service called Print Express, and Google Drive, which stores JayP’s photos.&lt;/p&gt;

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

&lt;p&gt;JayP wants to print all photos contained in his Google Drive account, using Print Express.&lt;/p&gt;

&lt;p&gt;Instead of uploading all photos from Google Drive to Print Express, we can have Print Express access the photos from Google Drive, using OAuth, without using Google Drive’s credentials.&lt;/p&gt;

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

&lt;p&gt;Now let’s explain few terminology.&lt;/p&gt;

&lt;h3&gt;
  
  
  1- Resource owner
&lt;/h3&gt;

&lt;p&gt;JayP is referred to as the resource owner.&lt;/p&gt;

&lt;p&gt;It is an entity capable of granting access to a protected resource. When the resource owner is a person, it is referred to as an end-user.&lt;/p&gt;

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

&lt;h3&gt;
  
  
  2- Resource server
&lt;/h3&gt;

&lt;p&gt;Google Drive here is the resource server.&lt;/p&gt;

&lt;p&gt;The resource server hosts the protected resources, and can accept and respond to protected resource requests using access tokens.&lt;/p&gt;

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

&lt;p&gt;The spec also mentions something called an authorization server.&lt;/p&gt;

&lt;p&gt;The authorization server issues access tokens to the client after successfully authenticating the resource owner and obtaining authorization.&lt;/p&gt;

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

&lt;p&gt;The resource server and the authorization server can be the same.&lt;/p&gt;

&lt;p&gt;For the sake of this article, we’ll have the authorization server and the resource server be the same, and not separate entities.&lt;/p&gt;

&lt;h3&gt;
  
  
  3- Client
&lt;/h3&gt;

&lt;p&gt;Print Express is the client.&lt;/p&gt;

&lt;p&gt;A client is an application making protected resources requests on behalf of the resource owner and with its authorization.&lt;/p&gt;

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

&lt;h1&gt;
  
  
  3- Flows
&lt;/h1&gt;

&lt;p&gt;In this article, we will cover two out of four flows described in the spec.&lt;/p&gt;

&lt;h3&gt;
  
  
  1- Authorization Code flow
&lt;/h3&gt;

&lt;p&gt;The authorization code flow is arguably the most generic flow which is primarily used in applications where a client secret can be securely stored on a server.&lt;/p&gt;

&lt;p&gt;1- The client FE redirects the user to an authorization endpoint from the resource server, where the user can authenticate and provide consent for the client to access their resources.&lt;/p&gt;

&lt;p&gt;2- Upon successful authentication and consent, the authorization server responds with a short-lived authorization code.&lt;/p&gt;

&lt;p&gt;3- The client, typically the server-side part, exchanges the authorization code for an access token by making a request to a token endpoint provided by the authorization server. This request includes the authorization code, client ID, and client secret.&lt;/p&gt;

&lt;p&gt;4- The authorization server validates the authorization code and client credentials. &lt;/p&gt;

&lt;p&gt;If successful it responds with an access token, and optionally a refresh token.&lt;/p&gt;

&lt;p&gt;The refresh token can be used to request new access tokens from the token endpoint, in case the last access token is expired, without requiring the end user to sign in again.&lt;/p&gt;

&lt;p&gt;5- With the obtained access token, the server-side component of the client can make authorized requests to access protected resources from the resource server by providing the access token in the request headers.&lt;/p&gt;

&lt;p&gt;In this flow, the front end part of the client, such as a web browser or a mobile app’s UI, may not see the access token, or if there’s a need for the front end to store it, it may receive it as encrypted from the server-side component.&lt;/p&gt;

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

&lt;h3&gt;
  
  
  2- Implicit Flow
&lt;/h3&gt;

&lt;p&gt;The implicit flow is aimed for client applications that do not have a server side part, for example a single page javascript application, or a mobile app.&lt;/p&gt;

&lt;p&gt;1- The client FE redirects the user to an authorization endpoint from the resource server, where the user can authenticate and provide consent for the client to access their resources.&lt;/p&gt;

&lt;p&gt;2- Upon successful authentication and consent, the resource server directly sends back the access token to the client, in the URL, and without the intermediate step of exchanging an authorization code for a token&lt;/p&gt;

&lt;p&gt;3- With the access token in hand, the client can now request access to protected resources from the resource server.&lt;/p&gt;

&lt;p&gt;In this case the access token will have a shorter lifespan than the one issued in the authorization code flow, and refresh token is not returned.&lt;/p&gt;

&lt;p&gt;Since the access token is returned directly, as is, to the front end, this flow is considered less secure.&lt;/p&gt;

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

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

&lt;h2&gt;
  
  
  4- Tokens
&lt;/h2&gt;

&lt;p&gt;Now before ending this article, let’s add few thing regarding the tokens.&lt;/p&gt;

&lt;p&gt;Commonly, both access tokens and refresh tokens can be JWTs, which means in this case they will be self contained. You can check our &lt;a href="https://dev.to/jaypmedia/jwt-explained-in-4-minutes-with-visuals-g3n"&gt;separate article explaining JWTs.&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;Tokens represent specific scopes (photos, email address, name) and durations of access, granted by the resource owner, and enforced by the resource server.&lt;/p&gt;

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

&lt;p&gt;The access token provides an abstraction layer, replacing the username and password with a single token understood by the resource server.  For this reason, issuing access tokens is more restrictive than the authorization grant used to obtain them.&lt;/p&gt;

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

&lt;h4&gt;
  
  
  That's it folks ! Here are other interesting topics:
&lt;/h4&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/dd8cCt9wMh8?start=323"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/0WH9oiYMS3M"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/1XbvNSwvkAs"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>node</category>
      <category>mobile</category>
    </item>
    <item>
      <title>WebSockets Explained Under 10 Minutes (With Visuals)</title>
      <dc:creator>Jean-Paul Rustom</dc:creator>
      <pubDate>Sat, 09 Mar 2024 21:30:42 +0000</pubDate>
      <link>https://dev.to/jaypmedia/websockets-explained-under-10-minutes-with-visuals-3ocl</link>
      <guid>https://dev.to/jaypmedia/websockets-explained-under-10-minutes-with-visuals-3ocl</guid>
      <description>&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/dd8cCt9wMh8"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h1&gt;
  
  
  Use-cases
&lt;/h1&gt;

&lt;p&gt;WebSockets are used in systems where we need to display data in real time and with low latency, such as chatting applications, stocks prices fluctuations, or game leaderboards.&lt;/p&gt;

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

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

&lt;h1&gt;
  
  
  History of HTTP 1.0 &amp;amp; HTTP 1.1
&lt;/h1&gt;

&lt;p&gt;Now before moving forward, let’s step back and talk a little bit about history.&lt;/p&gt;

&lt;h2&gt;
  
  
  HTTP 1.0 ( 1996 )
&lt;/h2&gt;

&lt;p&gt;In HTTP 1.0, each separate request would have its own TCP connection. &lt;/p&gt;

&lt;p&gt;We would open a TCP connection, send a request, and as soon as a response is received, we would close that connection.&lt;br&gt;
For example if we would want to load four images, we would open and close four separate TCP connections, which would kill the performance.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzzsya4eoaq2v1v3s5242.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzzsya4eoaq2v1v3s5242.png" alt="HTTP 1.0 Performance Killed"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  HTTP 1.1 ( 1997 )
&lt;/h2&gt;

&lt;p&gt;Now in HTTP 1.1, things have changed.&lt;/p&gt;

&lt;p&gt;We have a new header called Connection: ‘Keep-Alive’.&lt;/p&gt;

&lt;p&gt;We can initiate a TCP connection, keep it open, and have multiple requests and responses in this single TCP connection.&lt;/p&gt;

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

&lt;p&gt;Because persistent connections were introduced in HTTP 1.1, WebSockets’ minimum HTTP version should be 1.1.&lt;/p&gt;
&lt;h1&gt;
  
  
  Polling
&lt;/h1&gt;

&lt;p&gt;Historically, creating web apps that needed bidirectional communication, has required an abuse of HTTP to poll the server for updates.&lt;/p&gt;

&lt;p&gt;But sending multiple requests is expensive and could cause server overload. &lt;/p&gt;

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

&lt;p&gt;A simpler solution would be to use a single TCP connection for traffic in both directions.&lt;/p&gt;

&lt;p&gt;This is what the WebSocket Protocol provides.&lt;/p&gt;
&lt;h1&gt;
  
  
  Bi-directional Protocol
&lt;/h1&gt;

&lt;p&gt;As previously stated, WebSocket is a stateful bidirectional protocol, built on top of HTTP.&lt;/p&gt;

&lt;p&gt;It use a single TCP connection for traffic in both directions. &lt;/p&gt;

&lt;p&gt;The connection between client and server will keep alive until it is terminated by client, or by server.&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;WebSocket uses those default URI schemes, for secure and unsecure connections respectively&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

wss://jaypmedia.com/socket
ws://jaypmedia.com/socket


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

&lt;/div&gt;
&lt;h1&gt;
  
  
  WebSocket vs HTTP
&lt;/h1&gt;

&lt;p&gt;Also, WebSocket is not HTTP.&lt;br&gt;
It is, indeed, more complex, and of course more persistent and more lightweight.&lt;br&gt;
It is an independent TCP-based protocol. &lt;br&gt;
Its only relationship to HTTP is that its handshake is interpreted by HTTP servers as an HTTP Upgrade Request.&lt;/p&gt;

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

&lt;p&gt;Now don’t get the wrong idea, HTTP is great, but the request response model doesn’t cover bi-directional communication. &lt;/p&gt;
&lt;h1&gt;
  
  
  Handshake
&lt;/h1&gt;

&lt;p&gt;The WebSocket handshake is the bridge from HTTP to WebSockets.&lt;/p&gt;

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

&lt;p&gt;The WebSocket protocol begins its connection to the server as a simple HTTP request.&lt;/p&gt;

&lt;p&gt;In order to start the connection, clients sends an http GET request, that includes at least the following headers:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;

GET /chat HTTP/1.1

Host: server.jaypmedia.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13


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

&lt;/div&gt;
&lt;p&gt;If any of these are not included in the HTTP headers, the server should respond with an HTTP error code 400 Bad Request.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The Connection: Upgrade header was introduced in HTTP/1.1 to allow the client to notify the server of alternate means of communication.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;strong&gt;&lt;code&gt;Sec-WebSocket-Key&lt;/code&gt;&lt;/strong&gt; header is used during the WebSocket handshake to ensure that the client and server are speaking the WebSocket protocol.&lt;br&gt;
It is a base64 encoded value that is generated by randomly selecting 16-byte value as a nonce.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;strong&gt;&lt;code&gt;Sec-WebSocket-Version&lt;/code&gt;&lt;/strong&gt; header indicates the version of the WebSocket protocol that the client supports.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If the client is a web browser, it will supply the Origin header.&lt;br&gt;
If the server does not wish to accept connections from this origin, it can choose to reject the connection.&lt;br&gt;
Server will only accept connections from listed origins.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;If you are using a browser that supports WebSocket, the whole handshake and the generation of the relevant headers will be handled automatically by using the JavaScript API.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Server&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;The handshake from the server looks like this:&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

        HTTP/1.1 101 Switching Protocols

        Upgrade: websocket
        Connection: Upgrade
        Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=


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

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

&lt;p&gt;According to the WebSocket spec, the only indication that a connection to the WebSocket server has been accepted is the header field Sec-WebSocket-Accept. &lt;br&gt;
To get its value, the server would concatenate the value of Sec-WebSocket-Key received from the client, with a predefined global unique identifier, defined by the RFC.&lt;br&gt;
Then, the string formed will be hashed, then base64 encoded.&lt;/p&gt;

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

&lt;p&gt;This magic string exists because it will very likely not be used by servers that do not understand WebSockets.&lt;/p&gt;

&lt;p&gt;The server responds with a &lt;code&gt;101 status code.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Any code other than 101 results in an error and means that WebSocket handshake was not completed.&lt;/p&gt;

&lt;p&gt;Once the client and server have both sent their handshakes, and if the handshake was successful, then the data transfer part starts.&lt;/p&gt;

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

&lt;p&gt;Now let’s get a little bit deeper, shall we ?&lt;/p&gt;

&lt;p&gt;After a successful handshake, clients and servers transfer data back and forth using, at the bit level, a sequence of frames.&lt;/p&gt;

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

&lt;p&gt;There are control frames and data frames.&lt;/p&gt;

&lt;p&gt;Control frames communicate state about the WebSocket, for example the close frame which is used for closing connections.&lt;/p&gt;

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

&lt;p&gt;Data frames on the other hand, as their name implies, carry regular application data.&lt;/p&gt;

&lt;p&gt;Contrary to control frames, they can be fragmented.&lt;/p&gt;

&lt;p&gt;For security reasons and other concerns &lt;a href="https://datatracker.ietf.org/doc/html/rfc6455#section-10.3" rel="noopener noreferrer"&gt;explained by the RFC&lt;/a&gt;, it is required that a client MUST mask all frames that it sends to the server, whether or not TLS is used.&lt;/p&gt;

&lt;p&gt;Those concerns are related to proxies in the middle that do not understand WebSockets. &lt;/p&gt;

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

&lt;p&gt;RFC also requires that a server must close a connection if it receives a frame not masked.&lt;/p&gt;

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

&lt;p&gt;On the other hand, a server must not mask frames it sends to the client.&lt;/p&gt;

&lt;p&gt;A client will close a connection if it receives a masked frame. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxindfx2rbr9w19ls36tb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxindfx2rbr9w19ls36tb.png"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  &lt;strong&gt;WebSocket Frame Structure&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;Now let’s zoom a little bit into our frame.&lt;/p&gt;

&lt;p&gt;This is how the frame bits structure looks like.&lt;/p&gt;

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

&lt;p&gt;To know the details about what each field means, you can check &lt;a href="https://datatracker.ietf.org/doc/html/rfc6455#section-5" rel="noopener noreferrer"&gt;WebSocket’s spec&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We will go through some fields.&lt;/p&gt;

&lt;p&gt;1- FIN (1 bit) — Indicates if this is the final fragment in a message or not.&lt;/p&gt;

&lt;p&gt;2- Opcode: (4 bits) Defines the interpretation of the payload data.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;0: Continuation frame ( 0 for all frames except the first one )&lt;/li&gt;
&lt;li&gt;1: Text Frame&lt;/li&gt;
&lt;li&gt;2: Binary Frame&lt;/li&gt;
&lt;li&gt;8: Connection Close&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3- Mask (1 bit): If set to 1, masking-key is present. Should be one if sending from client.&lt;/p&gt;

&lt;p&gt;4- Masking key: ( 0 or 32 bits) The masking key field is present if MASK bit is set to , which means if sent from client.&lt;/p&gt;

&lt;p&gt;The masking key is a random key of 4 bytes chosen by the client.&lt;/p&gt;

&lt;p&gt;To decode the frame, we XOR its content with the masking key, 4 bytes at a time.&lt;/p&gt;
&lt;h1&gt;
  
  
  Frame Header size
&lt;/h1&gt;

&lt;p&gt;A WebSocket frame header can be at max 16 Bytes, which is way smaller than a big HTTP header which can reach the size of 16 KiloBytes.&lt;/p&gt;
&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;And that’s it ! We’re done with this quick explanation of WebSockets.&lt;/p&gt;

&lt;p&gt;Of course, you can try writing your own WebSocket server, or, you know, use already existing libraries that handles all this complexity.&lt;/p&gt;
&lt;h3&gt;
  
  
  More topics with visuals:
&lt;/h3&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/0WH9oiYMS3M"&gt;
&lt;/iframe&gt;
&lt;br&gt;
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/1XbvNSwvkAs"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>backenddevelopment</category>
      <category>javascript</category>
      <category>mobile</category>
    </item>
    <item>
      <title>JWT explained in 4 minutes (With Visuals)</title>
      <dc:creator>Jean-Paul Rustom</dc:creator>
      <pubDate>Sat, 24 Feb 2024 15:51:40 +0000</pubDate>
      <link>https://dev.to/jaypmedia/jwt-explained-in-4-minutes-with-visuals-g3n</link>
      <guid>https://dev.to/jaypmedia/jwt-explained-in-4-minutes-with-visuals-g3n</guid>
      <description>&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/0WH9oiYMS3M"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;JWT authentication and session authentication are ways to authenticate users of your web app.&lt;br&gt;
In this article we will explain the details of JWT, its structure along with its pros and cons.&lt;br&gt;
JWT stands for &lt;strong&gt;JSON Web Token&lt;/strong&gt;, and it is a commonly used stateless user authentication &lt;a href="https://datatracker.ietf.org/doc/html/rfc7519" rel="noopener noreferrer"&gt;standard&lt;/a&gt; used to securely transmit information between client and server in a JSON format. &lt;br&gt;
A JWT is encoded and not encrypted by default. &lt;br&gt;
It is digitally signed using a secret key known only to the server. &lt;br&gt;
It can be encrypted, but for this article we will focus on signed non-encrypted tokens.&lt;/p&gt;
&lt;h1&gt;
  
  
  Structure of JWT
&lt;/h1&gt;

&lt;p&gt;A JSON Web Token consists of 3 parts separated by a period.&lt;br&gt;
The header, the payload, and the signature.&lt;br&gt;
Each section is base64 encoded.&lt;/p&gt;

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

&lt;p&gt;The header consists of token type, which is JWT, and the signing algorithm used, such as HMAC SHA256 or RSA.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;

{ 
  "typ": "JWT",    
  "alg": "HS256"
}


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

&lt;/div&gt;
&lt;h3&gt;
  
  
  Payload
&lt;/h3&gt;

&lt;p&gt;The payload consists of the claims. &lt;br&gt;
Claims are statements about the user, and additional data.&lt;br&gt;
For example, we have the time the token was issued at.&lt;br&gt;
We also have its expiration time, because tokens should expire.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;

{
"iss": "example_issuer",
"sub": "user_id123",
"exp": 1644768000,
"iat": 1644744000
}


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

&lt;/div&gt;
&lt;h3&gt;
  
  
  Signature
&lt;/h3&gt;

&lt;p&gt;The signature is most important part of a JWT. &lt;br&gt;
It is calculated using the header, the payload, and the secret, which are fed to the signing algorithm to use.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;

signature = HMAC-SHA256(base64urlEncode(header) + "." + base64urlEncode(payload), secret_salt )


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

&lt;/div&gt;
&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo72ktulq29hondu66rbe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo72ktulq29hondu66rbe.png" alt="JWT Structure Decoded"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Steps
&lt;/h1&gt;

&lt;p&gt;The steps involved in a typical JWT authorization flow are as follows:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1- Authentication:&lt;/strong&gt; The user signs in using username and password, or using for example Google or Facebook.&lt;br&gt;
The server verifies the provided credentials.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2- Token Generation &amp;amp; sending token to client:&lt;/strong&gt; The server will generate the JWT and send it to the client, which stores it for future use.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3-Sending the Token to server:&lt;/strong&gt; When the client wants to access a protected resource on the server, it sends the JWT in the Authorization header of the HTTP request.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;

axios.get(URL, {
    headers: {
        'Authorization': 'Bearer ' + token,
    },
})


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

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;4-Verifying the Token:&lt;/strong&gt; The server receives the request and verifies the JWT by checking its signature using the secret key that was used to sign it. &lt;br&gt;
If the JWT is valid, the server extracts the information contained in it and uses it to determine what actions the user is authorized to perform.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5- Authorizing the Request:&lt;/strong&gt; If the user is authorized to access the resource, the server returns the requested data. If the user is not authorized, the server returns an error message.&lt;/p&gt;

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

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

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Lightweight&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Portable: can be processed on multiple platforms, web and mobile.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;JSON parsers are common in most programming languages.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Protected against tampering because of the secret key stored on server side.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The server does not need to store any session information, because of the stateless nature of the JWT token.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;
  
  
  Disadvantages
&lt;/h1&gt;

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

&lt;ul&gt;
&lt;li&gt;On server side, should manually mark non-expired JWT as invalid on logout.
A JWT can still be valid even after it has been deleted from the client. &lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;A method used is token blacklisting, which involves maintaining a list of invalidated tokens on the server side, preventing their reuse for authentication.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;For signed non encrypted tokens, we should not store sensitive info because JWT is protected against tampering but can be decoded and read by anyone.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Can provide full access if intercepted. That’s why JWTs on client side should be stored somewhere secure, for example in the browser in an HttpOnly cookie.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;My other articles:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://dev.to/jaypmedia/oauth-20-explained-in-simple-terms-with-visuals-30he"&gt;OAuth 2.0 Explained In Simple Terms (With Visuals)&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://dev.to/jaypmedia/what-happens-when-you-click-a-url-dns-lookup-tcp-handshake-http-33i1"&gt;What happens when you click a URL - DNS Lookup, TCP Handshake &amp;amp; HTTP (With visuals)&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://dev.to/jaypmedia/tls-certificates-2902"&gt;TLS Certificates 📝 - Simplified for web developers&lt;br&gt;
&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/iEXD7DB3QRQ"&gt;
&lt;/iframe&gt;
&lt;br&gt;
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/dd8cCt9wMh8"&gt;
&lt;/iframe&gt;
&lt;br&gt;
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/1XbvNSwvkAs"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>tls</category>
      <category>node</category>
    </item>
    <item>
      <title>What happens when you click a URL - DNS Lookup, TCP Handshake &amp; HTTP (With visuals)</title>
      <dc:creator>Jean-Paul Rustom</dc:creator>
      <pubDate>Fri, 16 Feb 2024 19:33:04 +0000</pubDate>
      <link>https://dev.to/jaypmedia/what-happens-when-you-click-a-url-dns-lookup-tcp-handshake-http-33i1</link>
      <guid>https://dev.to/jaypmedia/what-happens-when-you-click-a-url-dns-lookup-tcp-handshake-http-33i1</guid>
      <description>&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/1XbvNSwvkAs"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h1&gt;
  
  
  What happens when you type in a url
&lt;/h1&gt;

&lt;p&gt;Okay so what happens when JayP clicks on a url ? &lt;/p&gt;

&lt;p&gt;How would the internet take him to his desired website ?&lt;/p&gt;

&lt;h1&gt;
  
  
  Servers
&lt;/h1&gt;

&lt;p&gt;Before that, what exactly are websites ?&lt;/p&gt;

&lt;p&gt;Websites are collections of files, and images, that tell your browser how to display the site. &lt;/p&gt;

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

&lt;p&gt;A special powerful computer connected to the Internet, called a server, stores these files.&lt;/p&gt;

&lt;p&gt;Hosting these files on a server makes it reliable, and accessible to anyone anytime. &lt;/p&gt;

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

&lt;h1&gt;
  
  
  URL Structure
&lt;/h1&gt;

&lt;p&gt;The URL is an address that shows where a particular page can be found.&lt;/p&gt;

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

&lt;p&gt;The first part corresponds to the protocol used.&lt;/p&gt;

&lt;p&gt;A protocol is a set of rules that browser use for communication over the network. &lt;/p&gt;

&lt;p&gt;'https' is the secure version of the http protocol. &lt;/p&gt;

&lt;p&gt;The second part &lt;strong&gt;&lt;a href="http://www.jaypland.com" rel="noopener noreferrer"&gt;www.jaypland.com&lt;/a&gt;&lt;/strong&gt; is a &lt;strong&gt;domain name&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;We use it to reach to the server that is responsible for serving the information for that website.&lt;/p&gt;

&lt;h1&gt;
  
  
  IP
&lt;/h1&gt;

&lt;p&gt;In order for the web page to be retrieved, the URL needs to be translated into an &lt;strong&gt;IP address&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Clients interact through IP addresses which are unique identifiers.&lt;/p&gt;

&lt;p&gt;The IP tells to which server to connect.&lt;/p&gt;

&lt;p&gt;Actually, every device connected to the internet has a unique IP address.&lt;/p&gt;

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

&lt;p&gt;JayP could use the IP address instead of the URL to get content from a website. &lt;/p&gt;

&lt;p&gt;But, think of it for a second, it would be impossible to remember the IP addresses of every website!&lt;/p&gt;

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

&lt;p&gt;That's why domain names were invented. &lt;/p&gt;

&lt;p&gt;Now to understand how domain names are converted to IP addresses we need to talk about DNS.&lt;/p&gt;

&lt;h1&gt;
  
  
  DNS
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;DNS&lt;/strong&gt; stands for &lt;strong&gt;Domain Name System&lt;/strong&gt; and it is a huge database where domain names are stored with their corresponding IP addresses&lt;/p&gt;

&lt;p&gt;You could think of it as the phone book of the internet.&lt;/p&gt;

&lt;p&gt;Now how DNS works is a topic on its own and deserves its separate video, but here is the gist of it.&lt;/p&gt;

&lt;p&gt;When JayP clicks on a link, the browser checks its own cache, if the IP is not found then it will check the operating system cache. &lt;/p&gt;

&lt;p&gt;If the browser still cannot find the IP address, it will send a DNS query to the DNS resolver specified in the operating system's network settings, which is by default the ISP’s DNS resolver.&lt;/p&gt;

&lt;p&gt;The resolver will check its own cache. &lt;/p&gt;

&lt;p&gt;If the IP is still not found at any of those cache layers, the resolver will do a recursive DNS lookup. &lt;/p&gt;

&lt;p&gt;A recursive DNS lookup asks multiple DNS servers for the DNS record until it is found. &lt;/p&gt;

&lt;p&gt;The authoritative name server is responsible for knowing everything about the domain name.&lt;/p&gt;

&lt;p&gt;Finally, the resolver gets the IP address associated with the domain name and sends it back to the browser.&lt;/p&gt;

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

&lt;h1&gt;
  
  
  &lt;strong&gt;Connection with server using TCP&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;Now once the &lt;strong&gt;IP address&lt;/strong&gt; of the server is &lt;strong&gt;found&lt;/strong&gt;, the browser &lt;strong&gt;initiates a connection&lt;/strong&gt; with it using what is called a &lt;strong&gt;‘TCP 3-way handshake’&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;If you want to know more about it, we have a separate video explaining the handshake in details, in our youtube channel.&lt;/p&gt;

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

&lt;h1&gt;
  
  
  HTTP
&lt;/h1&gt;

&lt;p&gt;Once the connection is initiated, the browser will send an HTTP Get request to the server to get the webpage.&lt;/p&gt;

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

&lt;p&gt;&lt;code&gt;GET/ jaypland.com&lt;/code&gt;&lt;/p&gt;

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

&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;charset=&lt;/span&gt;&lt;span class="s"&gt;"UTF-8"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"viewport"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"width=device-width, initial-scale=1.0"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;JayPLand&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"styles.css"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;body&lt;/span&gt; &lt;span class="na"&gt;style=&lt;/span&gt;&lt;span class="s"&gt;"background-color: #00008B;"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;JayPLand&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;span&amp;gt;&lt;/span&gt;Where every click is an adventure&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://cdn.jaypland.com/jayp.png"&lt;/span&gt; &lt;span class="na"&gt;alt=&lt;/span&gt;&lt;span class="s"&gt;"JayP Logo"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"script.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;Then, the rendering engine of the browser starts interpreting and displaying the content.&lt;/p&gt;

&lt;p&gt;If the page has images or videos, the browser will see this and request that they are brought to the web page as well.&lt;/p&gt;

&lt;p&gt;And ta daaa...just like that the web page has loaded!&lt;/p&gt;

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

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>tls</category>
      <category>node</category>
    </item>
    <item>
      <title>Cipher Suites &amp; AEAD - ChaCha20-Poly1305 Example</title>
      <dc:creator>Jean-Paul Rustom</dc:creator>
      <pubDate>Sun, 03 Sep 2023 07:58:49 +0000</pubDate>
      <link>https://dev.to/jaypmedia/cipher-suites-aead-chacha20-poly1305-example-1i6</link>
      <guid>https://dev.to/jaypmedia/cipher-suites-aead-chacha20-poly1305-example-1i6</guid>
      <description>&lt;h2&gt;
  
  
  For the Animated Interactive Version:
&lt;/h2&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/S1Awy242Vf8"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h1&gt;
  
  
  What is a Cipher Suite
&lt;/h1&gt;

&lt;p&gt;Cipher suites in TLS 1.3 are combination of algorithms used for encryption and integrity.&lt;/p&gt;

&lt;p&gt;TLS 1.3 has only five possible cipher suites, because it removed all unsecure cipher suites from TLS 1.2.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;TLS_AES_128_GCM_SHA256&lt;/li&gt;
&lt;li&gt;TLS_AES_256_GCM_SHA384&lt;/li&gt;
&lt;li&gt;TLS_AES_128_CCM_8_SHA256&lt;/li&gt;
&lt;li&gt;TLS_CHACHA20_POLY1305_SHA256&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In TLS 1.2, separate algorithms were used for encryption and integrity of the messages.&lt;/p&gt;

&lt;p&gt;However, in TLS 1.3, all cipher suites use AEAD algorithms. &lt;/p&gt;

&lt;p&gt;AEAD stands for Authenticated Encryption with Associated Data.&lt;/p&gt;

&lt;p&gt;AEAD algorithms provide both encryption and authentication in a single step, making the process a lot simpler.&lt;/p&gt;

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

&lt;p&gt;For example the cipher suite &lt;strong&gt;&lt;code&gt;TLS_CHACHA20_POLY1305_SHA256&lt;/code&gt;&lt;/strong&gt; uses ChaCha20-Poly1305, as an AEAD cipher, and SHA-256 as a hash function for the Key Derivation Function. (Reminder: Key Derivation Function is used in TLS handshake to derive many keys)&lt;/p&gt;

&lt;h1&gt;
  
  
  Key Features About CHACHA20
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;It’s a symmetric key encryption algorithm&lt;/li&gt;
&lt;li&gt;Developed by Google&lt;/li&gt;
&lt;li&gt;Simple design and implementation, making it faster than AES&lt;/li&gt;
&lt;li&gt;Known for its security and high speed.&lt;/li&gt;
&lt;li&gt;Generates a stream of pseudo-random bits called the key-stream. This key-stream is then XORed with the plaintext to produce the cipher-text.&lt;/li&gt;
&lt;li&gt;Widely Supported&lt;/li&gt;
&lt;/ul&gt;

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

&lt;h2&gt;
  
  
  Encryption &amp;amp; Integrity with ChaCha20
&lt;/h2&gt;

&lt;p&gt;Okay, now the fun part.&lt;/p&gt;

&lt;p&gt;Let’s visualize how the elegant design of ChaCha20 integrates with Poly1305.&lt;/p&gt;

&lt;p&gt;First step is encrypt with CHACHA20. &lt;/p&gt;

&lt;p&gt;At a high level, ChaCha20 will take as inputs a shared secret key, a nonce, and a counter.&lt;/p&gt;

&lt;p&gt;Think of the nonce and counter as params used to increase the unpredictability and randomness of the cipher text.&lt;/p&gt;

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

&lt;p&gt;The "20" in "ChaCha20" refers to the number of rounds the algorithm goes through to process the data. These rounds involve various operations such as bit manipulations, addition, rotation, and XOR operations.&lt;/p&gt;

&lt;p&gt;These exists reduced round versions of ChaCha20 called ChaCha12 and ChaCha8.&lt;/p&gt;

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

&lt;p&gt;Based on these inputs, the ChaCha20 algorithm generates a pseudorandom stream of bits called the key-stream.&lt;/p&gt;

&lt;p&gt;Then, this key-stream is XORed with JayP’s plaintext message to produce the cipher text, in other words, the encrypted message.&lt;/p&gt;

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

&lt;p&gt;After that, the encrypted message, and unencrypted associated data such as addresses, ports, timestamps, together with the nonce and the secret key are inputs of POLY1305.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://medium.com/@jaypmedia/hmac-message-authentication-codes-why-hashing-alone-is-not-enough-for-data-integrity-96f636aa0c11" rel="noopener noreferrer"&gt;MAC&lt;/a&gt; (Message Authentication Code) algorithm POLY1305 will output a MAC. &lt;/p&gt;

&lt;p&gt;The MAC, also referred to as the authentication tag, will be sent along with the encrypted message.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Verification with ChaCha20
&lt;/h2&gt;

&lt;p&gt;Youtube, to decrypt and verify the message, would perform the reverse of the steps explained earlier.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Verification:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Youtube calculates the authentication tag from the received cipher text, the shared secret key, the associated data, and the nonce.&lt;/li&gt;
&lt;li&gt;The calculated authentication tag is compared to the received authentication tag.&lt;/li&gt;
&lt;li&gt;If they match, Youtube knows that the received message has not been altered.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

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

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Decryption:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;The ChaCha20 stream cipher is used with all necessary params to generate the same pseudo random stream, which is XORed with the received cipher text to produce the plaintext.
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;&lt;strong&gt;Also Published here:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://medium.com/@jaypmedia/cipher-suites-aead-chacha20-poly1305-example-812a609229f7" rel="noopener noreferrer"&gt;Medium&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>tls</category>
      <category>node</category>
    </item>
    <item>
      <title>Diffie-Hellman &amp; its Simple Maths Explained in 5 Minutes 🙆🏻‍♂️</title>
      <dc:creator>Jean-Paul Rustom</dc:creator>
      <pubDate>Fri, 18 Aug 2023 14:36:55 +0000</pubDate>
      <link>https://dev.to/jaypmedia/diffie-hellman-its-simple-maths-explained-in-5-minutes-3f2e</link>
      <guid>https://dev.to/jaypmedia/diffie-hellman-its-simple-maths-explained-in-5-minutes-3f2e</guid>
      <description>&lt;h2&gt;
  
  
  Watch the animated version here 👇🏻
&lt;/h2&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/aHSP1bmzqPo"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h1&gt;
  
  
  What is the discrete logarithm problem ?
&lt;/h1&gt;

&lt;p&gt;In order to understand the Diffie-Hellman algorithm, one must understand the discrete logarithm problem in Maths.&lt;/p&gt;

&lt;p&gt;The logarithmic function follows the concept of a trapdoor function. A trapdoor function is easy to compute in one direction, yet difficult to compute in the opposite direction.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--r9VQKjAc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7zklukvigj51qgnb5yo7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--r9VQKjAc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7zklukvigj51qgnb5yo7.png" alt="" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s visualize it this way:&lt;/p&gt;

&lt;p&gt;Given 2 teaspoons of coffee, 1 teaspoon of sugar and half cup of milk, you can make coffee.&lt;/p&gt;

&lt;p&gt;Now given the same coffee, can you guess the exact amounts of coffee, sugar, and milk?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DMUjdika--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h8psip4kh3730kojp4r1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DMUjdika--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h8psip4kh3730kojp4r1.png" alt="" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Modulo operation is an operation where the result is the remainder of the division operation performed with two given integers as operands.&lt;/p&gt;

&lt;p&gt;We say 13 mod 5 ≡ 3 because 13 = 5 x 2 + 3&lt;/p&gt;

&lt;p&gt;What if we want to find x such that   5^x mod 13 ≡ 11 ?&lt;/p&gt;

&lt;p&gt;Let’s try many values for x and check after how many tries we find the answer.&lt;/p&gt;

&lt;p&gt;After 8 tries, we will find that for x = 8 we will get 5 to the power x mod 13 is 11&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UTf62F_X--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yic3v0nc921kqr2lmtvs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UTf62F_X--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yic3v0nc921kqr2lmtvs.png" alt="" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now here is where it gets interesting. &lt;/p&gt;

&lt;p&gt;Let’s say we have x ≡ g^y mod p . &lt;/p&gt;

&lt;p&gt;But now we have p is a very large prime number (instead of 13) and g and x are also very large numbers (instead of 5 and 11). &lt;/p&gt;

&lt;p&gt;And by large, we mean, really large. Imagine for example p being equivalent to 2048 bits &lt;/p&gt;

&lt;p&gt;p=&lt;em&gt;292463028894281219037597349727360684779483317376473239399537257843546320734871513839651347201104480199877191389522614785890996622493775440722572336903336882065975199234931879695261910015161305537526235180562475469982005301778126151856278585195545100903316048416565416635315530213841740854981894053524430751990450476315137696784232893772161407889014577329968174019042697407332291644176957214843165640734510101308586892775505187939228207220238747243895829499434176678189216930154964392995431879145409980273938229601680574417474486982384981120906945098803000392061156847661464691587852166635245652933518718150794527&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Can you find y given these circumstances?&lt;/p&gt;

&lt;p&gt;This is the gist of the discrete logarithm problem, and even with the current generation computers it would take a very long time to solve.&lt;/p&gt;

&lt;p&gt;One of the reasons prime numbers are used is to avoid repeating patterns.&lt;/p&gt;

&lt;p&gt;For example, let's take p = 12, a non prime number&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;21 mod 12 =  2&lt;/li&gt;
&lt;li&gt;22 mod 12 = 4&lt;/li&gt;
&lt;li&gt;23 mod 12 = 8&lt;/li&gt;
&lt;li&gt;24 mod 12 = 4 (repeating)&lt;/li&gt;
&lt;li&gt;25 mod 12 = 10&lt;/li&gt;
&lt;li&gt;26 mod 12 = 2 (repeating)&lt;/li&gt;
&lt;li&gt;27 mod 12 = 4 (repeating)&lt;/li&gt;
&lt;li&gt;28 mod 12 = 8 (repeating)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example for p=11, prime number:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;21 mod 11 = 2&lt;/li&gt;
&lt;li&gt;22 mod 11 = 4&lt;/li&gt;
&lt;li&gt;23 mod 11 = 8&lt;/li&gt;
&lt;li&gt;24 mod 11 = 5&lt;/li&gt;
&lt;li&gt;25 mod 11 = 10&lt;/li&gt;
&lt;li&gt;26 mod 11 = 9&lt;/li&gt;
&lt;li&gt;27 mod 11 = 7&lt;/li&gt;
&lt;li&gt;28 mod 11 = 3&lt;/li&gt;
&lt;li&gt;29 mod 11 = 6&lt;/li&gt;
&lt;li&gt;210mod 11 = 1&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The discrete logarithm problem is the base of the Diffie-Hellman exchange algorithm.&lt;/p&gt;

&lt;h1&gt;
  
  
  Diffie-Hellman
&lt;/h1&gt;

&lt;p&gt;Diffie-Hellman key exchange can be used to securely exchange a secret key over an insecure network. It works using the concepts of prime numbers and modulo operations.&lt;/p&gt;

&lt;p&gt;First, JayP and Joe both agree on two numbers: g, and p, a large prime number. Those will be shared over the public internet.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--V2Ky-PLK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jqve2yq9kadkuyq9xtcq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--V2Ky-PLK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jqve2yq9kadkuyq9xtcq.png" alt="" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then, each of JayP and Joe will generate a random private key which will not be shared over the internet. &lt;/p&gt;

&lt;p&gt;After that JayP computes his public key using the formula in the screenshot below, and sends it to Joe.&lt;br&gt;
Similarly, Joe also computes his public key the same way and sends it to JayP.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7BMmQcJn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oolmnnox3ex7d1nkglo8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7BMmQcJn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oolmnnox3ex7d1nkglo8.png" alt="" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To recap, until now, both parties have shared over the public internet the params g and p, and their respective public keys. &lt;strong&gt;The private keys were not shared&lt;/strong&gt;. Now let’s see how the shared secret will be derived from these params.&lt;/p&gt;

&lt;p&gt;JayP computes the shared secret on his side using the equation shown in the screenshot below.&lt;/p&gt;

&lt;p&gt;Joe also computes the shared secret on his side using the same equation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_CeqgYRr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v0azjykllgkuvxcwgpmb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_CeqgYRr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v0azjykllgkuvxcwgpmb.png" alt="" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The secret key they have each calculated independently will be the same.&lt;/p&gt;

&lt;p&gt;Why? Well, due to simple maths. Let’s apply substitution of the public keys in each side.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--geW0pepm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0g97swykyp3b4qiqmxgz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--geW0pepm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0g97swykyp3b4qiqmxgz.png" alt="" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This explains how both JayP and Joe contribute to generating a shared secret value without actually transmitting it. &lt;/p&gt;

&lt;p&gt;Given p, g, and y, it is very easy to compute S. &lt;/p&gt;

&lt;p&gt;But over the public internet, Chady can only see p and g, and it will be very hard for him to compute y. Computing y is the discrete logarithm problem we just talked about earlier.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0E6fyCdd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ljwdwz9d9hguwkk261o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0E6fyCdd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ljwdwz9d9hguwkk261o.png" alt="" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the TLS handshake, the shared secret will be fed into what’s called a key derivation function.&lt;/p&gt;

&lt;p&gt;The KDF takes as input the shared secret, and other params such as a salt and some additional app-specific info. &lt;/p&gt;

&lt;p&gt;With all these inputs, the KDF will produce many keys, for example the &lt;a href="https://dev.to/jaypmedia/hmac-message-authentication-codes-why-hashing-alone-is-not-enough-for-data-integrity-36ad"&gt;MAC&lt;/a&gt; key, and the symmetric key used for data encryption.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wOJNnbRO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zsbouvkr4y4dyp7woqna.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wOJNnbRO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zsbouvkr4y4dyp7woqna.png" alt="" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Shameless Self Promotion 😨
&lt;/h2&gt;

&lt;p&gt;I'm kickstarting my new YouTube channel, &lt;a href="https://www.youtube.com/channel/UCjdab6ZvBSO-eCEwBL5H1jA"&gt;JayPMedia&lt;/a&gt;, where I'll be sharing all things web development. If you're keen to learn and grow with me, hit that subscribe button and let's dive into the world of coding and learning 😉&lt;/p&gt;

&lt;p&gt;Also published here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://medium.com/@jaypmedia/diffie-hellman-its-simple-maths-explained-in-5-minutes-f9061f04557c"&gt;https://medium.com/@jaypmedia/diffie-hellman-its-simple-maths-explained-in-5-minutes-f9061f04557c&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>tls</category>
      <category>node</category>
    </item>
    <item>
      <title>HMAC &amp; Message Authentication Codes - Why hashing alone is NOT ENOUGH for Data Integrity 😼</title>
      <dc:creator>Jean-Paul Rustom</dc:creator>
      <pubDate>Fri, 11 Aug 2023 21:03:59 +0000</pubDate>
      <link>https://dev.to/jaypmedia/hmac-message-authentication-codes-why-hashing-alone-is-not-enough-for-data-integrity-36ad</link>
      <guid>https://dev.to/jaypmedia/hmac-message-authentication-codes-why-hashing-alone-is-not-enough-for-data-integrity-36ad</guid>
      <description>&lt;h2&gt;
  
  
  For the animated version 👇🏻
&lt;/h2&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/vdzB5Rraeb4"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;Data integrity refers to the validation that data has not been modified or corrupted during transmission. It is an important topic to grasp before explaining TLS. Let’s illustrate it now.&lt;/p&gt;

&lt;p&gt;Suppose JayP and Joe are communicating over the public internet. Also, suppose both of them have a symmetric key and they have decided on the symmetric encryption algorithm to use.&lt;/p&gt;

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

&lt;p&gt;Here JayP sends his encrypted message. Encryption means confidentiality, and that no one can understand the message because it is encrypted. &lt;/p&gt;

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

&lt;p&gt;However, that does not mean that a malicious Chady cannot interfere with the message and change it, even though he does not understand it.&lt;/p&gt;

&lt;h1&gt;
  
  
  Bit-Flipping Attack 😨
&lt;/h1&gt;

&lt;p&gt;For example there is an attack known as bit flipping attack. &lt;/p&gt;

&lt;p&gt;Let’s have a bank transaction example to show how damaging this attack can be.&lt;/p&gt;

&lt;p&gt;Let’s say JayP is sending 100$ to Joe. &lt;/p&gt;

&lt;p&gt;Chady intercepts the message and although he does not understand a thing, he flips some bits and transfers the message to the bank. &lt;/p&gt;

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

&lt;p&gt;When the bank decrypts the message, it may show as 1000$ instead of 100$.&lt;/p&gt;

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

&lt;p&gt;That’s why, in addition to encryption, we need to make sure the data has not been altered.&lt;/p&gt;

&lt;h1&gt;
  
  
  The problem with hashing 🤔
&lt;/h1&gt;

&lt;p&gt;Back to our previous example.&lt;/p&gt;

&lt;p&gt;How can Joe make sure that the message has not been modified  ? 🤦🏻‍♂️&lt;/p&gt;

&lt;p&gt;The standard approach would be hashing.&lt;/p&gt;

&lt;p&gt;Let’s recall how hashing is typically used for data integrity.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Generating a Hash Value:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;JayP will apply a hash function to the data he intends to send, and will generate a fixed size hash value, often referred to as the “digest” or “checksum”, then he will send the data with its hash over the internet. If a single bit of the message is changed then it would output a different hash.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Verification Process:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;When Joe receives JayP’s message, he will apply the same hash function to the message. Then, he compares the computed hash with the received hash. If both match, it indicates that the data has not been altered by a malicious Chady.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;However, think for a second. Do you think this is enough?&lt;/p&gt;

&lt;p&gt;What do you think a malicious Chady would do to alter the data and still pass the integrity check?&lt;/p&gt;

&lt;p&gt;After all, hackers are smarter than that.&lt;br&gt;
Why not, alter the message and also send an altered hash ? How then can Joe know that the hash and the data have been modified ?&lt;/p&gt;

&lt;h1&gt;
  
  
  HMAC to the rescue 🦸🏻‍♂️
&lt;/h1&gt;

&lt;p&gt;Here comes the role of HMAC.&lt;/p&gt;

&lt;p&gt;HMAC stands for Hash Based Message Authentication Code. It is a cryptographic authentication technique that uses a hash function and a secret key.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;HMAC = hashFunc(secret key + message)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For HMAC to work, both JayP and Joe should agree on a shared secret and a hash function.&lt;/p&gt;

&lt;p&gt;In the TLS handshake article you will understand how the hash function is decided and how the secret key is generated through key exchange protocol. &lt;/p&gt;

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

&lt;h1&gt;
  
  
  HMAC Demo
&lt;/h1&gt;

&lt;p&gt;Let’s see the difference now.&lt;/p&gt;

&lt;p&gt;Now the hash function is applied with both the encrypted text and the secret. &lt;/p&gt;

&lt;p&gt;After that the generated MAC or message authentication code will be sent along the encrypted text.&lt;/p&gt;

&lt;p&gt;Let’s say Chady intercepts and changes the message along with its MAC.&lt;/p&gt;

&lt;p&gt;Remember that Chady does not have the secret key and will hash the encrypted message without any additional information.&lt;/p&gt;

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

&lt;p&gt;Joe, after receiving the message, will apply the HMAC function with the message and the secret key.&lt;/p&gt;

&lt;p&gt;The Message authentication codes will not match and Joe will know that the message has been tampered.&lt;/p&gt;

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

&lt;h1&gt;
  
  
  HMAC is one of many MACs algorithms
&lt;/h1&gt;

&lt;p&gt;So, let’s explain some of the details about MACs and HMAC, because I want you to be familiar with all the terms and to not be confused.&lt;/p&gt;

&lt;p&gt;HMAC is an algorithm that generates a Message Authentication Code.&lt;/p&gt;

&lt;p&gt;As explained, the message authentication code provides authenticity in addition of integrity, because it uses a secret key, and that’s how it mainly differs with a regular hash.&lt;/p&gt;

&lt;p&gt;There exists many &lt;strong&gt;algorithms&lt;/strong&gt; for calculating a message authentication code, and HMAC is just one of them.&lt;/p&gt;

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

&lt;p&gt;Other examples include for example POLY1305.&lt;/p&gt;

&lt;p&gt;Message Authentication codes are also used in authenticated encryption, which we will talk about in a separate article.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;HMAC advantages in TLS 👍🏻&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;HMAC is used in the TLS handshake, particularly in the “Finished” messages, where a MAC of the entire handshake up to that point is sent.&lt;/p&gt;

&lt;p&gt;HMAC is suitable for TLS for its &lt;strong&gt;many advantages&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Flexibility: HMAC can work with a variety of hash functions such as SHA-256 or SHA-3&lt;/li&gt;
&lt;li&gt;Standardization: HMAC is widely recognized and standardized, it has its own RFC (RFC2104) published in February 1997. This makes HMAC easier to implement in different environments. It can also be used for example in VPN and IPsec, web application APIs, and payment systems.&lt;/li&gt;
&lt;li&gt;Security: HMAC is also considered very secure against common hash functions vulnerabilities, due to the additional complexity introduced by the secret key.&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  HMAC Security
&lt;/h1&gt;

&lt;p&gt;HMAC's security has been extensively analyzed in the cryptographic community. The HMAC construction has been proven to offer security properties such as pseudorandomness, which means the output of HMAC resembles a random sequence, and unpredictability, which ensures that the output is difficult to predict even if the input is known.&lt;/p&gt;

&lt;p&gt;This additional randomness is achieved by the use of fixed strings defined in the HMAC algorithm, called ipad and opad, which are used to modify the key, and a series of steps including XOR operations, data appending, and applying the hash function to achieve the final MAC.&lt;/p&gt;

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

&lt;p&gt;The RFC particularly highlights the resistance against birthday attacks, which is an attack aiming at finding two messages leading to the same hash.&lt;/p&gt;

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

&lt;h1&gt;
  
  
  Why does HMAC internally modify the key
&lt;/h1&gt;

&lt;p&gt;Okay so why exactly does HMAC internally modify the key ?&lt;/p&gt;

&lt;p&gt;Well, In cryptography, &lt;strong&gt;a keys needs to be random&lt;/strong&gt; so that attackers can’t detect any patterns, because patterns make it easier for hackers to figure out the key.&lt;/p&gt;

&lt;p&gt;That’s why the ipad and opad values are critical components in the HMAC computation process, ensuring that the shared secret key is mixed and processed in a specific manner, enhancing the randomness of the key, and strengthening the security of the message authentication process.&lt;/p&gt;

&lt;p&gt;So that’s it ! I hope you are enjoying this mini-series about TLS. Don't forget to also check the &lt;a href="https://youtu.be/vdzB5Rraeb4" rel="noopener noreferrer"&gt;animated version&lt;/a&gt; 😉&lt;/p&gt;

&lt;p&gt;Also published here:&lt;br&gt;
&lt;a href="https://medium.com/@jaypmedia/hmac-message-authentication-codes-why-hashing-alone-is-not-enough-for-data-integrity-96f636aa0c11" rel="noopener noreferrer"&gt;https://medium.com/@jaypmedia/hmac-message-authentication-codes-why-hashing-alone-is-not-enough-for-data-integrity-96f636aa0c11&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>cybersecurity</category>
      <category>http</category>
      <category>javascript</category>
    </item>
    <item>
      <title>TCP / IP Stack - Oversimplified for Web Developers 🌍🧑🏻‍💻</title>
      <dc:creator>Jean-Paul Rustom</dc:creator>
      <pubDate>Thu, 10 Aug 2023 09:11:48 +0000</pubDate>
      <link>https://dev.to/jaypmedia/tcp-ip-stack-oversimplified-for-web-developers-3668</link>
      <guid>https://dev.to/jaypmedia/tcp-ip-stack-oversimplified-for-web-developers-3668</guid>
      <description>&lt;p&gt;&lt;strong&gt;Here is the animated version for those of you who prefer interactive videos:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/SXinGsiAPzI"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;The TCP / IP model is a conceptual model of how communications are made over the internet, and is consists of 4 layers.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--V_rz1XXm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wwdxwizwazzbjhbhn4yy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--V_rz1XXm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wwdxwizwazzbjhbhn4yy.png" alt="Layers of TCP / IP" width="730" height="1038"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  1. The Application Layer
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;application layer&lt;/strong&gt; is the layer that &lt;strong&gt;interacts directly with the end user&lt;/strong&gt;, providing the services and applications that allows user to access and use the network resources. &lt;/p&gt;

&lt;p&gt;They are software applications you use on your computer, such as web browsers, email clients and messaging apps.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7dP69Vu---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fij3hjfry37jndo05dni.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7dP69Vu---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fij3hjfry37jndo05dni.png" alt="Example: Applications that allows Internet access" width="654" height="406"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uAsi5pw4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wd61lhry20lxigbs2a2r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uAsi5pw4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wd61lhry20lxigbs2a2r.png" alt="Example: web browser, messaging apps" width="648" height="384"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;These use protocols such as:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;HTTPS&lt;/li&gt;
&lt;li&gt;DNS&lt;/li&gt;
&lt;li&gt;SMTP&lt;/li&gt;
&lt;li&gt;FTP&lt;/li&gt;
&lt;li&gt;SSH&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. The Transport Layer
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;transport layer&lt;/strong&gt; provides data transport services to and from the application layer. It offers two main options for data transportation: &lt;strong&gt;reliable&lt;/strong&gt; and efficient or &lt;strong&gt;unreliable&lt;/strong&gt; and fast.&lt;/p&gt;

&lt;p&gt;This layer uses protocols like TCP to provide reliable communication between two devices, and UDP to provide fast but unreliable transmission. (More on these in my other articles)&lt;/p&gt;

&lt;p&gt;The transport layer takes the application data (for eg an HTTP POST request) from the application layer and packages it into segments, which are then sent over the internet using port numbers.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1JStH9yO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zlllsjmtj8zwfl4wclfj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1JStH9yO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zlllsjmtj8zwfl4wclfj.png" alt="Structure of a TCP segment on a high level" width="800" height="505"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3. The Network Layer
&lt;/h2&gt;

&lt;p&gt;This layer is responsible for &lt;strong&gt;routing the data across multiple networks&lt;/strong&gt; and it uses protocols such as the IP protocol.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5wOhlg44--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vyqislfjkrsy7fb24d70.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5wOhlg44--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vyqislfjkrsy7fb24d70.png" alt="ip protocol route.png" width="800" height="773"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It takes the data from the transport layer and packages it into packets, which are then sent over the Internet using IP addresses.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--b6NNELhn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9jhg254thuhl5xy30dkp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--b6NNELhn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9jhg254thuhl5xy30dkp.png" alt="" width="800" height="459"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Think of it like a postal service that can deliver letters across the globe. &lt;/p&gt;

&lt;p&gt;It needs to know the destination and the best route to get there. The Internet layer uses IP addresses to identify different networks and determines the best path for data to travel across them. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Z8rZUuRZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ecs36r4auic983vcdq5p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Z8rZUuRZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ecs36r4auic983vcdq5p.png" alt="" width="800" height="363"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The network layer is considered an unreliable protocol&lt;/strong&gt; as it does not guarantee the delivery of packets or their order. The reliability of communication is achieved by higher protocols such as TCP which use IP as their underlying transport mechanism.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. The Link Layer
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;link layer&lt;/strong&gt; is responsible for &lt;strong&gt;taking the raw bits of data from the physical layer&lt;/strong&gt;, and organizing them into frames, then moving data between devices that are physically connected.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Pgwilbrb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xofalvmnpyq4s4lgwkyo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Pgwilbrb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xofalvmnpyq4s4lgwkyo.png" alt="Image description" width="800" height="319"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Think of it like a postman that is delivering letters within the same neighborhood. The postman needs to know the addresses of the sender and the recipient, and also needs to make sure that the letters are properly packaged and labeled. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--u2HhMlQd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hddem30hbj0204dmqfs1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--u2HhMlQd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hddem30hbj0204dmqfs1.png" alt="" width="800" height="514"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the same way, the data link layer uses frames to package data and add information such as source and destination MAC addresses to ensure it gets delivered to the correct device.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--W7PEjCdb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/33f4rr2myvrph3nofbhu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--W7PEjCdb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/33f4rr2myvrph3nofbhu.png" alt="" width="800" height="223"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>networking</category>
      <category>backend</category>
      <category>javascript</category>
    </item>
    <item>
      <title>TLS Certificates 📝 - Simplified for web developers</title>
      <dc:creator>Jean-Paul Rustom</dc:creator>
      <pubDate>Tue, 08 Aug 2023 07:56:21 +0000</pubDate>
      <link>https://dev.to/jaypmedia/tls-certificates-2902</link>
      <guid>https://dev.to/jaypmedia/tls-certificates-2902</guid>
      <description>&lt;h2&gt;
  
  
  Please have a time to check the animated version ;)
&lt;/h2&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/OU-e_Qz-v4U"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Let's say JayP wants to connect to youtube.com over the public internet. Before any communication occurs, some concerns will arise:&lt;br&gt;
How can he verify that he is indeed communicating with youtube ? (Authentication)&lt;br&gt;
How can JayP know that no one has intercepted his message ? (Confidentiality)&lt;br&gt;
Also, how can he be sure that no one altered the message? (Integrity)&lt;/p&gt;

&lt;p&gt;Well, let's tackle each problem separately.&lt;br&gt;
First of all, JayP should verify the identity of Youtube. For that, he needs Youtube's public key. The public key represents youtube's identity.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frpl1c910orh2nvd6r89a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frpl1c910orh2nvd6r89a.png" alt="Is it really Youtube? Or a malicious Chady ?"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;The problem is, attackers have come up with ways to intercept a request from one computer to another computer on the Internet and launch a man in the middle attack. With this kind of attack, our attacker Chady would inject his own public key and have JayP think he is communication with Youtube, while actually he is communicating with Chady.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Femh70t2rd4tjz4p8sk6p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Femh70t2rd4tjz4p8sk6p.png" alt="Chady sneaking in and sending HIS public key"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now you may think, scary, isn't it? Imagine what could be possible with this kind of attack, from stealing personal information such as login credentials or credit card details, to malware distribution or other malicious goals.&lt;br&gt;
How then, can JayP be sure that the public key he received comes really from Youtube?&lt;br&gt;
That's where digital certificates come in. (TADAAA)&lt;/p&gt;

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

&lt;p&gt;They ensure that the public key is indeed coming from Youtube, and not from a malicious Chady. Actually, digital certificates can be used not only for HTTPS, but also for mails, IOT, and VPN…&lt;/p&gt;

&lt;h2&gt;
  
  
  Information Inside TLS Certificate
&lt;/h2&gt;

&lt;p&gt;A digital certificate will contain many information, let's explore the most important ones.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frjia9rj3eoo9zdcessu3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frjia9rj3eoo9zdcessu3.png" alt="Youtube’s Certificate as shown in the Chrome browser"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Issuer&lt;/strong&gt; is the third party which signed this certificate, we'll get into it in a bit. Some basic information will include the Common Name, the organization and the country.&lt;/li&gt;
&lt;li&gt;Of course, a digital certificate will also include its &lt;strong&gt;Validity&lt;/strong&gt;, with two critical fields, not before and not after&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;Subject&lt;/strong&gt; is the owner of the certificate and would be Youtube in our case. The subject will contain information such as common name, address, and most importantly, the public key.&lt;/li&gt;
&lt;li&gt;Lastly, a digital certificate include its &lt;strong&gt;Signature&lt;/strong&gt;. This is the signature signed by the issuer that proves that this certificate is authentic.
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1meeho6gore3dyuydldy.png" alt="Certificate Authority"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Who signs the digital certificate ?
&lt;/h2&gt;

&lt;p&gt;Digital certificates are signed by entities called certificate authorities (for example DigiCert, Comodo, Symantec, Google Trust Services).&lt;br&gt;
But wait, what are certificate authorities?&lt;br&gt;
Well, in a nutshell, Certificate Authorities are trusted third-party organizations responsible for issuing digital certificates.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdi03a80qvg2p2jfc0u2t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdi03a80qvg2p2jfc0u2t.png"&gt;&lt;/a&gt;&lt;br&gt;
Certificate authorities have their own public and private keys. From that, let's explain how signatures are made.&lt;br&gt;
The previously mentioned information contained in a digital certificate will be hashed using for eg SHA-256 algorithm.&lt;br&gt;
After the hash is generated, the certificate authority encrypts the hash using its private key using RSA asymmetric key encryption. This encrypted hash is the signature of a certificate, and can only be decrypted using the certificate authority's public key. Anyone with the public key of the certificate authority which has signed this certificate can decrypt and verify that the certificate is authentic and has not been tampered.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmk2ve4w14zv26qqccqj8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmk2ve4w14zv26qqccqj8.png"&gt;&lt;/a&gt;&lt;br&gt;
When the browser receives the certificate, it will hash all the information on its side using the same hash algorithm, in this example, SHA-256. Then, it decrypts the signature using the certificate authority's public key to have the hash received from the certificate authority.&lt;br&gt;
If both hashes match, then the browser can confirm that this certificate is truly coming from the claimed certificate authority.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feqms2y9str49sx8u1ffv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feqms2y9str49sx8u1ffv.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Hierarchies for Certificate Authorities
&lt;/h2&gt;

&lt;p&gt;There exists hierarchies for certificate authorities, the root certificate authority and the intermediate certificate authority.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6t4ywybzro1stjjsaowf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6t4ywybzro1stjjsaowf.png"&gt;&lt;/a&gt;&lt;br&gt;
These root CA does not actually directly issue any digital certificates for servers. It only issues digital certificates for intermediate CAs that act on its behalf. The intermediate CAs can either issue digital certificates for another intermediate CA, or for a server directly.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvj8yo3jsrm89ao7gehaw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvj8yo3jsrm89ao7gehaw.png"&gt;&lt;/a&gt;&lt;br&gt;
Hence, There is a chain of trust, from the root CA to the server.&lt;br&gt;
Operating systems come bundled with a trust store, which is a list of trusted root certificate authorities. This list is maintained by the companies that make operating systems such as Apple and Microsoft. They all require a root certificate authority to undergo one or more audits proving their trustworthiness and validity.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does all of this fit into the picture ? Let's visualize the whole process from the start.
&lt;/h2&gt;

&lt;p&gt;First of all, let's assume youtube.com is a brand new startup. Understanding the need to be secure on a publicly available internet, Youtube would have to own a trusted digital certificate, and present it to its visitors.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fspcsu6adv6ux95xldlnf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fspcsu6adv6ux95xldlnf.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Youtube goes shopping for a digital certificate
&lt;/h2&gt;

&lt;p&gt;First of all, Youtube goes searching for an intermediate certificate authority. Remember that intermediate certificate authorities are middlemen between servers and root certificate authorities. In the case of Youtube, Google has its own certificate authority called Google Trust Services.&lt;/p&gt;

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

&lt;p&gt;After picking an intermediate CA, Youtube makes a certificate signing request. The certificate signing request will contain information that will be included in Youtube's digital certificate, such as common name, organization, and most importantly the public key of Youtube. Youtube will send the certificate signing request to the intermediate CA.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzo879jzywylha5d2e0tx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzo879jzywylha5d2e0tx.png"&gt;&lt;/a&gt;&lt;br&gt;
The intermediate certificate authority will verify the CSR containing information about the owner also referred to as the subject. Then, it adds field like issuer information, which is information about the intermediate certificate, and the validity, then signs all these information as explained previously in the signing process. After signing, the intermediate certificate authority will send back the digital certificate for youtube.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcnbs7qqfy03zu3vklk7s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcnbs7qqfy03zu3vklk7s.png"&gt;&lt;/a&gt;&lt;br&gt;
Youtube can now attach its newly bought digital certificate to its web servers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: JayP connects to youtube.com to watch fitness videos
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz43au33398nt1cqxs2iz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz43au33398nt1cqxs2iz.png"&gt;&lt;/a&gt;&lt;br&gt;
When he will connect to Youtube, Youtube will send its own certificate and certificates of intermediate certificate authorities. The root certificate authority will not be sent because it is available on JayP's Operating System.&lt;br&gt;
Youtube's certificate will include the issuer information, which is the intermediate certificate authority. The browser will hence know that this certificate is signed by Google Trust Services Intermediate Certificate Authority, and will have its certificate. Any digital certificate will contain information, and a signature, as explained previously.&lt;br&gt;
The browser will hash all the information to obtain their hash value. Then, it will decrypt the signature using the issuer's public key, in other words, the intermediate certificate authority's public key. Again, remember that the signature can only be decrypted using the issuer's public key. After that the browser will compare the two hash values, if they are the same, then this part of the chain is verified, and the browser will move on verifying the intermediate certificate authority, because as explained, intermediate certificate authorities are not trusted directly by the operating systems.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbm63zt8es79bh0wlduv6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbm63zt8es79bh0wlduv6.png"&gt;&lt;/a&gt;&lt;br&gt;
Okay now the browser will verify the certificate of Google Trust Intermediate Certificate Authority. This intermediate certificate is signed by Google Trust Services Root Certificate authority.&lt;br&gt;
Please note that before this step, we could have many intermediate certificate authorities between the end certificate and the root certificate, and for each part of the chain the same verification process will happen.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F76rzdzvi2w3n2vk5sufz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F76rzdzvi2w3n2vk5sufz.png"&gt;&lt;/a&gt;&lt;br&gt;
In our example, we will just stick to one.&lt;br&gt;
The signature of the certificate of the intermediate certificate authority will be verified with the public key of the root certificate authority, as previously.&lt;br&gt;
When reaching the root certificate authority, that's where the chain ends. The root certificate authority, which is self signed, will be available in JayPee's operating system's trust store.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7ldos9eymblm1lzfei2w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7ldos9eymblm1lzfei2w.png"&gt;&lt;/a&gt;&lt;br&gt;
That's it! The browser, after this verification, will display a nice looking lock icon, like the one in your browser. Now it is guaranteed that the communication is really with Youtube, and that it is safe (HOORAY)&lt;/p&gt;

&lt;h2&gt;
  
  
  Three types of Domain Certificates
&lt;/h2&gt;

&lt;p&gt;Before we wrap up, we will shortly explain three main types of TLS certificates.&lt;/p&gt;

&lt;h2&gt;
  
  
  1) Single Domain Certificate
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqth572fjuwak6yp2u9oe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqth572fjuwak6yp2u9oe.png"&gt;&lt;/a&gt;&lt;br&gt;
A single domain certificate is designed to secure a single fully qualified domain name. It means that the certificate is valid only for one specific domain and does not cover any subdomains or additional domains. For example, for the domain "&lt;a href="http://www.youtube.com," rel="noopener noreferrer"&gt;www.youtube.com,&lt;/a&gt;" a single domain certificate would secure only that domain and not any other variations like "academy.youtube.com" or "blog.youtube.com"&lt;br&gt;
Use cases: Single domain certificates are ideal when you have a single website or web application that does not require additional subdomains. They are the most straightforward and cost-effective option for securing a specific domain.&lt;/p&gt;

&lt;h2&gt;
  
  
  2) Wildcard Certificate
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnd5bcjgx26f2mb6kufil.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnd5bcjgx26f2mb6kufil.png"&gt;&lt;/a&gt;&lt;br&gt;
Next we have a wildcard certificate, which is a type of TLS certificate issued for a main domain and all its subdomains using a single certificate. The wildcard character asterix is used in the Common Name (CN) field or Subject Alternative Name (SAN) field to indicate that any subdomain under the specified domain is covered. For example, if you have a wildcard certificate for "&lt;em&gt;.youtube.com," it would secure "youtube.com," "academy.youtube.com&lt;/em&gt;," blog.youtube.com", and so on.&lt;br&gt;
Use cases: Wildcard certificates saves you from having to manage and renew individual certificates for each subdomain. However, it's essential to note that wildcard certificates only cover one level of subdomains. For instance, a wildcard certificate for "*.youtube.com" would not cover "academy.blog.youtube.com."&lt;/p&gt;

&lt;h2&gt;
  
  
  3) Multi-Domain (SAN) Certificate
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu1xc3qk4cj07lzjukyhx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu1xc3qk4cj07lzjukyhx.png"&gt;&lt;/a&gt;&lt;br&gt;
A multi-domain certificate, also known as a Subject Alternative Name certificate, allows you to secure multiple unrelated domains within a single certificate. Each domain or subdomain to be secured is listed in the Subject Alternative Name (SAN) extension of the certificate.&lt;br&gt;
For example, Google and other large companies would use these multi-domain certificates.&lt;br&gt;
One of the main benefits would be a Simplified Certificate Management: Google operates a lot of web services and applications, each with its own domain or subdomain. Using multi-domain certificates allows them to consolidate the management of certificates for multiple domains or subdomains into a single certificate. This streamlines the process of certificate issuance, renewal, and monitoring.&lt;/p&gt;

&lt;p&gt;In my next article I will discuss encryption with TLS&lt;/p&gt;

</description>
      <category>tls</category>
      <category>webdev</category>
      <category>react</category>
      <category>node</category>
    </item>
  </channel>
</rss>
