<?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: Shubham Khan</title>
    <description>The latest articles on DEV Community by Shubham Khan (@shubhamkhan).</description>
    <link>https://dev.to/shubhamkhan</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%2F2108084%2Faab82232-cf20-4ac2-95c5-13a9f3cf9d9c.jpg</url>
      <title>DEV Community: Shubham Khan</title>
      <link>https://dev.to/shubhamkhan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shubhamkhan"/>
    <language>en</language>
    <item>
      <title>How to Securely Remove a File from Git History: A Step-by-Step Guide</title>
      <dc:creator>Shubham Khan</dc:creator>
      <pubDate>Wed, 23 Oct 2024 13:02:49 +0000</pubDate>
      <link>https://dev.to/shubhamkhan/how-to-securely-remove-a-file-from-git-history-a-step-by-step-guide-10fm</link>
      <guid>https://dev.to/shubhamkhan/how-to-securely-remove-a-file-from-git-history-a-step-by-step-guide-10fm</guid>
      <description>&lt;p&gt;When managing a Git repository, it's possible to mistakenly commit sensitive information or files that should not be part of your project’s history. In this guide, we'll walk you through the process of safely removing such files from Git history, using the example of a common development file, .env.development, which typically contains environment variables. By following these steps, you can ensure that sensitive data is permanently erased from your repository.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Remove a File from Git History?
&lt;/h2&gt;

&lt;p&gt;There are several reasons why you might want to remove a file from your Git history:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Security:&lt;/strong&gt; Accidentally committing sensitive data, such as API keys or passwords, can put your project at risk.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Repository Cleanliness:&lt;/strong&gt; Keeping your Git history clean and organized makes it easier to track changes and manage your codebase.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Best Practices:&lt;/strong&gt; Following good version control practices ensures that your repository contains only necessary files, avoiding clutter.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 1: Using git filter-branch to Remove the File
&lt;/h2&gt;

&lt;p&gt;The git filter-branch command allows you to rewrite your Git history, which is useful for removing unwanted files. Open your terminal, navigate to your repository, and run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git filter-branch --force --index-filter "git rm --cached --ignore-unmatch .env.development" --prune-empty --tag-name-filter cat -- --all
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What Does This Command Do?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;--force:&lt;/strong&gt; Forces the command to rewrite your Git history.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;--index-filter:&lt;/strong&gt; Allows modification of the Git index.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;git rm --cached --ignore-unmatch .env.development:&lt;/strong&gt; Removes the .env.development file from the Git index but leaves it on your filesystem.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;--prune-empty:&lt;/strong&gt; Removes any empty commits that result from this operation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;--tag-name-filter cat:&lt;/strong&gt; Ensures that any tags in your repository are preserved.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;-- --all:&lt;/strong&gt; Applies this operation to all branches and tags.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 2: Clean Up Your Repository
&lt;/h2&gt;

&lt;p&gt;After removing the file, you need to clean up your Git repository to fully remove any traces of it. Run these commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git reflog expire --expire=now --all
git gc --prune=now --aggressive
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;git reflog expire --expire=now --all:&lt;/strong&gt; Expires all reflog entries immediately, clearing history that might still contain references to the removed file.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;git gc --prune=now --aggressive:&lt;/strong&gt; Runs garbage collection to remove unreferenced files and optimize the repository.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 3: Force Push the Changes to Your Remote Repository
&lt;/h2&gt;

&lt;p&gt;To apply these changes to your remote repository, you’ll need to force-push the updated history. Be cautious when doing this, especially in shared repositories.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git push origin --force --all
git push origin --force --tags
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Important Notes:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Force-pushing overwrites the remote history. Ensure that all collaborators are aware of these changes to avoid conflicts.&lt;/li&gt;
&lt;li&gt;Before force pushing, it’s a good idea to inform your team, as their local repositories will need to be synchronized with the updated history.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 4: Remove the File Locally and Update .gitignore
&lt;/h2&gt;

&lt;p&gt;Now that the file is removed from your Git history, delete it from your local repository and prevent it from being tracked again by adding it to .gitignore.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git rm --cached .env.development
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, add .env.development to your .gitignore file to prevent it from being accidentally committed again in the future. Open or create a .gitignore file in the root of your project and add:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.env.development
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;By following these steps, you can ensure that sensitive files are permanently removed from your Git history and that they won't be tracked in the future. This not only helps protect your project from security risks but also keeps your repository clean and organized.&lt;/p&gt;

&lt;p&gt;Remember to update your .gitignore file as a best practice to prevent sensitive files from being accidentally committed again. With these techniques, you can maintain a safer and more efficient Git repository.&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>git</category>
      <category>beginners</category>
      <category>security</category>
    </item>
    <item>
      <title>Beginner’s Guide to AES Encryption and Decryption in JavaScript using CryptoJS</title>
      <dc:creator>Shubham Khan</dc:creator>
      <pubDate>Wed, 16 Oct 2024 17:49:29 +0000</pubDate>
      <link>https://dev.to/shubhamkhan/beginners-guide-to-aes-encryption-and-decryption-in-javascript-using-cryptojs-592</link>
      <guid>https://dev.to/shubhamkhan/beginners-guide-to-aes-encryption-and-decryption-in-javascript-using-cryptojs-592</guid>
      <description>&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%2Fel5xl29trhts1ev8o1ea.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%2Fel5xl29trhts1ev8o1ea.PNG" alt="ASE Encryption" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In today’s digital world, safeguarding sensitive information is critical. One of the most trusted ways to secure data is by using AES (Advanced Encryption Standard). This guide will walk you through how to use AES for encryption and decryption in JavaScript with the help of the CryptoJS library, making it both secure and easy to implement.&lt;/p&gt;

