learn_pass.sh
· 1.4 KiB · Bash
Raw
#!/bin/bash
# Secret service entry needs the account key set to:
account_value=learning_password
# Function to check the input against the stored secret
check_secret() {
local input="$1"
local secret_service_account="$2"
local secret
secret=$(secret-tool lookup account "$secret_service_account")
if [[ $? -ne 0 || -z "$secret" ]]; then
echo -e "\033[31mError: Could not retrieve secret.\033[0m"
return 1
fi
local result=""
for ((i = 0; i < ${#input}; i++)); do
if [[ "${input:$i:1}" == "${secret:$i:1}" ]]; then
result+="\033[32m\033[0m" # Green for correct
else
result+="\033[31m\033[0m" # Red for incorrect
fi
done
local len_string=""
if [[ ${#input} -gt ${#secret} ]]; then
len_string="\033[33m+$((${#input} - ${#secret}))\033[0m"
elif [[ ${#input} -lt ${#secret} ]]; then
len_string="\033[33m-$((${#secret} - ${#input}))\033[0m"
else
len_string="\033[32m==\033[0m"
fi
echo -e "Result ($len_string): $result"
return 0
}
while true; do
echo -n "Enter secret (hidden input, Ctrl+C to exit): "
input=""
while IFS= read -rsn1 char; do
if [[ "$char" == "" ]]; then # Enter key
echo
break
fi
echo -n ""
input+="$char"
done
check_secret "$input" "$account_value"
echo
done
| 1 | #!/bin/bash |
| 2 | # Secret service entry needs the account key set to: |
| 3 | account_value=learning_password |
| 4 | |
| 5 | |
| 6 | # Function to check the input against the stored secret |
| 7 | check_secret() { |
| 8 | local input="$1" |
| 9 | local secret_service_account="$2" |
| 10 | |
| 11 | local secret |
| 12 | secret=$(secret-tool lookup account "$secret_service_account") |
| 13 | |
| 14 | if [[ $? -ne 0 || -z "$secret" ]]; then |
| 15 | echo -e "\033[31mError: Could not retrieve secret.\033[0m" |
| 16 | return 1 |
| 17 | fi |
| 18 | |
| 19 | local result="" |
| 20 | for ((i = 0; i < ${#input}; i++)); do |
| 21 | if [[ "${input:$i:1}" == "${secret:$i:1}" ]]; then |
| 22 | result+="\033[32m\033[0m" # Green for correct |
| 23 | else |
| 24 | result+="\033[31m\033[0m" # Red for incorrect |
| 25 | fi |
| 26 | done |
| 27 | |
| 28 | local len_string="" |
| 29 | if [[ ${#input} -gt ${#secret} ]]; then |
| 30 | len_string="\033[33m+$((${#input} - ${#secret}))\033[0m" |
| 31 | elif [[ ${#input} -lt ${#secret} ]]; then |
| 32 | len_string="\033[33m-$((${#secret} - ${#input}))\033[0m" |
| 33 | else |
| 34 | len_string="\033[32m==\033[0m" |
| 35 | fi |
| 36 | |
| 37 | echo -e "Result ($len_string): $result" |
| 38 | return 0 |
| 39 | } |
| 40 | |
| 41 | while true; do |
| 42 | echo -n "Enter secret (hidden input, Ctrl+C to exit): " |
| 43 | input="" |
| 44 | |
| 45 | while IFS= read -rsn1 char; do |
| 46 | if [[ "$char" == "" ]]; then # Enter key |
| 47 | echo |
| 48 | break |
| 49 | fi |
| 50 | echo -n "" |
| 51 | input+="$char" |
| 52 | done |
| 53 | |
| 54 | check_secret "$input" "$account_value" |
| 55 | |
| 56 | echo |
| 57 | done |