Create a gist now

Instantly share code, notes, and snippets.

anonymous /btsync-secret
Created Apr 6, 2014

What would you like to do?
btsync-secret - A Bittorrent Sync Secret Generator
#!/bin/sh
#/ Usage: btsync-secret [option]...
#/ A Bittorrent Sync Secret Generator
set -e
h_flag=false
v_flag=false
e_flag=false
n_flag=false
c_flag=false
rod59_secret=""
secret_length=""
rand_binary=""
new_secret=""
function show_help() {
echo -e "\nbtsync-secret, a Bittorrent Sync Secret Generator"
echo "Usage: btsync-secret [option]..."
echo -e "\nRun btsync-secret without flags to generate a standard read-write secret.\n(33 character base32)"
echo -e "\nOptions:"
echo -e " -h\tShow this help."
echo -e " -v\tShow details as secrets are being generated."
echo -e " -e\tGenerate a read-write-encrypted secret.\n\t(33 character base32 with encrypted peer support)\n\t Similar to a standard read-write secret, except Bittorrent\n\t Sync calculates an encrypted hash tree for the folder. All\n\t files are encrypted before being synced to other peers."
echo -e "\n -n\tGenerate a 33 character read-only-encrypted secret from a 59\n\tcharacter read-only-decrypted secret.\n\t A 59 character read-only-decrypted secret decrypts files\n\t synced from an encrypted peer, and stores those files\n\t decrypted on the peer.\n\t A 33 character read-only-encrypted secret cannot decrypt files\n\t synced from an encrypted peer, and stores those files\n\t encrypted on the peer."
echo -e "\n -c\tGenerate a custom base64 secret. Default length is 40 characters.\n\t For custom length, enter a number over 40. btsync-secret\n\t generates a custom length rounded down from your chosen\n\t number to the nearest number divisible by 4.\n"
}
while :
do
case $1 in
-h | --help | -\?)
show_help
exit 0
;;
-v )
v_flag=true
shift
;;
-e )
e_flag=true
shift
test "$2" = "-v" && {
v_flag=true
}
;;
-n )
n_flag=true
shift
rod59_secret="$1"
test "$1" = "-v" && {
v_flag=true
rod59_secret="$2"
}
test "$2" = "-v" && {
v_flag=true
rod59_secret="$1"
}
break
;;
-c )
c_flag=true
shift
secret_length="$1"
test "$1" = "-v" && {
v_flag=true
secret_length="$2"
}
test "$2" = "-v" && {
v_flag=true
secret_length="$1"
}
break
;;
--) # End of all options
shift
break
;;
*[!-hvenc]*)
printf >&2 'WARN: Unknown option (ignored): %s\n' "$1"
exit 1
;;
-*)
printf >&2 'WARN: Unknown option (ignored): %s\n' "$1"
exit 1
;;
*) # no more options. Stop while loop
break
;;
esac
done
function gen_base32_secret() {
rand_binary=`head -c20 /dev/random`
new_secret=`python -c "import base64; print base64.b32encode('''$rand_binary''')"`
case "$new_secret" in
*=*) gen_base32_secret ;;
esac
}
function gen_base64_secret() {
head -c$(($secret_length/4*3)) /dev/random | base64 -w0
}
function gen_roe33_secret {
echo -n "F"
echo -n "$rod59_secret" | head -c33 | cut -c2-
}
test ! "$h_flag" = true && test ! "$e_flag" = true && test ! "$n_flag" = true && test ! "$c_flag" = true && {
test "$v_flag" = true && {
test -n "$1" && {
echo -e "\nYour input of \"$1\" was ignored.\nUse the -c flag to generate a secret of custom length."
}
echo -e "\nGenerating a standard read-write secret...\n(33 character base32)\n"
}
gen_base32_secret
echo -e "A$new_secret"
test "$v_flag" = true && {
echo " "
}
exit 0
}
test "$h_flag" = true && test ! "$e_flag" = true && test ! "$n_flag" = true && test ! "$c_flag" = true && {
show_help
exit 0
}
test ! "$h_flag" = true && test "$e_flag" = true && test ! "$n_flag" = true && test ! "$c_flag" = true && {
test "$v_flag" = true && {
test -n "$1" && {
echo -e "\nYour input of \"$1\" was ignored.\nUse the -c flag to generate a secret of custom length."
}
echo -e "\nGenerating a read-write-encrypted secret...\n(33 character base32 with encrypted peer support)\n"
}
gen_base32_secret
echo -e "D$new_secret"
test "$v_flag" = true && {
echo " "
}
exit 0
}
test ! "$h_flag" = true && test ! "$e_flag" = true && test "$n_flag" = true && test ! "$c_flag" = true && {
test -z "$rod59_secret" && {
test "$v_flag" = true && {
echo -e "\nEnter a 59 character read-only-decrypted secret to\ngenerate a 33 character read-only-encrypted secret.\n"
exit 0
}
exit 0
}
test -n "$rod59_secret" && {
is_rod59_secret=`echo -n "$rod59_secret" | wc -m`
test "$is_rod59_secret" = "59" && {
test "$v_flag" = true && {
echo -e "\nGenerating a read-only-encrypted secret...\n(33 character base32)\n"
}
gen_roe33_secret
test "$v_flag" = true && {
echo " "
}
exit 0
}
echo -e "\nEnter a 59 character read-only-decrypted secret to\ngenerate a 33 character read-only-encrypted secret.\n"
exit 0
}
}
test ! "$h_flag" = true && test ! "$e_flag" = true && test ! "$n_flag" = true && test "$c_flag" = true && {
test -z "$secret_length" && {
test "$v_flag" = true && {
echo -e "\nGenerating a custom read-write secret...\n(40 character base64)\n"
}
secret_length=40
gen_base64_secret
test "$v_flag" = true && {
echo -e "\n"
exit 0
}
echo
exit 0
}
test -n "$secret_length" && {
case $secret_length in
''|*[!0-9]*)
echo -e "\nFor a secret of custom length, enter a number over 40\n"
exit 0
;;
*)
test "$secret_length" -ge 40 && {
test "$v_flag" = true && {
echo -e "\nGenerating a custom length read-write secret...\n("$((($secret_length/4)*4))" character base64)\n"
}
gen_base64_secret
test "$v_flag" = true && {
echo -e "\n"
exit 0
}
echo
exit 0
}
echo -e "\nFor a secret of custom length, enter a number over 40\n"
exit 0
;;
esac
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment