Browse Source

reworked whole script

panda 2 years ago
parent
commit
00c3e01483
7 changed files with 126 additions and 101 deletions
  1. 5 0
      emails.txt
  2. 0 21
      gpg-quorum3.sh
  3. 0 49
      gpg-quorum5.sh
  4. 121 0
      gpg-quorum_3of5.sh
  5. 0 9
      test.sh
  6. 0 10
      variables3.sh
  7. 0 12
      variables5.sh

+ 5 - 0
emails.txt

@@ -0,0 +1,5 @@
+A
+B
+C
+D
+E

+ 0 - 21
gpg-quorum3.sh

@@ -1,21 +0,0 @@
-#!/usr/bin/env bash
-
-source variables.sh
-
-file=$1
-
-#A,B
-gpg -ea -r $a -r $b $debug --output $file.step1 $file
-
-#A,C
-gpg -ea -r $a -r $c $debug --output $file.step2 $file.step1
-
-#B,C
-gpg -ea -r $b -r $c $debug --output $file.asc $file.step2
-
-rm test.txt.step*
-
-#matrix:
-#A,B
-#A,C
-#B,C

+ 0 - 49
gpg-quorum5.sh

@@ -1,49 +0,0 @@
-#!/usr/bin/env bash
-
-source variables.sh
-
-file=$1
-
-#A,B,C
-gpg -ea -r $a -r $b -r $c $debug --output $file.step1 $file
-
-#A,B,D
-gpg -ea -r $a -r $b -r $d $debug --output $file.step2 $file.step1
-
-#A,B,E
-gpg -ea -r $a -r $b -r $e $debug --output $file.step3 $file.step2
-
-#A,C,D
-gpg -ea -r $a -r $c -r $d $debug --output $file.step4 $file.step3
-
-#A,C,E
-gpg -ea -r $a -r $c -r $e $debug --output $file.step5 $file.step4
-
-#A,D,E
-gpg -ea -r $a -r $d -r $e $debug --output $file.step6 $file.step5
-
-#B,C,D
-gpg -ea -r $b -r $c -r $d $debug --output $file.step7 $file.step6
-
-#B,C,E
-gpg -ea -r $b -r $c -r $e $debug --output $file.step8 $file.step7
-
-#B,D,E
-gpg -ea -r $b -r $d -r $e $debug --output $file.step9 $file.step8
-
-#C,D,E
-gpg -ea -r $c -r $d -r $e $debug --output $file.asc $file.step9
-
-rm test.txt.step*
-
-#matrix:
-#A,B,C
-#A,B,D
-#A,B,E
-#A,C,D
-#A,C,E
-#A,D,E
-#B,C,D
-#B,C,E
-#B,D,E
-#C,D,E

+ 121 - 0
gpg-quorum_3of5.sh

