Introduction
Let’s face it—coming up with secure passwords can be a real headache. In a world where our lives are increasingly online, the need for strong, unique passwords is more important than ever. So, to make life a little easier (and to level up my Bash skills), I created a Bash script that generates and securely stores passwords using openssl
and ccrypt
. Let’s see how it works.
Why Bash?
Bash scripting is like having a Swiss army knife for your terminal. It’s powerful, versatile, and once you get the hang of it, you’ll wonder how you ever lived without it. In this little project, we’ll use Bash to create a script that does the heavy lifting for us—generating random passwords and then stashing them away in a secure, encrypted file. No more trying to remember if you used your girlfriend's birthday (if you have one ) or your cat's.
The Script
Here’s the magic in action:
#!/bin/bash
generate_password() {
openssl rand -base64 32 | cut -c1-$1
}
save_password() {
echo "$1" | ccrypt -e -k "$2" > "password_$3.txt.cpt"
}
echo "Enter the length of the passwords:"
read length
password1=$(generate_password $length)
password2=$(generate_password $length)
password3=$(generate_password $length)
echo "Generated Passwords:"
echo "1: $password1"
echo "2: $password2"
echo "3: $password3"
echo "Do you want to save these passwords with encryption? (y/n)"
read save_choice
if [ "$save_choice" == "y" ]; then
echo "Enter the encryption key:"
read -s encryption_key
save_password "$password1" "$encryption_key" "1"
save_password "$password2" "$encryption_key" "2"
save_password "$password3" "$encryption_key" "3"
echo "Passwords have been encrypted and saved securely."
else
echo "Passwords not saved."
fi
How the Script Works
Step 1: Prompt for Password Length: First things first, the script asks how long you want your passwords to be. Short and sweet or long and strong—it's your call!
Step 2: Generate Random Passwords: Using
openssl
, the script whips up three random passwords faster than you can say “password123.” Thecut -c1-$1
part just trims them down to the size you specified.Step 3: Display Generated Passwords: Voila! Your shiny new passwords appear on your screen. No need to remember them all at once—we’ll take care of storing them securely next.
Step 4: Option to Save with Encryption: If you’re feeling like a secret agent, you can opt to save these passwords with encryption. Just enter your super-secret key, and the script will tuck your passwords away safely using
ccrypt
.
Step-by-Step Guide
Install Required Tools
Before you run this script, make sure you have openssl
and ccrypt
installed. If not, no worries—just pop these commands into your terminal:
bashCopy codesudo apt-get install openssl
sudo apt-get install ccrypt
Running the Script
Save the Script: Save the script to a file, like
password_
generator.sh
.Make it Executable: Give your script some power with:
bashCopy codechmod +x password_generator.sh
Run the Script: Time to see it in action:
bashCopy code./password_generator.sh
Follow the Prompts
Enter how long you want your passwords.
Check out the awesome passwords the script generates.
Decide if you want to save them with encryption.
Accessing Saved Passwords
If you chose to save them, your passwords will be tucked away in encrypted files named password_1.txt.cpt
, password_2.txt.cpt
, and password_3.txt.cpt
. To unlock them later, just use:
bashCopy codeccrypt -d password_1.txt.cpt
Conclusion
This little Bash script might be simple, but it’s a powerful tool in your scripting arsenal. Not only does it save you from the hassle of coming up with new passwords, but it also ensures they’re stored securely. Plus, it’s a great way to get some practice with Bash scripting—because who doesn’t love learning by doing (and having a little fun along the way)?