This is the first Bash script I've written, so I'm looking for feedback on best practices, conventions, things like that.
This script makes a few assumptions
- There is a java keystore stored at
~/.keystore
- There is an alias for an entry in that keystore with a value of
test
- Both the keystore and entry share the same initial password
- That shared password is
test
After that, for both the keystore and key entry, it pulls a number of bytes from /dev/urandom
, Base64 encodes them, and sets that as the password.
#!/bin/bash
keystore_file=~/.keystore
config_file=~/.keystore.config
alias_name=test
initial_password=test
generate_password() {
local password_length=$1
local password="$(dd if=/dev/urandom bs=$password_length count=1 | base64 -w 0)"
echo $password
}
set_keystore_password() {
local password_length=80
local password=$(generate_password $password_length)
keytool -storepasswd -keystore $keystore_file -storepass $initial_password -new $password
echo $password >> $config_file
echo $password
}
set_key_password() {
local keystore_password=$1
local password_length=80
local password=$(generate_password $password_length)
keytool -keypasswd -keystore $keystore_file -storepass $keystore_password -alias $alias_name -keypass $initial_password -new $password
echo $password >> $config_file
}
initialize_keystore() {
if [ -f $config_file ]
then
rm $config_file
touch $config_file
fi
local keystore_password=$(set_keystore_password)
set_key_password $keystore_password
}
initialize_keystore
---
(horizontal rule) \$\endgroup\$