Random Password Generator Development

LinuxLinuxBeginner
Practice Now

Introduction

In this project, you will learn how to create a random password generator script that meets specific requirements. The password generator will create a 12-character password that includes at least one digit, one uppercase letter, one lowercase letter, and one special character from the set ><+-{}:.&;.

👀 Preview

$ cd /home/labex/project
$ sh genpass.sh
## Example
2Dsxw9+xS:27

ðŸŽŊ Tasks

In this project, you will learn:

  • How to set up the project environment and create the necessary script file
  • How to implement the logic to generate a random password that meets the specified requirements
  • How to test the password generator script to ensure it works as expected

🏆 Achievements

After completing this project, you will be able to:

  • Understand the process of creating a random password generator script
  • Implement a password generation algorithm that meets specific requirements
  • Test and validate the generated passwords to ensure they meet the desired criteria

Set Up the Project Environment

In this step, you will set up the project environment and create the necessary file for the password generator script.

  1. Open the terminal and navigate to the /home/labex/project directory.
cd /home/labex/project
  1. Create a new file named genpass.sh using the touch command.
touch genpass.sh
  1. Open the genpass.sh file in a text editor and add the following shebang line at the beginning of the file:
#!/bin/zsh

## Random Password Generator
## This script generates a random password that meets the specified requirements.

This line tells the system to use the zsh shell to execute the script.

Implement the Password Generation Logic

In this step, you will implement the logic to generate a random password that meets the specified requirements.

  1. In the genpass.sh file, add the following function to generate the password:
## Function to generate a random password
generate_password() {
  local length=12
  local password=''

  ## Special characters
  local special_chars='><+-{}:.&;'
  local special_char="${special_chars:$RANDOM%${#special_chars}:1}"
  password+="$special_char"

  ## Digits
  local digits='0123456789'
  local digit="${digits:$RANDOM%${#digits}:1}"
  password+="$digit"

  ## Uppercase letters
  local upper_case='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  local upper="${upper_case:$RANDOM%${#upper_case}:1}"
  password+="$upper"

  ## Lowercase letters
  local lower_case='abcdefghijklmnopqrstuvwxyz'
  local lower="${lower_case:$RANDOM%${#lower_case}:1}"
  password+="$lower"

  ## Remaining characters
  local remaining_length=$((length - 4))
  local characters='><+-{}:.&;0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
  local num_characters=${#characters}

  for ((i = 0; i < remaining_length; i++)); do
    local random_char="${characters:$RANDOM%$num_characters:1}"
    password+="$random_char"
  done

  ## Shuffle the order of password characters
  password=$(echo "$password" | fold -w1 | shuf | tr -d '\n')

  echo "$password"
}

This function generates a random password that meets the specified requirements.

  1. Add the following code to the end of the genpass.sh file to call the generate_password function and output the generated password:
## Generate password and output
generate_password

Test the Password Generator

In this step, you will test the password generator script to ensure it works as expected.

  1. Save the genpass.sh file.
  2. In the terminal, run the script to generate a password:
sh genpass.sh

The script should output a random password that meets the requirements:

## Example
2Dsxw9+xS:27
  1. Run the script a few more times to ensure it generates different passwords each time.

Congratulations! You have successfully implemented a random password generator that meets the specified requirements.

âœĻ Check Solution and Practice

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

Other Linux Tutorials you may like