Skip to main content ppwriters

#DartProjects

Project 6

Building a Password Generator in Dart

Welcome back, Dart learners! We have been on a journey together, creating some cool projects. Remember the temperature converter, simple arithmetic calculator, Todo list application, number guessing game, and text analyzer we made? Now, it’s time to add one more to our list. Today, we’ll be building a simple, yet very useful tool: a Password Generator.

Overview

Before we start, let’s have a quick overview of what we are going to create. We are going to build a program in Dart that generates a random password for the user. The length of the password will be decided by the user input.

Setting Up

As usual, we start by importing the Dart libraries we need:

import 'dart:math';
import 'dart:io';
  • dart:math: This library will help us generate random numbers, which will be used to create random characters for our password.
  • dart:io: This is used for reading user input from the console.

Main Function

Every Dart program starts with a main() function. In our case, it’s going to ask the user to enter the length of the password they want. Then, it will call the generatePassword function and finally print the generated password.

void main() {
  stdout.write('Enter password length: ');
  var input = stdin.readLineSync();
  var length = int.tryParse(input ?? '12') ?? 12;
  var password = generatePassword(length);
  print('Random password: $password');
}

Think of stdout.write as your way of communicating with the user, asking for their input, and stdin.readLineSync() as the user’s way of communicating back to you. int.tryParse is used to convert this input string into an integer, which represents the length of the password.

Generating the Password

The heart of our program is the generatePassword function. This is where the magic happens:

String generatePassword(int length) {
  var rand = Random.secure();
  var codeUnits = List.generate(length, (index) {
    return rand.nextInt(93) + 33;
  });
  return String.fromCharCodes(codeUnits);
}

Let’s break it down:

  1. We start by creating a new instance of Random.secure(). This gives us a random number generator.

  2. Then, we create a list of integers, each of which represents a character code, using List.generate(). The size of the list is the desired length of the password.

  3. Inside List.generate(), we create random numbers in the range 33-126. In the ASCII table, this range represents almost all printable characters, which includes letters (both lower and upper case), numbers, and special characters.

  4. Finally, we convert these character codes to actual characters using String.fromCharCodes(), which gives us our random password.

And voila! You’ve just built a password generator.

Recap

In this blog post, we built a random password generator using Dart. This little utility is a practical and powerful tool to enhance security. As you can see, Dart is a versatile language that can be used to build all sorts of applications. Keep exploring and happy coding!

Enjoying? Tell your friends.

#DartProjects

Learn the basics of Dart by building projects. Learn to build various projects step by step using Dart along the way brush up your Dart programming skills.

back to Dart projects

Join our community on Discord to connect with fellow learners, share your progress, and get help with any questions you may have throughout the #DartProject challenge. Join now and get started on your journey to mastering Dart!

join now