Building a Secure Password Generator: A JavaScript Project
In today’s digital world, having strong, unique passwords is more important than ever. That’s why I built a password generator application that helps users create secure passwords with customizable options as a part of a Scrimba solo Project. In this post, I’ll walk you through the key features and implementation details of this project.
Features
The password generator comes with several user-friendly features:
- Generate two random passwords simultaneously
- Customize password length using a slider (1-20 characters)
- Option to include numbers and symbols
- One-click copy to clipboard functionality
- Clean, modern user interface
- Mobile responsiveness
Implementation
Let’s dive into the core implementation. The application is built using vanilla JavaScript, HTML, and CSS, making it lightweight and fast.
Character Set
The application uses a comprehensive character set that includes:
- Uppercase letters (A-Z)
- Lowercase letters (a-z)
- Numbers (0-9)
- Special symbols (~, !, @, #, etc.)
const characters = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","0", "1", "2", "3", "4", "5", "6", "7", "8", "9","~","`","!","@","#","$","%","^","&","*","(",")","_","-","+","=","{","[","}","]",",","|",":",";","<",">",".","?","/"];
Password Generation Logic
The core password generation function handles different combinations of character sets based on user preferences:
function generatePwd() { let passwordOne = '' let passwordTwo = ''
checkNumbers() checkSymbols()
if (numbersChecked === false && symbolsChecked === false) { // Generate password with only letters // it takes only the first 53 entries of the array ( only letters ) for (let i = 0; i < pwdLength; i++) { const indexOne = Math.floor(Math.random() * 52); const indexTwo = Math.floor(Math.random() * 52); passwordOne += characters[indexOne]; passwordTwo += characters[indexTwo]; } } else if (numbersChecked === true && symbolsChecked === false) { // Generate password with letters and numbers for (let i = 0; i < pwdLength; i++) { const indexOne = Math.floor(Math.random() * 62); const indexTwo = Math.floor(Math.random() * 62); passwordOne += characters[indexOne]; passwordTwo += characters[indexTwo]; } } // ... additional conditions for other combinations}
User Interface
The HTML structure provides a clean and intuitive interface:
<div id="container"> <div id="container-div"> <h1>Generate a <br><span id="highlight">random Password</span></h1> <h2>Never use an insecure password again.</h2> <div class="button-check-ctn"> <button onclick="generatePwd()">Generate passwords</button> <div class="checkbox-wrapper"> <input type="checkbox" class="custom-checkbox" id="check-numbers"> <label for="check-numbers">Numbers</label> </div> <div class="checkbox-wrapper"> <input type="checkbox" class="custom-checkbox" id="check-symbols"> <label for="check-symbols">Symbols</label> </div> </div> <!-- Password display and copy functionality --> </div></div>
Copy to Clipboard Functionality
The application includes a convenient copy-to-clipboard feature:
function copyPwdOne(passwordField) { const tempInput = document.createElement("input"); tempInput.value = pwdFieldOne.textContent; document.body.appendChild(tempInput); tempInput.select(); navigator.clipboard.writeText(tempInput.value).then(() => { alert("Password copied" + " " + tempInput.value); }); document.body.removeChild(tempInput);}
Security Considerations
While this password generator provides a good starting point for creating secure passwords, it’s important to note that:
- The passwords are generated client-side, which means they’re not transmitted over the internet
- The application uses JavaScript’s
Math.random()
for randomization, which is suitable for this use case - Users can customize the complexity of their passwords based on their needs
Conclusion
This password generator project demonstrates how to create a practical, user-friendly tool using vanilla JavaScript. The code is clean, modular, and easy to understand, making it a great learning resource for those interested in web development.
The application was created as part of a Scrimba solo project, showcasing how to build practical tools while learning web development fundamentals. You can find the complete source code on GitHub and try out the live version to generate your own secure passwords at pwd.elimer.de.