# Project 6: Password Generator

> Learn to build a simple yet powerful password generator in Dart. This step-by-step guide teaches you how to work with user input, generate random numbers, and manipulate strings in Dart.

- URL: https://www.appwriters.dev/projects/dart/project6
- Series: Dart projects

---

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:

```dart
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.

```dart
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:

```dart
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!
