Laravel, you usually need to set a session/secret/encryption key.
I know why this is, but I always end up looking around for some random password generator so I can get a random string that is exactly 32 characters. Isn't there an easier way?!?!
Yes there is. If you have the magical OpenSSL installed, which most do, you can use it to generate a random string.
I found a article online that uses base64 to generate a string of a certain length. The only thing is that base64 is padded with 8 bits. Which means that if you want 32 then you need to use 24. This goes up exponentially as the number gets bigger. So there is a trim part of the function that clips off the extra characters.
Here is the function broken down into steps:
I put a check in there if the argument is not a number. This is just for the dummies out there.
You are probably on Linux. I found this little snippet for the lazy. This way you can forget about translating it each time.
alias pbcopy="xsel --clipboard --input"
alias pbpaste="xsel --clipboard --output"
Now for the actual shell function:
# if the argument is a number
# cut the string so that there is no base64 padding
# generate a random password of the specified length
# then copy it to the clipboard without a newline
# usage: password 32
password() {
LENGTH="$1"
REGEX='^[0-9]+$'
if [[ $LENGTH =~ $REGEX ]] ; then
PASSWD=$(openssl rand -base64 $LENGTH | head -c$LENGTH)
echo $PASSWD
echo $PASSWD | tr -d '\n' | pbcopy
echo "Password copied to clipboard"
else
echo "Argument must be a number"
fi
}
Here is how you would use it, and what the results would look like:
~ ❯ password 32
gY4zLES+WWF5+iNWo0FYx+os6EmDwecf
Password copied to clipboard
~ ❯ password fu
Argument must be a number
~ ❯
This function would be put in your .bashrc
file, or you .zshrc
file if you are a cool ZSH user.
I'm a full-stack developer, co-organizer of PHP Vancouver meetup, and winner of a Canadian Developer 30 under 30 award. I'm a huge Open Source advocate and contributor to a lot of projects in my community. When I am not sitting at a computer, I'm trying to perfect some other skill.