aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/chksum
diff options
context:
space:
mode:
authorGravatar Arun Prakash Jana <engineerarun@gmail.com>2019-11-22 17:51:13 +0530
committerGravatar Arun Prakash Jana <engineerarun@gmail.com>2019-11-22 17:51:13 +0530
commit594d645e259083b217518ef0507edfe5395711af (patch)
tree531fcf59d50287011797fd3179258379b0376622 /plugins/chksum
parent085bda03305ffd5cb502743a06ac6b2513b7234e (diff)
downloadnnn-594d645e259083b217518ef0507edfe5395711af.tar.gz
Rename plugins, group related ones
Diffstat (limited to 'plugins/chksum')
-rwxr-xr-xplugins/chksum66
1 files changed, 66 insertions, 0 deletions
diff --git a/plugins/chksum b/plugins/chksum
new file mode 100755
index 0000000..0c13da2
--- /dev/null
+++ b/plugins/chksum
@@ -0,0 +1,66 @@
+#!/usr/bin/env sh
+
+# Description: Create and verify checksums
+#
+# For selection: it will generate one file containing the checksums with file names
+# [and with paths if they are in another directory]
+# the output checksum filename will be checksum_timestamp.checksum_type
+# For file: if the file is a checksum, the plugin does the verification
+# if the file is not a checksum, checksum will be generated for it
+# the output checksum filename will be filename.checksum_type
+# For directory: recursively calculates checksum for all the files in the directory
+# the output checksum filename will be directory.checksum_type
+#
+# Shell: POSIX compliant
+# Author: ath3, Arun Prakash Jana
+
+selection=${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection
+resp=f
+chsum=md5
+
+checksum_type()
+{
+ echo "possible checksums: md5, sha1, sha224, sha256, sha384, sha512"
+ printf "create md5 (m), sha256 (s), sha512 (S) (or type one of the above checksums) [default=m]: "
+ read -r chsum_resp
+ for chks in md5 sha1 sha224 sha256 sha384 sha512
+ do
+ if [ "$chsum_resp" = "$chks" ]; then
+ chsum=$chsum_resp
+ return
+ fi
+ done
+ if [ "$chsum_resp" = "s" ]; then
+ chsum=sha256
+ elif [ "$chsum_resp" = "S" ]; then
+ chsum=sha512
+ fi
+}
+
+if [ -s "$selection" ]; then
+ printf "work with selection (s) or current file (f) [default=f]: "
+ read -r resp
+fi
+
+if [ "$resp" = "s" ]; then
+ checksum_type
+ sed 's|'"$PWD/"'||g' < "$selection" | xargs -0 -I{} ${chsum}sum {} > "checksum_$(date '+%Y%m%d%H%M').$chsum"
+elif [ -n "$1" ]; then
+ if [ -f "$1" ]; then
+ for chks in md5 sha1 sha224 sha256 sha384 sha512
+ do
+ if echo "$1" | grep -q \.${chks}$; then
+ ${chks}sum -c < "$1"
+ read -r _
+ return
+ fi
+ done
+ checksum_type
+ file=$(basename "$1").$chsum
+ ${chsum}sum "$1" > "$file"
+ elif [ -d "$1" ]; then
+ checksum_type
+ file=$(basename "$1").$chsum
+ find "$1" -type f -exec ${chsum}sum "{}" + > "$file"
+ fi
+fi