Skip to content

Instantly share code, notes, and snippets.

Created June 17, 2014 05:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/e168a8b46b9a8a5c7d24 to your computer and use it in GitHub Desktop.
Save anonymous/e168a8b46b9a8a5c7d24 to your computer and use it in GitHub Desktop.
password and hash generation with apg, htpasswd/bcrypt
#!/bin/bash
# password and hash pair generation with apg, htpasswd/bcrypt
# usage:
# source this file or include the passnhash function in your .bash_profile/.bashrc
# then: `passnhash 0:35:500:14`
# prerequisites:
# 1) recent version of htpasswd that supports bcrypt (-B flag)
# deb: `apt-get install apache2-utils`
# mac: `brew install brew install homebrew/apache/httpd24`
# 2) apg
# deb: `apt-get install apg`
# mac: `brew install apg`
# be thoughtful if/when outputting to the terminal instead of a file
function passnhash () {
local ALG=$(echo "$@" | awk -F: '{ print $1 }')
local LEN=$(echo "$@" | awk -F: '{ print $2 }')
local RND=$(echo "$@" | awk -F: '{ print $3 }')
local CST=$(echo "$@" | awk -F: '{ print $4 }')
local PAS=$(apg -a "$ALG" -m "$LEN" -n "$RND" | tail -n 1)
# "username" is a placeholder value since we're only interested in the hash
local HAS=$(echo "$PAS" | htpasswd -n -i -B -C "$CST" username | tr -d '\n' | awk -F: '{ print $2 }')
printf "password: %s\nhash: %s\n" "$PAS" "$HAS"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment