Skip to content

A small Shell script (command-line) to generate a random password

Notifications You must be signed in to change notification settings

Reda96R/Password-Generator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 

Repository files navigation


Logo

Password-Generator

Table of Contents
  1. About The Project
  2. Script summary
  3. Generating a random password
  4. Printing the password
  5. The script
  6. Note
  7. Contributing
  8. Contact
  9. Acknowledgments

About The Project

We all find ourselves in situations where you need a new random password that you can use for any software installation or when you sign-up to any website. There are a lot of options out there in order to achieve this. You can use a password manager/vault where you often have the option to randomly generate a password or to use a website that can generate the password on your behalf. I made a small Shell script (command-line) to generate a password that I can quickly use.

Built With

Script summary

First, let's do a quick summary of what the script is going to do:

  1. The script will give the user the chance to choose the password length when it's executed.
  2. The script will then generate a random password with the length that was specified in step 1.

Generating a random password

We can use several commands in order to generate a random string of characters. for this operation I'm going for the pseudorandom number generator /dev/urandom which is an algorithm that uses mathematical formulas to produce sequences of random numbers. PRNGs generate a sequence of numbers approximating the properties of random numbers.

How PRNG works?

Linear Congruential Generator is the go for algorithm for generating pseudo-randomized numbers. The generator is defined by the recurrence relation:
Xn+1 = (aXn + c) mod m
where X is the sequence of pseudo-random values
m, 0 < m  - modulus 
a, 0 < a < m  - multiplier
c, 0 ≤ c < m  - increment
x0, 0 ≤ x0 < m  - the seed or start value

To generate the next random integer we use the previous random integer, the integer constants, and the integer modulus. and to get started, the algorithm requires an initial Seed(value), which must be provided by some means. The appearance of randomness is provided by performing modulo arithmetic.

Printing the password

To print the generated password, I used Strings, with this command I can take the output from /dev/urandom, and Srtings extracts strings of printable characters so that other commands can use the strings without having to contend with non-printable characters.

next I used grep, it searches the given file(in our case the output from Strings) for lines containing a match to the given strings or words, -o(--only-matching) which helps us display only matched parts of lines, in our case [:alnum:] which is any of [:digit:] or [:alpha:] (in other words means [0-9A-Za-z], except the latter form is dependent upon the ASCII character encoding).

Then we'll use head, this command is generally used to display the top lines in a text file. We use -n (--lines=[-]NUM) that prints the first NUM lines (specified by the user) instead of the first 10; with the leading '-', print all but the last NUM lines of the file.

Last I used tr, which can be used to translate, squeeze, and delete characters from standard input, writing to standard output in Linux and Unix, in our case, we needed it so we can delete new lines (-d '\n'), and to have the generated password in one line so the user can easily copy it.

The script

First we begin the script with the shebang #!/bin/bash , we use it to tell the operating system which interpreter to use to parse the rest of the file. next we ask the user for some input. In our case we would like to know how many characters the password needs to be

#!/bin/bash
clear
echo "How many characters you would like the password to have?"
read i

at last, generate the passwords and then print it so the user can use it.

#!/bin/bash
clear
echo "How many characters you would like the password to have?"
read i
echo "Password is:"
strings /dev/urandom | grep -o '[[:alnum:]]' | head -n $i | tr -d '\n'

Note

While the script is working fine, it expects that the user will provide the requested input. In order to prevent any issues I will make sure to do some more advance checks on the user input in the future commits in order to make sure the script will continue to work fine even if the provided input does not match our needs.

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

Contact

Reda Rayyad - Twitter - Instagram - [email protected]

Project Link: https://github.com/Reda96R/Password-Generator

Acknowledgments

Here you can find some of the resources that I found helpful while making this script

(back to top)

About

A small Shell script (command-line) to generate a random password

Resources

Stars

Watchers

Forks

Releases

No releases published

Languages