Last active 1 year ago

needs nerdfont & secret service entry with account = $account_value

Revision 439d60bdec34585a0075c65bb886c72a4c9dea0e

example.md Raw

Example

learn_pass.sh Raw
1#!/bin/bash
2# Secret service entry needs the account key set to:
3account_value=learning_password
4
5
6# Function to check the input against the stored secret
7check_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
41while 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
57done