&lt;p&gt;By the end of this post, you’ll have a solid understanding of how to encrypt data and decrypt it when needed. Let’s dive in!&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Data Security Matters
&lt;/h2&gt;

&lt;p&gt;In an age of increasing cyber threats, encryption is no longer optional—it’s essential. Whether you're building a simple web app or a complex platform, AES encryption helps ensure that your users' data remains safe and secure.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is AES Encryption?
&lt;/h2&gt;

&lt;p&gt;AES is a symmetric encryption method, meaning the same key is used to encrypt and decrypt data. It's a well-regarded standard due to its speed and security, making it a popular choice for protecting sensitive data like passwords, personal information, and financial details.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why You Need Encryption
&lt;/h2&gt;

&lt;p&gt;Data breaches, phishing attacks, and other threats make encryption essential for any web or mobile app that handles sensitive information. With AES encryption, even if an unauthorized person intercepts your data, they won’t be able to read it without the secret key.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is CryptoJS?
&lt;/h2&gt;

&lt;p&gt;CryptoJS is a robust JavaScript library that offers a range of cryptographic functionalities, including encryption, decryption, and hashing. It’s widely used for implementing AES encryption in web development, thanks to its simplicity and performance.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Does AES Work?
&lt;/h2&gt;

&lt;p&gt;Encryption and decryption using AES involve the following steps:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Encryption:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Plain text is transformed into unreadable ciphertext using a secret key.&lt;/li&gt;
&lt;li&gt;The encrypted data is then encoded into a safe format for transmission.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Decryption:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The ciphertext is decrypted back into the original plain text using the same secret key.&lt;/li&gt;
&lt;li&gt;Both processes ensure the confidentiality and integrity of the data being exchanged.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Implementing AES Encryption and Decryption in JavaScript
&lt;/h2&gt;

&lt;p&gt;Here’s a step-by-step guide to creating AES encryption and decryption functions in JavaScript using CryptoJS. We'll utilize environment variables to securely store our secret key.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AES Encryption Function&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const encryptWithSecretKey = (text) =&amp;gt; {
  // Fetch the secret key from environment variables
  const secretKey = process.env.NEXT_PUBLIC_SECRET_KEY?.replace(/\\n/g, "\n");

  // Generate a random Initialization Vector (IV) for security
  const iv = CryptoJS.lib.WordArray.random(16);

  // Encrypt the text using AES with CBC mode and the secret key
  const encrypted = CryptoJS.AES.encrypt(
    text,
    CryptoJS.enc.Hex.parse(secretKey),
    {
      iv: iv,
      padding: CryptoJS.pad.Pkcs7,
      mode: CryptoJS.mode.CBC,
    }
  );

  // Concatenate IV and ciphertext and encode in Base64 format
  const encryptedBase64 = CryptoJS.enc.Base64.stringify(
    iv.concat(encrypted.ciphertext)
  );

  return encryptedBase64;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Key Points:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The Initialization Vector (IV) is randomly generated for each encryption, adding an extra layer of security.&lt;/li&gt;
