commit 18021f665e57d21ca8042c2600c62cf85481d200
parent 248a64472233980b810d9ce76c89459495958459
Author: Natasha Kerensikova <natgh@instinctive.eu>
Date:   Sat, 14 Sep 2024 09:18:50 +0000
Random character generation is refactored to reach full coverage of pashage.sh
Diffstat:
1 file changed, 9 insertions(+), 3 deletions(-)
diff --git a/src/pashage.sh b/src/pashage.sh
@@ -69,6 +69,14 @@ glob_exists() {
 	fi
 }
 
+# Generate random characters
+#   $1: number of characters
+#   $2: allowed character set
+random_chars() {
+	LC_ALL=C tr -dc -- "$2" </dev/urandom | dd ibs=1 obs=1 count="$1" \
+	    2>/dev/null || true
+}
+
 # Find the deepest recipient file above the given path
 set_LOCAL_RECIPIENT_FILE() {
 	LOCAL_RECIPIENT_FILE="/$1"
@@ -137,7 +145,6 @@ yesno() {
 }
 
 
-
 ##################
 # SCM MANAGEMENT #
 ##################
@@ -617,8 +624,7 @@ do_encrypt() {
 #   DECISION: whether to ask before overwrite
 #   OVERWRITE: whether to re-use existing secret data
 do_generate() {
-	NEW_PASS="$(LC_ALL=C tr -dc -- "$3" </dev/urandom \
-	    | LC_ALL=C dd ibs=1 obs=1 count="$2" 2>/dev/null || true)"
+	NEW_PASS="$(random_chars "$2" "$3")"
 	NEW_PASS_LEN="$(strlen "${NEW_PASS}")"
 
 	if [ "${NEW_PASS_LEN}" -ne "$2" ]; then