Skip to content

Instantly share code, notes, and snippets.

@cdeath
Last active March 10, 2024 13:57
Show Gist options
  • Save cdeath/f02f330ad4122413f381b1dd7ebfa804 to your computer and use it in GitHub Desktop.
Save cdeath/f02f330ad4122413f381b1dd7ebfa804 to your computer and use it in GitHub Desktop.
password generator

Password (Re)Generator

TL;DR; A keyed-hash string encoding function.
Useful for setting strong passwords that you will not remember but can regenerate.

Use any hash function you like.

HMAC with BASE64 (no special chars)

echo -n "username" | openssl sha256 -hmac "password" | cut -d " " -f 2 | base64 | cut -c 1-64

HMAC with BASE64 and ROT47

echo -n "username" | openssl sha256 -hmac "password" | cut -d " " -f 2 | base64 | tr "\!-~" "P-~\!-O" | cut -c 1-64

HMAC with BASE91

brew install base91
echo -n "username" | openssl sha256 -hmac "password" | cut -d " " -f 2 | base91 | tr -d "\n" | cut -c 1-64

bash function with args (username, password, length), directly to clipboard (on macOS)

pw() {
  echo -n "$1" | openssl sha256 -hmac "$2" | cut -d " " -f 2 | base91 | tr -d "\n" | cut -c 1-${3:-64} | pbcopy
}

interactive shell script with optional flags

#!/bin/bash

while getopts d:e:l: flag
do
    case "${flag}" in
        d) dgst=${OPTARG};;
        e) enc=${OPTARG};;
        l) length=${OPTARG};;
    esac
done

read -r -p "username: " username
read -s -p "password: " password

echo
echo -n "$username" | openssl ${dgst:-sha256} -hmac "$password" | cut -d " " -f 2 | ${enc:-base91} | tr -d "\n" | cut -c 1-${length:-64} | tr -d "\n" | pbcopy

examples with foo and bar:

pw
ztMfs+1Xs!o@tNLTS8tE%kOu}Rm/26;TuREFnl&G81vxnCGTG)61Ak?HAJQz#6`j

pw -l 32
ztMfs+1Xs!o@tNLTS8tE%kOu}Rm/26;T

pw -e base64
MjBiOWQxZGMxOTNhNmY1MzQ0NDhmMjQ1ZTg5Zjk4NTIxMTVjNzg2MjhiMjNmMTNi

pw -d sha1 -e cat
cd53d1c3ab32e5665ecc224c3d61581c0fd847ea
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment