summaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authorMarc Vertes <mvertes@free.fr>2025-04-26 12:32:31 +0200
committerMarc Vertes <mvertes@free.fr>2025-04-26 12:32:31 +0200
commitdf31a7764ff9168b211070b8ccc08186a8d06d3a (patch)
tree8ea5eb868a57f417ed46af638c0a5854af947f06 /bin
parent7d4158e20b29a7d1d922ad1567f802f9e4865e44 (diff)
parent733d2209b0c78adaf55baf26b4aa67ef08f057ab (diff)
Merge github.com:mvertes/dotfiles
Diffstat (limited to 'bin')
l---------bin/cle1
-rwxr-xr-xbin/crypt45
2 files changed, 46 insertions, 0 deletions
diff --git a/bin/cle b/bin/cle
new file mode 120000
index 0000000..a0092cf
--- /dev/null
+++ b/bin/cle
@@ -0,0 +1 @@
+../.cle/cle \ No newline at end of file
diff --git a/bin/crypt b/bin/crypt
new file mode 100755
index 0000000..64afc42
--- /dev/null
+++ b/bin/crypt
@@ -0,0 +1,45 @@
+#!/bin/sh
+
+crypt_usage='Usage: crypt [-d] [-o output] [input]
+
+Encrypt or decrypt input (stdin) to ouput (stdout), using ssh rsa key.
+
+Options:
+ -d action is decrypt (default: encrypt)
+ -o output set ouput (default: stdout)'
+
+# Encrypt stdin to stdout.
+encrypt() {
+ set -- "$(openssl rand -hex 32)"
+
+ echo "$1" | openssl pkeyutl -encrypt -pubin -inkey /dev/fd/3 3<<- EOF
+ $(ssh-keygen -e -f ~/.ssh/id_rsa.pub -m PKCS8)
+ EOF
+
+ openssl aes-256-cbc -pbkdf2 -pass file:/dev/fd/3 3<<- EOF
+ $1
+ EOF
+}
+
+# Decrypt stdin to stdout.
+decrypt() {
+ openssl aes-256-cbc -d -pbkdf2 -pass file:/dev/fd/3 3<<- EOF
+ $(dd ibs=256 count=1 iflag=direct status=none |
+ openssl pkeyutl -decrypt -inkey ~/.ssh/id_rsa)
+ EOF
+}
+
+# Execute main only if not sourced.
+if [ "${0##*/}" = "crypt" ]; then
+ cmd=encrypt
+ while getopts :do: opt; do
+ case $opt in
+ d) cmd=decrypt ;;
+ o) exec 1>"$OPTARG" ;;
+ *) echo "$crypt_usage" >&2; exit 1 ;;
+ esac
+ done
+ shift $((OPTIND - 1))
+ [ "$1" ] && exec 0<"$1"
+ "$cmd"
+fi