Makefile: add pickrand
[cmccabe-bin] / enc.sh
1 #!/usr/bin/env bash
2
3 die() {
4     echo $@
5     exit 1
6 }
7
8 if [[ "x${PASS}" = "x" ]]; then
9     read -s -p "enter password: " PASS
10     echo
11     read -s -p "re-enter password: " PASS2
12     [[ ${PASS} == ${PASS2} ]] || die "The passwords did not match."
13 fi
14
15 #[ "x${SALT}" = "x" ] && die "you must set SALT to the salt."
16 #SALT=$(dd count=1024 if=/dev/random 2>/dev/null |md5sum|sed 's/-//')
17 #[ $? -ne 0 ] && die "failed to generate SALT"
18
19 RET=0
20 for FILE in "$@"; do
21     if [[ "${FILE}" = *.nc ]]; then
22         echo "Not encrypting ${FILE} because its name already ends in .nc"
23     else
24         NEW_FILE="${FILE}.nc"
25         if [ -e "${NEW_FILE}" ]; then
26             echo "Not encrypting ${FILE} because there is already a ${NEW_FILE}"
27         else
28             if openssl enc -aes-256-ecb \
29                     -salt \
30                     -k "${PASS}" < "${FILE}" > "${NEW_FILE}"; then
31                 echo "Created ${NEW_FILE}"
32             else
33                 echo "Failed to create ${NEW_FILE}"
34                 RET=1
35             fi
36         fi
37     fi
38 done
39 exit $RET