gpg-quorum_3of5.sh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/usr/bin/env bash
  2. #Scipt to encrypt a file with some known keys,
  3. #the script will combin the encryptions to ensure that a minimum quorum defined in "combinate" variable is needed to open the file.
  4. #the script will not auto-adjust to an arbitrary change of "combinate" since one or more loops in the main loop should be tuned,
  5. #right now it's adjusted ofr a quorum of 3 recipients.
  6. #
  7. #usage: gpg-quorum_3of5.sh filename_to_encrypt.ext
  8. #retrieve the email list
  9. readarray -t emails < emails.txt
  10. file=$1
  11. #debug email if you want to self-decrypt, you can also leave it blank but don't populate it without "-r":
  12. debug="-r youermail@provider.net"
  13. #counters:
  14. counter=0
  15. firstrun=0
  16. #minimum numbers of recipients that needs to agree to decypher the file:
  17. combinate=3
  18. #work directory:
  19. workdir="_WORKDIR"
  20. #lenght of array:
  21. max=${#emails[@]} # Take the length of that array
  22. #-----
  23. #CALCULATE number of combinations for our emails
  24. #function for factorial calculation
  25. function fun_fact {
  26. num=$1
  27. fact=1
  28. for((i=2;i<=num;i++))
  29. {
  30. fact=$((fact * i)) #fact = fact * i
  31. }
  32. echo $fact
  33. }
  34. #n is the lenght of the array
  35. n=$max
  36. #r is the number of recipients needed to open the file
  37. r=$combinate
  38. a=$( fun_fact $n )
  39. b=$( fun_fact $r )
  40. c=$(( $n - $r ))
  41. d=$( fun_fact $c )
  42. t=$(( $b * $d ))
  43. ans=$(( $a / $t ))
  44. #echo "max number of combinations: "$ans
  45. #-----
  46. #create our workdir if it does not exist:
  47. if [ ! -d "$workdir" ]
  48. then
  49. echo "creating dir"
  50. mkdir $workdir
  51. else
  52. echo "Directory $workdir exists, exiting"
  53. exit
  54. fi
  55. #main loop:
  56. for ((idxA=0; idxA<max; idxA++)); do # iterate idxA from 0 to length
  57. for ((idxB=idxA; idxB<max; idxB++)); do # iterate idxB from idxA to length
  58. for ((idxC=idxB; idxC<max; idxC++)); do # iterate idxC from idxB to length
  59. if [ "${emails[$idxA]}" == "${emails[$idxB]}" ] || [ "${emails[$idxB]}" == "${emails[$idxC]}" ] || [ "${emails[$idxA]}" == "${emails[$idxC]}" ]; then
  60. #echo "A, B or C are the same"
  61. continue
  62. else
  63. #custom execution for first run:
  64. if [ "$counter" == 0 ]; then
  65. gpg -ea -r "${emails[$idxA]}" -r "${emails[$idxB]}" -r "${emails[$idxC]}" "$debug" --output $workdir/$file.step$counter $file
  66. #custom execution for last run:
  67. elif [ "$counter" == $((ans-1)) ]; then
  68. gpg -ea -r "${emails[$idxA]}" -r "${emails[$idxB]}" -r "${emails[$idxC]}" "$debug" --output $file.ENCRYPTED $workdir/$file.step$((counter-1))
  69. #normal execution:
  70. else
  71. gpg -ea -r "${emails[$idxA]}" -r "${emails[$idxB]}" -r "${emails[$idxC]}" "$debug" --output $workdir/$file.step$counter $workdir/$file.step$((counter-1))
  72. fi
  73. #increase loop counter:
  74. ((counter++))
  75. fi
  76. done
  77. done
  78. done
  79. #remove work directory:
  80. rm -rf $workdir
  81. #the combination matrix for a quorum of 3 recipients on 5:
  82. #recipients: A,B,C,D,E
  83. #
  84. #A,B,C
  85. #A,B,D
  86. #A,B,E
  87. #A,C,D
  88. #A,C,E
  89. #A,D,E
  90. #B,C,D
  91. #B,C,E
  92. #B,D,E
  93. #C,D,E
  94. #SOURCES:
  95. #http://www.anonhack.in/2018/05/program-to-calculate-combination-ncr-in-bash-shell-scripting/
  96. #https://www.log2base2.com/shell-script-examples/loop/shell-script-to-find-factorial-of-a-number.html
  97. #https://www.dcode.fr/combinations