Skip to content

Commit

Permalink
refact: Add new gen password format
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuscomh committed Jan 29, 2025
1 parent 76b8eb5 commit 6b833c3
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 42 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# shellPass - Shell Script Password Generator

[![Release](https://img.shields.io/badge/release-3.2.3-brightgreen)](https://github.com/mateuscomh/shellPass/releases)
[![Release](https://img.shields.io/badge/release-3.3.1-brightgreen)](https://github.com/mateuscomh/shellPass/releases)
[![Build Status](https://github.com/mateuscomh/shellPass/actions/workflows/super-linter.yml/badge.svg)](https://github.com/mateuscomh/shellPass/actions/workflows/super-linter.yml)

## Overview
Expand Down
99 changes: 58 additions & 41 deletions shellPass.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,69 +14,86 @@ BOLD=$(tput bold)
ITALIC=$(tput dim)

main() {
local VERSION="Ver:3.2.3"
local AUTHOR="Matheus Martins-3mhenrique@gmail.com"
local USAGE="Generate random passwords from CLI
local VERSION="Ver:3.3.1"
local AUTHOR="Matheus Martins-3mhenrique@gmail.com"
local USAGE="Generate random passwords from CLI
███████╗██╗ ██╗███████╗██╗ ██╗ ██████╗ █████╗ ▄▄███▄▄·▄▄███▄▄·
██╔════╝██║ ██║██╔════╝██║ ██║ ██╔══██╗██╔══██╗██╔════╝██╔════╝
███████╗███████║█████╗ ██║ ██║ ██████╔╝███████║███████╗███████╗
╚════██║██╔══██║██╔══╝ ██║ ██║ ██╔═══╝ ██╔══██║╚════██║╚════██║
███████║██║ ██║███████╗███████╗███████╗██║ ██║ ██║███████║███████║
╚══════╝╚═╝ ╚═╝╚══════╝╚══════╝╚══════╝╚═╝ ╚═╝ ╚═╝╚═▀▀▀══╝╚═▀▀▀══╝ "
local MAX="$1"
local TIPO="$2"
echo -e "$USAGE"
case "$MAX" in
v | -v | --version ) echo -e "${BOLD} $VERSION / $AUTHOR ${FECHA}"; return;;
* ) echo "$VERSION" "$AUTHOR"; _checkSize; _checkType ;;
esac
local MAX="$1"
local TIPO="$2"
echo -e "$USAGE"
case "$MAX" in
v | -v | h | -h | --version)
echo -e "${ITALIC} $VERSION / $AUTHOR ${FECHA}"
return
;;
*)
echo "$VERSION"
_checkSize
_checkType
;;
esac

case "$TIPO" in
1)
CPX='0-9' ;;
2)
CPX='a-z0-9A-Z' ;;
3)
CPX='A-Za-z0-9!"#$%&'\''()*+,-./:;<=>?@[\]^_{|}~' ;;
q|Q) echo "Bye..."; exit ;;
esac
_makePass
_writeinfile
case "$TIPO" in
1)
CPX='0-9'
;;
2)
CPX='a-z0-9A-Z'
;;
3)
CPX='A-Za-z0-9!"#$%&'\''()*+,-./:;<=>?@[\]^_{|}~'
;;
q | Q)
echo "Bye..."
exit
;;
esac
_makePass
_writeinfile
}

_checkSize() {
while [[ -z "$MAX" || "$MAX" == *[^[:digit:]]* || "$MAX" -lt 1 ]]; do
echo -e "${BOLD} Enter the QUANTITY of characters for the password or [Q]uit: ${FECHA}"
read -r MAX
[[ $MAX == [qQ] ]] && echo "Bye..." && exit 0
done
while [[ -z "$MAX" || "$MAX" == *[^[:digit:]]* || "$MAX" -lt 1 ]]; do
echo -e "${BOLD} Enter the QUANTITY of characters for the password or [Q]uit: ${FECHA}"
read -r MAX
[[ $MAX == [qQ] ]] && echo "Bye..." && exit 0
done
}

_checkType() {
while [[ "$TIPO" != [1-3] && "$TIPO" != [qQ] ]]; do
echo -e "${BOLD} Enter the TYPE [1,2,3] for password complexity you want or [Q]uit ${FECHA}
while [[ "$TIPO" != [1-3] && "$TIPO" != [qQ] ]]; do
echo -e "${BOLD} Enter the TYPE [1,2,3] for password complexity you want or [Q]uit ${FECHA}
${ITALIC} 1 - Password only numbers ${FECHA}
${ITALIC} 2 - Password with LeTtErS and numb3rs ${FECHA}
${ITALIC} 3 - Password with LeTtErS, numb3rs and Sp3c1@l Ch@r@ct&rs ${FECHA}"
read -rsn 1 TIPO;
done
read -rsn 1 TIPO
done
}

_writeinfile() {
SCRIPT_PATH="${BASH_SOURCE:-$0}"
ABS_SCRIPT_PATH="$(readlink -f "${SCRIPT_PATH}")"
ABS_DIRECTORY="$(dirname "${ABS_SCRIPT_PATH}")"
echo "$(date '+%d/%m/%y %H:%M:%S') - $PASS" >> "$ABS_DIRECTORY"/history.log
SCRIPT_PATH="${BASH_SOURCE:-$0}"
ABS_SCRIPT_PATH="$(readlink -f "${SCRIPT_PATH}")"
ABS_DIRECTORY="$(dirname "${ABS_SCRIPT_PATH}")"
echo "$(date '+%d/%m/%y %H:%M:%S') - $PASS" >>"$ABS_DIRECTORY"/history.log
}

_makePass() {
PASS=$(cat /dev/urandom LC_ALL=C | tr -dc "$CPX"| head -c "$MAX")
case $(uname -s) in
Darwin) printf %s "$PASS" | pbcopy 2> /dev/null ;;
Linux) command -v xclip > /dev/null && { printf %s "$PASS" | xclip -sel copy; } ;;
*) echo "This is compatible only for GNU/Linux, MacOS or WSL2"; exit 1 ;;
esac
echo -e "${BOLD}$PASS${FECHA}"
# PASS=$(cat /dev/urandom LC_ALL=C | tr -dc "$CPX" | head -c "$MAX")
PASS=$(tr -dc "$CPX" </dev/urandom | head -c "$MAX")
case $(uname -s) in
Darwin) printf %s "$PASS" | pbcopy 2>/dev/null ;;
Linux) command -v xclip >/dev/null && { printf %s "$PASS" | xclip -sel copy; } ;;
*)
echo "This is compatible only for GNU/Linux, MacOS or WSL2"
exit 1
;;
esac
echo -e "${BOLD}$PASS${FECHA}"
}
#---Main
main "$1" "$2"

0 comments on commit 6b833c3

Please sign in to comment.