From 682e810e6e893a1b8824a9eef032ff1fea9eaafc Mon Sep 17 00:00:00 2001 From: ath3 Date: Thu, 20 Jun 2019 03:39:23 +0200 Subject: [PATCH] Extended md5sum script to support multiple checksums and renamed it to checksum --- plugins/checksum | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ plugins/md5sum | 34 ------------------------- 2 files changed, 64 insertions(+), 34 deletions(-) create mode 100755 plugins/checksum delete mode 100755 plugins/md5sum diff --git a/plugins/checksum b/plugins/checksum new file mode 100755 index 00000000..44f2274c --- /dev/null +++ b/plugins/checksum @@ -0,0 +1,64 @@ +#!/usr/bin/env sh + +# Description: Create and verify checksums +# +# If selection is used: it will generate one file containing the checksums with file names +# [and with paths if they are in another directory] +# The filename will be checksum_timestamp.checksum_type +# If file is used: if the file is a checksum, it does the verification +# if the file is not a checksum, it will be created from it +# The filename will be filename.checksum_type +# +# Shell: POSIX compliant +# Author: ath3 + +selection=~/.config/nnn/.selection +resp=f +chsum=md5 +ischksum=0 + +checksum_type() +{ + echo "possible checksums: md5, sha1, sha224, sha256, sha384, sha512" + echo -n "create md5 (m), sha256 (s), sha512 (S) (or type one of the above checksums) [default=m]: " + read 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 + echo -n "work with selection (s) or current file (f) [default=f]: " + read 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" +else + if [ -n "$1" ] && [ -f "$1" ]; then + for chks in md5 sha1 sha224 sha256 sha384 sha512 + do + if [ "$(echo "$1" | grep \.${chks}$)" ]; then + ischksum=1 + ${chks}sum -c < "$1" + read + exit + fi + done + if [ $ischksum -eq 0 ]; then + checksum_type + file=$(basename "$1").$chsum + ${chsum}sum "$1" > "$file" + fi + fi +fi diff --git a/plugins/md5sum b/plugins/md5sum deleted file mode 100755 index 76a2571d..00000000 --- a/plugins/md5sum +++ /dev/null @@ -1,34 +0,0 @@ -#!/usr/bin/env sh - -# Description: Create and verify md5 checksums -# -# If selection is used: it will generate one md5 file containing the checksums and file names -# [with paths if they are in another directory] -# If file is used: if the file is .md5 file, then it does the check -# if the file is not .md5 file, it creates the md5 file from it -# -# Shell: POSIX compliant -# Author: ath3 - -selection=~/.config/nnn/.selection -resp=f - -if [ -s "$selection" ]; then - echo -n "work with selection (s) or current file (f) [default=f]: " - read resp -fi - -if [ "$resp" = "s" ]; then - file=$(basename "$(cat $selection | tr '\0' '\n' | head -n 1)").md5 - cat "$selection" | sed 's|'"$PWD/"'||g' | xargs -0 -i md5sum {} > "$file" -else - if ! [ -z "$1" ] && [ -f "$1" ]; then - if [ $(echo $1 | grep \.md5$) ]; then - cat "$1" | md5sum -c - read - else - file=$(basename "$1").md5 - md5sum "$1" > "$file" - fi - fi -fi