Last active
October 11, 2015 20:36
-
-
Save metachris/2440d94dfdbe716503b2 to your computer and use it in GitHub Desktop.
bash hash generator function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Simple Bash Hash Generator | |
# | |
# Author: Chris Hager <[email protected]> | |
# | |
# Put the `hashgen` method in your `.functions` file and source it from your `.bash_profile` | |
# | |
# Usage: hashgen [type] [#chars] | |
# | |
# Optional argument `type`: | |
# | |
# n .... numbers | |
# x .... a-f | |
# xn ... a-f, 0-9 | |
# xX ... a-f, A-F | |
# xXn .. a-f, A-F, 0-9 | |
# a .... a-z | |
# an ... a-z, 0-9 | |
# aA ... a-z, A-Z | |
# aAn .. a-z, A-Z, 0-9 | |
# | |
# Examples: | |
# | |
# $ hashgen | |
# ccee24b6 | |
# | |
# $ hashgen 20 | |
# c1df84a25f42af24c81c | |
# | |
# $ hashgen aAn 20 | |
# VcmTRAiv4LVDiraxSCna | |
# | |
function hashgen() { | |
CHARS=8 | |
TYPES=(n x xn xX xXn a an aA aAn) | |
TYPE=xn # default: lower case a-f + 0-9 | |
RE_NUM='^[0-9]+$' | |
for ARG in "$@" | |
do | |
if [[ $ARG =~ $RE_NUM ]]; then | |
CHARS=$ARG | |
else | |
case "${TYPES[@]}" in *"$ARG"*) TYPE=$ARG ;; esac | |
fi | |
done | |
#echo "type: $TYPE, chars: $CHARS" | |
case $TYPE in | |
"aAn"*) cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w $CHARS | head -n 1 ;; | |
"aA"*) cat /dev/urandom | tr -dc 'a-zA-Z' | fold -w $CHARS | head -n 1 ;; | |
"an"*) cat /dev/urandom | tr -dc 'a-z0-9' | fold -w $CHARS | head -n 1 ;; | |
"a"*) cat /dev/urandom | tr -dc 'a-z' | fold -w $CHARS | head -n 1 ;; | |
"xXn"*) cat /dev/urandom | tr -dc 'a-fA-F0-9' | fold -w $CHARS | head -n 1 ;; | |
"xX"*) cat /dev/urandom | tr -dc 'a-fA-F' | fold -w $CHARS | head -n 1 ;; | |
"xn"*) cat /dev/urandom | tr -dc 'a-f0-9' | fold -w $CHARS | head -n 1 ;; | |
"x"*) cat /dev/urandom | tr -dc 'a-f' | fold -w $CHARS | head -n 1 ;; | |
"n"*) cat /dev/urandom | tr -dc '0-9' | fold -w $CHARS | head -n 1 ;; | |
esac | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment