Random Password Generator Development

LinuxLinuxBeginner
Practice Now

Introduction

In this project, you'll learn how to create a random password generator script that meets specific security requirements. The script will generate a 12-character password that includes at least one digit, one uppercase letter, one lowercase letter, and one special character from the set ><+-{}:.&;. This project will provide you with hands-on experience in scripting and working with random data.

๐Ÿ‘€ Preview

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

๐ŸŽฏ Tasks

In this project, you will:

  • Set up the project environment and create the necessary script file.
  • Implement the logic to generate a random password that meets the specified complexity requirements.
  • Test the password generator script to ensure it functions correctly.

๐Ÿ† Achievements

Upon completing this project, you will be able to:

  • Understand the process of creating a random password generator script.
  • Implement a password generation algorithm that satisfies specific character type requirements.
  • Test and validate the generated passwords to confirm they meet the desired criteria.

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/TextProcessingGroup(["`Text Processing`"]) linux(("`Linux`")) -.-> linux/VersionControlandTextEditorsGroup(["`Version Control and Text Editors`"]) linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/BasicSystemCommandsGroup -.-> linux/test("`Condition Testing`") linux/BasicFileOperationsGroup -.-> linux/touch("`File Creating/Updating`") linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") linux/TextProcessingGroup -.-> linux/tr("`Character Translating`") linux/VersionControlandTextEditorsGroup -.-> linux/vim("`Text Editing`") subgraph Lab Skills linux/echo -.-> lab-301485{{"`Random Password Generator Development`"}} linux/test -.-> lab-301485{{"`Random Password Generator Development`"}} linux/touch -.-> lab-301485{{"`Random Password Generator Development`"}} linux/cd -.-> lab-301485{{"`Random Password Generator Development`"}} linux/tr -.-> lab-301485{{"`Random Password Generator Development`"}} linux/vim -.-> lab-301485{{"`Random Password Generator Development`"}} end

Set Up the Project Environment

In this step, you'll prepare your working environment and create the necessary file for the password generator script.

  1. Open your terminal and navigate to the /home/labex/project directory using the cd command:

    cd /home/labex/project
  2. Create a new file named genpass.sh using the touch command. This will be the file where you write your password generator script:

    touch genpass.sh
  3. Open the genpass.sh file in a text editor. You can use nano, vim, or any text editor you prefer. Add the following shebang line at the very 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. The comments following the shebang line provide a brief description of the script.

Implement the Password Generation Logic

In this step, you'll add the core logic to the genpass.sh script to generate random passwords that meet the complexity requirements.

  1. Inside 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 works by:

    • Initializing the password with one special character, one digit, one uppercase letter, and one lowercase letter to meet the basic criteria.
    • Calculating the number of remaining characters to reach the desired length of 12.
    • Randomly selecting the remaining characters from a pool of digits, letters (both upper and lower case), and special characters.
    • Shuffling the password to ensure the specific characters are not always at the start.
  2. Add the following code to the end of the genpass.sh file. This part of the script calls the generate_password function and prints the generated password to the console:

    ## Generate password and output
    generate_password

Test the Password Generator

In this step, you'll test the genpass.sh script to ensure it generates passwords that meet all the requirements.

  1. Save the genpass.sh file in your text editor.

  2. In the terminal, make the script executable using the chmod command:

    chmod +x genpass.sh

    This command adds execute permissions to the file, allowing it to be run as a program.

  3. Run the script using the following command:

    sh genpass.sh

    The script should output a random 12-character password to the console. The output should contain at least one special character, one digit, one uppercase letter, and one lowercase letter.

    ## Example
    2Dsxw9+xS:27
  4. Run the script several more times. Each time, a different password should be generated. This verifies that the randomness in the password generator works correctly.

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

โœจ Check Solution and Practice

Summary

Congratulations! You have completed this project and created a random password generator script. You now have a foundation to build on for future scripting projects.
You can continue practicing with more labs on LabEx to further enhance your skills.

Other Linux Tutorials you may like