&lt;li&gt;The encryption process uses AES in CBC (Cipher Block Chaining) mode, making it more secure by using the IV along with the secret key.&lt;/li&gt;
&lt;li&gt;The result is encoded in Base64 for safe transmission in URLs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;AES Decryption Function&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const decryptWithSecretKey = (encryptedText) =&amp;gt; {
  try {
    const fullCipher = CryptoJS.enc.Base64.parse(encryptedText);

    // Extract IV and ciphertext from the parsed cipher
    const iv = CryptoJS.lib.WordArray.create(fullCipher.words.slice(0, 4), 16);
    const ciphertext = CryptoJS.lib.WordArray.create(fullCipher.words.slice(4));

    const cipherParams = CryptoJS.lib.CipherParams.create({
      ciphertext: ciphertext,
    });

    // Fetch and parse the secret key from environment variables
    const secretKey = process.env.NEXT_PUBLIC_SECRET_KEY?.replace(
      /\\n/g,
      "\n"
    );

    // Decrypt the ciphertext using AES and the provided secret key
    const decrypted = CryptoJS.AES.decrypt(
      cipherParams,
      CryptoJS.enc.Hex.parse(secretKey),
      {
        iv: iv,
        padding: CryptoJS.pad.Pkcs7,
        mode: CryptoJS.mode.CBC,
      }
    );

    // Return decrypted text in UTF-8 format
    return decrypted.toString(CryptoJS.enc.Utf8);
  } catch (error) {
    console.error("Decryption error:", error);
    return;
  }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Key Points:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Decryption reverses the encryption process by using the same IV and secret keys that were used for encryption.&lt;/li&gt;
&lt;li&gt;If the decryption fails due to an incorrect key or other issues, the error is logged and null is returned.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Essential Concepts
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Secret Key:&lt;/strong&gt; The key that encrypts and decrypts the data. This should be stored securely in environment variables, not hard-coded in your application.&lt;br&gt;
&lt;strong&gt;Initialization Vector (IV):&lt;/strong&gt; A unique value that makes the encryption more secure. It ensures that even the same input text results in different ciphertexts every time.&lt;br&gt;
&lt;strong&gt;Base64 Encoding:&lt;/strong&gt; After encryption, the ciphertext is Base64 encoded to make it suitable for storage or transmission, such as in URLs or APIs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Benefits of AES Encryption
&lt;/h2&gt;

&lt;p&gt;AES encryption provides several advantages:&lt;br&gt;
&lt;strong&gt;Performance:&lt;/strong&gt; AES is highly optimized for speed, making it ideal for web applications.&lt;br&gt;
&lt;strong&gt;Security:&lt;/strong&gt; AES has been rigorously tested and is widely regarded as one of the most secure encryption methods.&lt;br&gt;
&lt;strong&gt;Simplicity:&lt;/strong&gt; With libraries like CryptoJS, implementing AES encryption in JavaScript is straightforward and efficient.&lt;/p&gt;

&lt;h2&gt;
  
  
  Best Practices for Implementing AES
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Use environment variables to store sensitive data like your secret key. Never hard-code secrets in your application code.&lt;/li&gt;
&lt;li&gt;Always generate a new IV for each encryption operation to ensure the highest level of security.&lt;/li&gt;
&lt;li&gt;Handle errors properly during decryption to prevent issues like data loss or unauthorized access.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;AES encryption using CryptoJS is a powerful and efficient way to protect sensitive data in your JavaScript applications. With the steps outlined above, you can easily implement encryption and decryption for secure communication or data storage.&lt;/p&gt;

&lt;p&gt;This guide provides you with all the tools you need to secure your app and protect your users’ data. By following best practices, you can confidently handle sensitive information, knowing it’s encrypted and secure.&lt;/p&gt;

&lt;p&gt;Now that you’ve learned the basics of AES encryption, it’s time to put it into practice in your projects. Keep your data secure, and your users will trust your application more!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>security</category>
    </item>
    <item>
      <title>Mastering the Fundamentals of Object-Oriented Programming (OOP) in Java</title>
      <dc:creator>Shubham Khan</dc:creator>
      <pubDate>Sun, 13 Oct 2024 04:24:07 +0000</pubDate>
      <link>https://dev.to/shubhamkhan/mastering-the-fundamentals-of-object-oriented-programming-oop-in-java-1oma</link>
      <guid>https://dev.to/shubhamkhan/mastering-the-fundamentals-of-object-oriented-programming-oop-in-java-1oma</guid>
      <description>&lt;p&gt;Object-Oriented Programming (OOP) is one of the most important concepts in modern programming. It allows you to design software that is more flexible, modular, and easier to maintain. This article will take you through the four core pillars of OOP—Encapsulation, Inheritance, Polymorphism, and Abstraction—with practical examples in Java to help you get started.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Encapsulation: Safeguarding Data
&lt;/h2&gt;

&lt;p&gt;Encapsulation is the principle of bundling data (fields) and methods (functions) that operate on the data within a single class, restricting direct access to that data. This protects the data from being altered unexpectedly or improperly. Instead of accessing fields directly, you use public methods known as getters and setters.&lt;/p&gt;

&lt;p&gt;Here’s an example in Java:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Person {
    // Private variable to restrict access
    private int age;

    // Getter method to retrieve the age
    public int getAge() {
        return age;
    }

    // Setter method to update the age with validation
    public void setAge(int age) {
        if (age &amp;gt; 0) {
            this.age = age;
        } else {
            System.out.println("Age must be a positive number.");
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the age variable is marked as private to prevent direct access. The getAge() and setAge() methods allow controlled access to the age field, ensuring that only valid data is set. This approach encapsulates the data and safeguards it from external interference.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Inheritance: Reusing Functionality
&lt;/h2&gt;

&lt;p&gt;Inheritance allows one class to inherit the properties and methods of another, making it easier to reuse code and create relationships between objects. The class that inherits is called the "child" or "subclass," while the class being inherited from is the "parent" or "superclass."&lt;/p&gt;

&lt;p&gt;Here’s a simple example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Superclass
public class Animal {
    public void eat() {
        System.out.println("This animal eats.");
    }
}

// Subclass inheriting from Animal
public class Dog extends Animal {
    public void bark() {
        System.out.println("The dog barks.");
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the Dog class inherits the eat() method from the Animal class. This demonstrates how the Dog class can reuse methods from its parent class without needing to rewrite them.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Polymorphism: Flexibility in Action
&lt;/h2&gt;

&lt;p&gt;Polymorphism allows you to perform a single action in different ways depending on the object. It can be achieved through method overriding, where a subclass provides a specific implementation of a method that is already defined in its parent class.&lt;/p&gt;

&lt;p&gt;Take a look at this example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Superclass
public class Animal {
    public void speak() {
        System.out.println("The animal makes a sound.");
    }
}

// Subclass overriding the speak method
public class Dog extends Animal {
    @Override
    public void speak() {
        System.out.println("The dog barks.");
    }
}

// Subclass overriding the speak method
public class Cat extends Animal {
    @Override
    public void speak() {
        System.out.println("The cat meows.");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal myDog = new Dog();  // Polymorphism in action
        myDog.speak();  // Output: The dog barks

        Animal myCat = new Cat();
        myCat.speak();  // Output: The cat meows
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even though myDog and myCat are declared as type Animal, Java will invoke the appropriate method for each object. This is the power of polymorphism—it lets you handle different objects in a uniform way, yet their behaviour can vary.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Abstraction: Simplifying Complex Systems
&lt;/h2&gt;

&lt;p&gt;Abstraction is about hiding complex details and showing only the essential information. In Java, abstraction can be achieved using abstract classes or interfaces. These allow you to define methods that subclasses must implement, without specifying how they should work.&lt;/p&gt;

&lt;p&gt;Here’s an example using an abstract class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Abstract class
public abstract class Shape {
    // Abstract method with no implementation
    public abstract double calculateArea();
}

// Subclass implementing the abstract method
public class Rectangle extends Shape {
    private double width;
    private double height;

    public Rectangle(double width, double height) {
        this.width = width;
        this.height = height;
    }

    @Override
    public double calculateArea() {
        return width * height;
    }
}

public class Main {
    public static void main(String[] args) {
        Shape rectangle = new Rectangle(5, 10);
        System.out.println("Rectangle area: " + rectangle.calculateArea());  // Output: 50
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the Shape class defines an abstract method calculateArea(), which must be implemented by any subclass like Rectangle. This allows you to work with shapes without needing to know the specific details of how the area is calculated—simplifying the interaction with the object.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;By mastering the four fundamental principles of OOP—Encapsulation, Inheritance, Polymorphism, and Abstraction—you can create Java applications that are clean, maintainable, and scalable. Whether you're working on a small project or a large-scale system, these concepts will help you write better, more efficient code.&lt;/p&gt;

&lt;p&gt;So, dive into OOP with Java and start applying these principles in your own projects. Understanding these concepts will not only make you a better Java developer but also enhance your overall programming skills!&lt;/p&gt;

&lt;h1&gt;
  
  
  Java #OOP #Encapsulation #Inheritance #Polymorphism #Abstraction #JavaLearning #ProgrammingFundamentals #CodeNewbie #SoftwareDevelopment #Tech
&lt;/h1&gt;

</description>
      <category>java</category>
      <category>oop</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How to Generate RSA Key Using JavaScript and Python</title>
      <dc:creator>Shubham Khan</dc:creator>
      <pubDate>Sat, 12 Oct 2024 11:36:58 +0000</pubDate>
      <link>https://dev.to/shubhamkhan/how-to-generate-rsa-key-using-javascript-and-python-2l6h</link>
      <guid>https://dev.to/shubhamkhan/how-to-generate-rsa-key-using-javascript-and-python-2l6h</guid>
      <description>&lt;p&gt;In today’s digital age, ensuring the security of sensitive information is paramount. RSA, one of the most widely used encryption techniques, helps achieve that by allowing secure communication and data protection. If you’re a beginner looking to learn how to generate RSA key pairs, this tutorial will guide you through the process in both JavaScript and Python.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is RSA?
&lt;/h2&gt;

&lt;p&gt;RSA (Rivest-Shamir-Adleman) is a public-key cryptosystem used for secure data transmission. It uses two keys:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Public key:&lt;/strong&gt; Used for encrypting messages or verifying digital signatures.&lt;br&gt;
&lt;strong&gt;Private key:&lt;/strong&gt; Used for decrypting messages or signing documents.&lt;br&gt;
The keys are mathematically linked, but it’s nearly impossible to derive the private key from the public key, making RSA a highly secure encryption method.&lt;/p&gt;
&lt;h2&gt;
  
  
  Why Use RSA?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Data Encryption:&lt;/strong&gt; Encrypt sensitive information to secure communication.&lt;br&gt;
&lt;strong&gt;Digital Signatures:&lt;/strong&gt; Verify the authenticity of messages or documents.&lt;br&gt;
&lt;strong&gt;Authentication:&lt;/strong&gt; Provide secure authentication mechanisms.&lt;br&gt;
Now, let’s explore how to generate RSA keys using JavaScript and Python, two popular programming languages.&lt;/p&gt;
&lt;h2&gt;
  
  
  Generating RSA Keys Using OpenSSL (Command Line)
&lt;/h2&gt;

&lt;p&gt;Before diving into code, you can easily generate RSA keys using OpenSSL. OpenSSL is a widely-used tool for cryptographic operations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Generate a 2048-bit RSA private key:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;openssl genpkey -algorithm RSA -out private_key.pem -pkeyopt rsa_keygen_bits:2048
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Extract the public key:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;openssl rsa -pubout -in private_key.pem -out public_key.pem
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;This will produce two files:&lt;/strong&gt; private_key.pem (the private key) and public_key.pem (the public key).&lt;/p&gt;

&lt;h2&gt;
  
  
  Generating RSA Keys in JavaScript
&lt;/h2&gt;

&lt;p&gt;In JavaScript, you can use Node.js's crypto module to generate RSA keys. Here’s a step-by-step guide.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step-by-Step Guide (JavaScript)&lt;/strong&gt;&lt;br&gt;
Set up your environment: Ensure that you have Node.js installed on your system. If not, you can download it from nodejs.org.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Generate the RSA key pair:&lt;/strong&gt; Node.js provides the crypto module, which offers built-in functions for cryptographic operations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { generateKeyPairSync } = require('crypto');

const { publicKey, privateKey } = generateKeyPairSync('rsa', {
  modulusLength: 2048,
  publicKeyEncoding: {
    type: 'spki',
    format: 'pem'
  },
  privateKeyEncoding: {
    type: 'pkcs8',
    format: 'pem'
  }
});

console.log('Public Key:\n', publicKey);
console.log('Private Key:\n', privateKey);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Key Points:&lt;/strong&gt;&lt;br&gt;
modulusLength: 2048 ensures that the key is 2048 bits long.&lt;br&gt;
The generated public and private keys are returned in PEM format, which is widely used for storing cryptographic keys.&lt;br&gt;
With this script, you can now generate RSA keys in JavaScript and use them for encryption, digital signatures, or secure communication.&lt;/p&gt;
&lt;h2&gt;
  
  
  Generating RSA Keys in Python
&lt;/h2&gt;

&lt;p&gt;Python provides an excellent library called cryptography for generating RSA keys. Follow the steps below to generate RSA keys in Python.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step-by-Step Guide (Python)&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Install the cryptography library:&lt;/strong&gt; If you don’t have the cryptography library installed, you can easily install it using pip:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install cryptography
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Generate RSA Keys:&lt;/strong&gt; After installing the library, use the following Python script to generate your RSA key pair.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives import serialization

# Generate a private key
private_key = rsa.generate_private_key(
    public_exponent=65537,
    key_size=2048,
    backend=default_backend()
)

# Save the private key in PEM format
with open("private_key.pem", "wb") as private_file:
    private_file.write(
        private_key.private_bytes(
            encoding=serialization.Encoding.PEM,
            format=serialization.PrivateFormat.TraditionalOpenSSL,
            encryption_algorithm=serialization.NoEncryption()
        )
    )

# Generate the public key from the private key
public_key = private_key.public_key()

# Save the public key in PEM format
with open("public_key.pem", "wb") as public_file:
    public_file.write(
        public_key.public_bytes(
            encoding=serialization.Encoding.PEM,
            format=serialization.PublicFormat.SubjectPublicKeyInfo
        )
    )

print("Keys generated and saved as PEM files!")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Key Points:&lt;/strong&gt;&lt;br&gt;
This script generates a private key with a length of 2048 bits and a public exponent of 65537 (a common choice for RSA).&lt;br&gt;
The private and public keys are stored in PEM format for easy use.&lt;br&gt;
Understanding the Output&lt;br&gt;
&lt;strong&gt;Whether you’re working in JavaScript or Python, the output will be your RSA key pair:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Private Key:&lt;/strong&gt; This key is used to decrypt data or sign documents. It should be kept confidential.&lt;br&gt;
&lt;strong&gt;Public Key:&lt;/strong&gt; This key is used to encrypt data or verify signatures. It can be safely shared with others.&lt;br&gt;
Here’s what an RSA private key looks like in PEM format:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEA...
-----END RSA PRIVATE KEY-----
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Practical Uses of RSA Keys
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Encrypting and Decrypting Messages:&lt;/strong&gt; Encrypt a message using the public key and decrypt it with the corresponding private key.&lt;br&gt;
&lt;strong&gt;Signing and Verifying Documents:&lt;/strong&gt; Sign a document with the private key, and anyone with the public key can verify the signature’s authenticity.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;RSA key generation is essential for secure communication, encryption, and digital signatures. In this guide, we demonstrated how to generate RSA key pairs in both JavaScript and Python. Using the Node.js crypto module and Python’s cryptography library, you can easily generate secure RSA key pairs for your applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Takeaways:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;RSA involves a pair of keys:&lt;/strong&gt; a public key and a private key.&lt;br&gt;
The process of generating RSA keys in JavaScript and Python is straightforward with the right libraries.&lt;br&gt;
These keys can be used for various security purposes, such as encryption, authentication, and digital signatures.&lt;br&gt;
By following this guide, you can start implementing robust security features in your projects. Happy coding!&lt;/p&gt;

&lt;h2&gt;
  
  
  FAQs
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Q: What is the ideal RSA key size for strong encryption?&lt;/strong&gt;&lt;br&gt;
A: A 2048-bit key is commonly used, but for stronger security, 4096-bit keys are recommended.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q: Can I share my private key?&lt;/strong&gt;&lt;br&gt;
A: No, the private key should always remain confidential. Only the public key can be shared.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q: What is PEM format?&lt;/strong&gt;&lt;br&gt;
A: PEM (Privacy-Enhanced Mail) is a base64-encoded format used for storing cryptographic keys and certificates.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>python</category>
      <category>security</category>
    </item>
    <item>
      <title>Understanding JavaScript Hoisting: A Simple Guide</title>
      <dc:creator>Shubham Khan</dc:creator>
      <pubDate>Sun, 06 Oct 2024 13:28:54 +0000</pubDate>
      <link>https://dev.to/shubhamkhan/understanding-javascript-hoisting-a-simple-guide-59k0</link>
      <guid>https://dev.to/shubhamkhan/understanding-javascript-hoisting-a-simple-guide-59k0</guid>
      <description>&lt;p&gt;If you're new to JavaScript, you may have run into confusing situations where variables seem to be undefined or errors like ReferenceError pop up unexpectedly. This can often be traced back to a concept known as hoisting. But what is hoisting, and how does it affect your code?&lt;/p&gt;

&lt;p&gt;In this guide, we'll break down the concept of hoisting and how it works in JavaScript. By the end, you'll understand why hoisting happens and how you can avoid common mistakes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is Hoisting?&lt;/strong&gt;&lt;br&gt;
Hoisting is JavaScript’s behavior of moving variable and function declarations to the top of their scope before the code runs. This means that declarations (not the assignments) are processed during a preparation phase before the actual execution of your code.&lt;/p&gt;

&lt;p&gt;JavaScript goes through a creation phase first, where it allocates memory for variables and functions. However, the way it handles functions and variables is slightly different.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Function Hoisting: Fully Hoisted Definitions&lt;/strong&gt;&lt;br&gt;
Functions declared using the function keyword are hoisted with their full definition. This allows you to call or use a function before its actual declaration in the code.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sum(5, 3); // Output: 8

function sum(a, b) {
  console.log(a + b);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even though the sum() function is called before it’s declared in the code, it works perfectly because the function declaration is hoisted to the top of its scope during the creation phase.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Variable Hoisting: var, let, and const&lt;/strong&gt;&lt;br&gt;
Variable hoisting behaves differently from function hoisting, and it varies depending on whether you use var, let, or const.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. var Declarations&lt;/strong&gt;&lt;br&gt;
When you declare a variable using var, it is hoisted to the top of its scope with a default value of undefined. This means that you can access the variable before its declaration, but until you assign a value to it, the variable will hold undefined.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(city); // Output: undefined
var city = "New York";
console.log(city); // Output: "New York"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, city is hoisted with a value of undefined initially. Once the value "New York" is assigned, the second console.log() correctly outputs "New York."&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. let and const Declarations&lt;/strong&gt;&lt;br&gt;
With let and const, variables are also hoisted, but they remain uninitialized. This means that if you try to access them before their declaration, you'll get a ReferenceError.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(name); // ReferenceError: Cannot access 'name' before initialization
let name = "John Doe";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This error happens because let and const variables exist in something called the Temporal Dead Zone (TDZ) between the start of their scope and the point where they are actually declared. During this time, you cannot reference the variable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Difference Between let and const&lt;/strong&gt;&lt;br&gt;
Both let and const behave similarly in terms of hoisting, but const requires you to assign a value during declaration, while let allows you to declare a variable without immediately assigning a value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const name = "John Doe"; // Must be initialized
let age; // Can be declared without assignment
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Hoisting in Practice&lt;/strong&gt;&lt;br&gt;
Let’s look at an example that demonstrates both function and variable hoisting in action:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(city); // Output: undefined
sum(3, 4);    // Output: 7

function sum(x, y) {
  console.log(x + y);
}

var city = "New York";
console.log(city); // Output: "New York"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the sum function is hoisted with its full definition, so it can be called before the function is declared. However, the city is hoisted with a value of undefined, which explains why the first console.log() outputs undefined. Once the assignment occurs, the second console.log() outputs the correct value.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tips for Avoiding Hoisting Pitfalls&lt;/strong&gt;&lt;br&gt;
To avoid issues caused by hoisting, follow these best practices:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;- Use let and const instead of var to avoid accessing variables before their declaration.&lt;/li&gt;
&lt;li&gt;- Declare variables and functions at the top of their scope to ensure your code behaves predictably.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Recap of Key Hoisting Concepts&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hoisting refers to JavaScript’s behavior of moving declarations to the top of their scope before the code runs.&lt;/li&gt;
&lt;li&gt;Functions declared with function are hoisted with their full definitions, allowing them to be used before they are declared.&lt;/li&gt;
&lt;li&gt;Variables declared with var are hoisted with a default value of undefined, while variables declared with let and const remain uninitialized, causing a ReferenceError if accessed before declaration.&lt;/li&gt;
&lt;li&gt;The Temporal Dead Zone (TDZ) applies to let and const, preventing them from being accessed before they are initialized.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By understanding hoisting, you can avoid common issues in JavaScript and write more predictable code. With practice, these concepts will become second nature.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Enhancing Performance in JavaScript: Understanding Debouncing and Throttling</title>
      <dc:creator>Shubham Khan</dc:creator>
      <pubDate>Sat, 05 Oct 2024 19:36:02 +0000</pubDate>
      <link>https://dev.to/shubhamkhan/enhancing-performance-in-javascript-understanding-debouncing-and-throttling-pe3</link>
      <guid>https://dev.to/shubhamkhan/enhancing-performance-in-javascript-understanding-debouncing-and-throttling-pe3</guid>
      <description>&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
Debouncing: A Strategic Delay

&lt;ul&gt;
&lt;li&gt;How Debouncing Works&lt;/li&gt;
&lt;li&gt;Debouncing Example&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

Throttling: Controlling Event Frequency

&lt;ul&gt;
&lt;li&gt;How Throttling Works&lt;/li&gt;
&lt;li&gt;Throttling Example&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

Implementing in React: Debounce and Throttle with Custom Hooks

&lt;ul&gt;
&lt;li&gt;Custom Hook for Debouncing&lt;/li&gt;
&lt;li&gt;Using the Debounce Hook&lt;/li&gt;
&lt;li&gt;Custom Hook for Throttling&lt;/li&gt;
&lt;li&gt;Using the Throttle Hook&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Final Thoughts&lt;/li&gt;

&lt;/ul&gt;




&lt;p&gt;Performance optimization is critical in modern web applications, especially those that involve user interactions like typing in a search bar, scrolling, or resizing a window. These actions can fire off many function calls in a short time, which can degrade performance. &lt;/p&gt;

&lt;p&gt;To mitigate this, two common techniques are &lt;strong&gt;debouncing&lt;/strong&gt; and &lt;strong&gt;throttling&lt;/strong&gt;, which allow you to control the rate at which a function is invoked, leading to a smoother, more efficient experience.&lt;/p&gt;

&lt;h2&gt;
  
  
  Debouncing: A Strategic Delay
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Debouncing&lt;/strong&gt; delays the execution of a function until a specified time has passed since the last event trigger. It is particularly helpful when dealing with events like search inputs, where you want to avoid making an API request on every keystroke.&lt;/p&gt;

&lt;h3&gt;
  
  
  How Debouncing Works
&lt;/h3&gt;

&lt;p&gt;Imagine a search input where you want to wait until the user has stopped typing for 300ms before making an API request. Debouncing allows you to ensure that the function is only executed after the user has paused typing, preventing unnecessary API calls.&lt;/p&gt;

&lt;h3&gt;
  
  
  Debouncing Example
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;debounce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;func&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;context&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;args&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arguments&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;clearTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;timeout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;func&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nx"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;searchAPI&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;API request made&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;debouncedSearch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;debounce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;searchAPI&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nf"&gt;debouncedSearch&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Only triggers 300ms after the last call&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the API request will only be made if the user pauses for 300ms.&lt;/p&gt;

&lt;h2&gt;
  
  
  Throttling: Controlling Event Frequency
&lt;/h2&gt;

&lt;p&gt;In contrast to debouncing, &lt;strong&gt;throttling&lt;/strong&gt; ensures that a function is called at most once every specified interval, even if the event continues to trigger. This technique is ideal for scenarios like window resizing or scrolling, where the events fire continuously.&lt;/p&gt;

&lt;h3&gt;
  
  
  How Throttling Works
&lt;/h3&gt;

&lt;p&gt;Throttling allows a function to execute only once during a defined period (e.g., 200ms), ensuring that the function is not overwhelmed by repeated triggers.&lt;/p&gt;

&lt;h3&gt;
  
  
  Throttling Example
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;throttle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;func&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;lastFunc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;lastRan&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;context&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;args&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arguments&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;lastRan&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;func&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nx"&gt;lastRan&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;clearTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;lastFunc&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nx"&gt;lastFunc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;lastRan&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nx"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nx"&gt;func&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
          &lt;span class="nx"&gt;lastRan&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;limit&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;lastRan&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;updateLayout&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Layout updated&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;throttledUpdate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;throttle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;updateLayout&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;resize&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;throttledUpdate&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the layout update function will only be called once every 200ms during window resizing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementing in React: Debounce and Throttle with Custom Hooks
&lt;/h2&gt;

&lt;p&gt;In React, we can use custom hooks to make the debounce and throttle functionality reusable across components. This enhances modularity and optimizes performance in various interactions.&lt;/p&gt;

&lt;h3&gt;
  
  
  Custom Hook for Debouncing
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useRef&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useCallback&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;useDebounce&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;func&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;timer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useRef&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;useCallback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;timer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;clearTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;timer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="nx"&gt;timer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nx"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;func&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;useDebounce&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Using the Debounce Hook
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;useDebounce&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./useDebounce&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;SearchComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;searchTerm&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setSearchTerm&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fetchResults&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Fetching results for &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;debouncedFetch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useDebounce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fetchResults&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleSearch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setSearchTerm&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;debouncedFetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;searchTerm&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;onChange&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleSearch&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;placeholder&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Search...&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;SearchComponent&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Custom Hook for Throttling
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useRef&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useCallback&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;useThrottle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;func&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;lastRun&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useRef&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;useCallback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;now&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;now&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;lastRun&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nx"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;lastRun&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;now&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;func&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;useThrottle&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Using the Throttle Hook
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useEffect&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;useThrottle&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./useThrottle&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ScrollComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleScroll&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Scrolled!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;throttledScroll&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useThrottle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;handleScroll&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;scroll&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;throttledScroll&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;removeEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;scroll&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;throttledScroll&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;throttledScroll&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{{&lt;/span&gt; &lt;span class="na"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;200vh&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Scroll&lt;/span&gt; &lt;span class="nx"&gt;down&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;see&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;effect&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;ScrollComponent&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Both &lt;strong&gt;debouncing&lt;/strong&gt; and &lt;strong&gt;throttling&lt;/strong&gt; are indispensable techniques to enhance performance in modern applications. While debouncing is ideal for inputs like search fields, throttling is best suited for high-frequency events like scrolling. Custom hooks in React, like &lt;code&gt;useDebounce&lt;/code&gt; and &lt;code&gt;useThrottle&lt;/code&gt;, make these optimizations easy to implement across your app, ensuring a more efficient, responsive experience.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>performance</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Securing Data with RSA Encryption and Decryption Across Platforms</title>
      <dc:creator>Shubham Khan</dc:creator>
      <pubDate>Sun, 29 Sep 2024 11:49:33 +0000</pubDate>
      <link>https://dev.to/shubhamkhan/securing-data-with-rsa-encryption-and-decryption-across-platforms-30d7</link>
      <guid>https://dev.to/shubhamkhan/securing-data-with-rsa-encryption-and-decryption-across-platforms-30d7</guid>
      <description>&lt;h2&gt;
  
  
  Introduction to RSA Encryption
&lt;/h2&gt;

&lt;p&gt;In today's digital landscape, securing sensitive data is crucial for individuals and organizations alike. RSA (Rivest-Shamir-Adleman) encryption stands out as a robust solution for protecting data. It is an asymmetric encryption algorithm, which means that it uses a pair of keys: a public key for encryption and a private key for decryption. One of the main benefits of RSA encryption is that the private key never needs to be shared, which minimizes the risk of it being compromised.&lt;/p&gt;

&lt;p&gt;This article explores how to use RSA encryption across three popular programming languages—JavaScript, Python, and PHP—making it easier to secure data in cross-platform applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cross-Platform Encryption and Decryption: The Scenario
&lt;/h2&gt;

&lt;p&gt;Imagine you're building a web application where sensitive information (like authentication data or personal details) must be securely transmitted between the client (front end) and the server (back end). For instance, you might encrypt a message on the client side in JavaScript and then decrypt it on the server using either Python or PHP.&lt;/p&gt;

&lt;p&gt;RSA is well-suited for this scenario because it provides the flexibility of encryption in one language and decryption in another, ensuring cross-platform compatibility.&lt;/p&gt;

&lt;h2&gt;
  
  
  RSA Implementation: JavaScript, Python, and PHP
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;JavaScript (Next.js with JSEncrypt)&lt;br&gt;
Encryption:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import JSEncrypt from 'jsencrypt';

// Function to encrypt a message using a public key
const encryptWithPublicKey = (message) =&amp;gt; {
  const encryptor = new JSEncrypt();
  const publicKey = process.env.NEXT_PUBLIC_PUBLIC_KEY.replace(/\\n/g, "\n");
  encryptor.setPublicKey(publicKey);
  const encryptedMessage = encryptor.encrypt(message);
  return encryptedMessage;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Decryption:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import JSEncrypt from 'jsencrypt';

// Function to decrypt a message using a private key
const decryptWithPrivateKey = (encryptedMessage) =&amp;gt; {
  const decryptor = new JSEncrypt();
  const privateKey = process.env.PRIVATE_KEY.replace(/\\n/g, "\n");
  decryptor.setPrivateKey(privateKey);
  const decryptedMessage = decryptor.decrypt(encryptedMessage);
  return decryptedMessage;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Public Key Encryption:&lt;/strong&gt;  The JSEncrypt library encrypts the message using the public key. This ensures that only the corresponding private key can decrypt it.&lt;br&gt;
&lt;strong&gt;Private Key Decryption:&lt;/strong&gt; The message is decrypted with the private key, which is securely stored in an environment variable.&lt;br&gt;
&lt;strong&gt;Security Consideration:&lt;/strong&gt; By using RSA, we ensure that the data sent from the client is encrypted and secure.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python (using rsa library)&lt;br&gt;
Encryption:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import rsa
import base64

def encrypt_with_public_key(message: str, public_key_str: str) -&amp;gt; str:
    public_key = rsa.PublicKey.load_pkcs1_openssl_pem(public_key_str.encode())
    encrypted_message = rsa.encrypt(message.encode(), public_key)
    return base64.b64encode(encrypted_message).decode()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Decryption:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import rsa
import base64

def decrypt_with_private_key(encrypted_message: str, private_key_str: str) -&amp;gt; str:
    private_key = rsa.PrivateKey.load_pkcs1(private_key_str.encode())
    encrypted_bytes = base64.b64decode(encrypted_message.encode())
    decrypted_message = rsa.decrypt(encrypted_bytes, private_key)
    return decrypted_message.decode()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Public Key Encryption:&lt;/strong&gt; The message is encrypted using a public key, ensuring that only the intended private key holder can decrypt it.&lt;br&gt;
&lt;strong&gt;Base64 Encoding:&lt;/strong&gt; After encryption, the message is Base64 encoded to ensure compatibility with text transmission.&lt;br&gt;
&lt;strong&gt;Private Key Decryption:&lt;/strong&gt; The private key is used to decrypt the Base64-encoded encrypted message, ensuring confidentiality.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PHP (using OpenSSL)&lt;br&gt;
Encryption:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function encrypt_with_public_key($message) {
    $publicKey = getenv('PUBLIC_KEY');
    openssl_public_encrypt($message, $encrypted, $publicKey);
    return base64_encode($encrypted);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Decryption:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function decrypt_with_private_key($encryptedMessage) {
    $privateKey = getenv('PRIVATE_KEY');
    $encryptedData = base64_decode($encryptedMessage);
    openssl_private_decrypt($encryptedData, $decrypted, $privateKey);
    return $decrypted;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Public Key Encryption:&lt;/strong&gt; The openssl_public_encrypt function encrypts the message using the public key, ensuring that only the private key can decrypt it.&lt;br&gt;
&lt;strong&gt;Private Key Decryption:&lt;/strong&gt; The openssl_private_decrypt function decrypts the message using the private key, ensuring that sensitive information remains secure.&lt;br&gt;
&lt;strong&gt;Environment Variables:&lt;/strong&gt; Both the public and private keys are securely stored in environment variables, enhancing security.&lt;/p&gt;

&lt;h2&gt;
  
  
  Best Practices for Encryption
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Use Environment Variables:&lt;/strong&gt; Always store your keys in environment variables instead of hard-coding them into your application. This reduces the risk of exposing sensitive information.&lt;br&gt;
&lt;strong&gt;Encrypt Sensitive Data:&lt;/strong&gt; Encrypt personal and sensitive data such as passwords, financial details, or personally identifiable information (PII) to prevent unauthorized access.&lt;br&gt;
&lt;strong&gt;Use HTTPS:&lt;/strong&gt; Ensure your application communicates over HTTPS to safeguard data in transit.&lt;br&gt;
&lt;strong&gt;Secure Key Management:&lt;/strong&gt; Regularly rotate encryption keys and ensure they are stored securely.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Choose RSA Encryption?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Enhanced Data Security:&lt;/strong&gt; RSA encryption ensures that sensitive data is kept secure during transmission, preventing unauthorized access.&lt;br&gt;
&lt;strong&gt;Asymmetric Encryption:&lt;/strong&gt; RSA uses a public key for encryption and a private key for decryption, which ensures the private key never needs to be shared.&lt;br&gt;
&lt;strong&gt;Cross-Platform Compatibility:&lt;/strong&gt; RSA works seamlessly across different platforms and programming languages, making it ideal for web applications where different technologies are used on the client and server sides.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;RSA encryption offers a reliable way to secure sensitive data across multiple programming environments. By implementing RSA encryption and decryption in JavaScript, Python, and PHP, you can protect sensitive information, enhance security, and ensure cross-platform compatibility. Whether it's for securing API calls, safeguarding user data, or ensuring the confidentiality of messages, RSA provides a robust encryption solution.&lt;/p&gt;

&lt;p&gt;If you found this guide helpful, consider sharing it with fellow developers, and stay tuned for more insights into encryption and data security!&lt;/p&gt;

&lt;h1&gt;
  
  
  Encryption #RSA #CyberSecurity #DataSecurity #WebDevelopment #CrossPlatformSecurity #JavaScript #Python #PHP
&lt;/h1&gt;

</description>
      <category>javascript</category>
      <category>python</category>
      <category>php</category>
      <category>rsa</category>
    </item>
  </channel>
</rss>
