aboutsummaryrefslogtreecommitdiffstats
path: root/jsaccess/encrypt.sh
blob: 31029366f46d179d8d4a7f06f557eb7f4e1b2f44 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/bin/sh

cleanup() {
	rm -f $tmp
	umask $sumask
	exit 0
}

if [ $# -ne 1 ]; then
	echo "usage: encrypt.sh <myfile>"
	exit 1
fi
myfile=$1
myfilename=`basename $myfile`

sumask=$(umask)
umask 077
tmp=`mktemp ./jsaXXXXXXXX`
trap cleanup INT TERM EXIT

echo -n "Enter passphrase used to encrypt: "
read pass
dirhash=`echo -n $pass |openssl rmd160 |cut -d' ' -f2`
dir="jsa/files/$dirhash"
file=`echo -n ${dirhash}${myfilename} |openssl rmd160 |cut -d' ' -f2`
echo $dir
mkdir -p $dir
touch $dir/index.html

base64 -w0 $myfile > $tmp
echo -n $pass |openssl enc -e -a -aes-256-cbc -in $tmp -out $dir/$file -pass stdin ||exit $?
echo "CREATED $dir/$file"

if [ -f $dir/index.txt ]; then
	echo -n $pass |openssl enc -d -a -aes-256-cbc -in $dir/index.txt -out $tmp -pass stdin ||exit $?
else
	echo > $tmp
fi
echo $myfilename >> $tmp
rm -f $dir/index.txt
echo -n $pass |openssl enc -e -a -aes-256-cbc -in $tmp -out $dir/index.txt -pass stdin ||exit $?
echo "UPDATED $dir/index.txt"