From 6b833c3f88ad0ac4229eca9fc4fb0fcb7a17dfed Mon Sep 17 00:00:00 2001 From: mateuscomh Date: Wed, 29 Jan 2025 10:09:57 -0300 Subject: [PATCH] refact: Add new gen password format --- README.md | 2 +- shellPass.sh | 99 ++++++++++++++++++++++++++++++---------------------- 2 files changed, 59 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index 0ac167a..60ee830 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/shellPass.sh b/shellPass.sh index 5b79ead..78f57a0 100755 --- a/shellPass.sh +++ b/shellPass.sh @@ -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/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"