@@ -0,0 +1,121 @@
+#!/usr/bin/env bash
+
+#Scipt to encrypt a file with some known keys,
+#the script will combin the encryptions to ensure that a minimum quorum defined in "combinate" variable is needed to open the file.
+#the script will not auto-adjust to an arbitrary change of "combinate" since one or more loops in the main loop should be tuned,
+#right now it's adjusted ofr a quorum of 3 recipients.
+#
+#usage: gpg-quorum_3of5.sh filename_to_encrypt.ext
+
+#retrieve the email list
+readarray -t emails < emails.txt
+file=$1
+#debug email if you want to self-decrypt:
+debug="-r youermail@provider.net"
+#counters:
+counter=0
+firstrun=0
+#minimum numbers of recipients that needs to agree to decypher the file:
+combinate=3
+#work directory:
+workdir="_WORKDIR"
+#lenght of array:
+max=${#emails[@]}                                  # Take the length of that array
+
+
+#-----
+#CALCULATE number of combinations for our emails
+
+#function for factorial calculation
+function fun_fact {
+  num=$1
+  fact=1
+  for((i=2;i<=num;i++))
+  {
+    fact=$((fact * i))  #fact = fact * i
+  }
+  echo $fact
+}
+
+#n is the lenght of the array
+n=$max
+#r is the number of recipients needed to open the file
+r=$combinate
+
+a=$( fun_fact $n )
+b=$( fun_fact $r )
+c=$(( $n - $r ))
+d=$( fun_fact $c )
+t=$(( $b * $d ))
+ans=$(( $a / $t ))
+
+#echo "max number of combinations: "$ans
+#-----
+
+
+#create our workdir if it does not exist:
+if [ ! -d "$workdir" ]
+then
+    echo "creating dir"
+    mkdir $workdir
+else
+    echo "Directory $workdir exists, exiting"
+    exit
+fi
+
+
+#main loop:
+for ((idxA=0; idxA<max; idxA++)); do              # iterate idxA from 0 to length
+  for ((idxB=idxA; idxB<max; idxB++)); do         # iterate idxB from idxA to length
+    for ((idxC=idxB; idxC<max; idxC++)); do         # iterate idxC from idxB to length
+
+      if [ "${emails[$idxA]}" == "${emails[$idxB]}" ] || [ "${emails[$idxB]}" == "${emails[$idxC]}" ] || [ "${emails[$idxA]}" == "${emails[$idxC]}" ]; then
+
+        echo "A, B or C are the same"
+        continue
+
+      else
+        #custom execution for first run:
+        if [ "$counter" == 0 ]; then
+          gpg -ea -r "${emails[$idxA]}" -r "${emails[$idxB]}" -r "${emails[$idxC]}" "$debug" --output $workdir/$file.step$counter $file
+
+        #custom execution for last run:
+        elif [ "$counter" == $((ans-1)) ]; then
+          gpg -ea -r "${emails[$idxA]}" -r "${emails[$idxB]}" -r "${emails[$idxC]}" "$debug" --output $file.ENCRYPTED $workdir/$file.step$((counter-1))
+
+        #normal execution:
+        else
+          gpg -ea -r "${emails[$idxA]}" -r "${emails[$idxB]}" -r "${emails[$idxC]}" "$debug" --output $workdir/$file.step$counter $workdir/$file.step$((counter-1))
+
+        fi
+          #increase loop counter:
+          ((counter++))
+
+      fi
+    done
+  done
+done
+
+#remove work directory:
+rm -rf $workdir
+
+
+#the combination matrix for a quorum of 3 recipients on 5:
+#recipients: A,B,C,D,E
+#
+#A,B,C
+#A,B,D
+#A,B,E
+#A,C,D
+#A,C,E
+#A,D,E
+#B,C,D
+#B,C,E
+#B,D,E
+#C,D,E
+
+
+#SOURCES:
+#http://www.anonhack.in/2018/05/program-to-calculate-combination-ncr-in-bash-shell-scripting/
+#https://www.log2base2.com/shell-script-examples/loop/shell-script-to-find-factorial-of-a-number.html
+

+ 0 - 9
test.sh

@@ -1,9 +0,0 @@
-#!/usr/bin/env bash
-
-set -- A B "C"
-for a; do
-    shift
-    for b; do
-        printf "%s - %s\n" "$a" "$b"
-    done
-done

+ 0 - 10
variables3.sh

@@ -1,10 +0,0 @@
-#!/usr/bin/env bash
-
-#add here the keys/emails of the recipients:
-a="email@domain.net"
-b=""
-c=""
-#
-#the debug is a little different, use it with "-r" or leave the variable empty:
-debug="-r email@domain.net"
-

+ 0 - 12
variables5.sh

@@ -1,12 +0,0 @@
-#!/usr/bin/env bash
-
-#add here the keys/emails of the recipients:
-a="email@domain.net"
-b=""
-c=""
-d=""
-e=""
-#
-#the debug is a little different, use it with "-r" or leave the variable empty:
-debug="-r email@domain.net"
-