add openssl encrypt / decrypt wrappers
authorColin Patrick Mccabe <cmccabe@alumni.cmu.edu>
Mon, 23 Mar 2015 06:13:02 +0000 (23:13 -0700)
committerColin Patrick Mccabe <cmccabe@alumni.cmu.edu>
Mon, 23 Mar 2015 06:13:02 +0000 (23:13 -0700)
Signed-off-by: Colin McCabe <cmccabe@alumni.cmu.edu>

dec.sh [new file with mode: 0755]
enc.sh [new file with mode: 0755]

diff --git a/dec.sh b/dec.sh
new file mode 100755 (executable)
index 0000000..ef4aaa1
--- /dev/null
+++ b/dec.sh
@@ -0,0 +1,31 @@
+#!/usr/bin/env bash
+
+die() {
+    echo $@
+    exit 1
+}
+
+[ "x${SALT}" = "x" ] && die "you must set SALT to the salt."
+[ "x${PASS}" = "x" ] && die "you must set PASS to the password"
+
+RET=0
+for FILE in "$@"; do
+    if [[ "${FILE}" = *.nc ]]; then
+        BASE_FILE="$(dirname ${FILE})/$(basename ${FILE} .nc)"
+        if [ -e "${BASE_FILE}" ]; then
+            echo "Not decrypting $FILE because there is already a ${BASE_FILE}"
+        else
+            if openssl enc -d -aes-256-ecb \
+                    -S "$SALT" \
+                    -k "$PASS" < "${FILE}" > "${BASE_FILE}"; then
+                echo "Created ${BASE_FILE}"
+            else
+                echo "Failed to create ${BASE_FILE}"
+                RET=1
+            fi
+        fi
+    else
+        echo "Not decrypting $FILE because its name does not end in .nc"
+    fi
+done
+exit $RET
diff --git a/enc.sh b/enc.sh
new file mode 100755 (executable)
index 0000000..a0ec8ef
--- /dev/null
+++ b/enc.sh
@@ -0,0 +1,31 @@
+#!/usr/bin/env bash
+
+die() {
+    echo $@
+    exit 1
+}
+
+[ "x${SALT}" = "x" ] && die "you must set SALT to the salt."
+[ "x${PASS}" = "x" ] && die "you must set PASS to the password"
+
+RET=0
+for FILE in "$@"; do
+    if [[ "${FILE}" = *.nc ]]; then
+        echo "Not encrypting ${FILE} because its name already ends in .nc"
+    else
+        NEW_FILE="${FILE}.nc"
+        if [ -e "${NEW_FILE}" ]; then
+            echo "Not encrypting ${FILE} because there is already a ${NEW_FILE}"
+        else
+            if openssl enc -aes-256-ecb \
+                    -S "${SALT}" \
+                    -k "${PASS}" < "${FILE}" > "${NEW_FILE}"; then
+                echo "Created ${NEW_FILE}"
+            else
+                echo "Failed to create ${NEW_FILE}"
+                RET=1
+            fi
+        fi
+    fi
+done
+exit $RET