Generate a long Access Key

Generate a long Access Key
Photo by rc.xyz NFT gallery / Unsplash

I am installing a web app and in the configuration file it says the "secret key" should be a very long sequence of random characters. This won't be a string that I need to remember. It got me thinking of a way to generate such in a script. One such option is pwgen -s 64 1 which will give a random string 64 characters in length, which is probably good enough for what I'm doing. For example: the output of such would look like:

xKlo35yAuIPORDz8RLC9yFg1s8Nh2dMXI2xEhfuTrzzwXNRzexNfbicIn9ZR6N4Y

Then I got to thinking of those Recovery Keys that sites sometime provide to users, those are usually easier to read aloud should it be necessary. After a few minutes I came up with the following one liner:

PASS=`pwgen -cn -B  -C  4 22  | tr a-z A-Z |sed -e 's/ /-/g'`

echo $PASS
X3L7-9APA-PJM9-R4BB-97RC-E4VM-HN3W-CER3-3ACT-FQN3-KP3O-TH9D-7BUZ-FRT3-FRT3-7DAK Y7AH-7KKM-VXU9-RJS4-PMK3-XH3W

Nicely produces a very long random password/access key, what ever you want to call it.