2019-06-20 01:39:23 +00:00
|
|
|
#!/usr/bin/env sh
|
|
|
|
|
|
|
|
# Description: Create and verify checksums
|
|
|
|
#
|
2022-09-19 06:52:32 +00:00
|
|
|
# Note: On macOS, install the relevant checksum packages from Homebrew with:
|
|
|
|
# brew install coreutils
|
2022-09-17 16:09:52 +00:00
|
|
|
#
|
2021-05-15 17:32:01 +00:00
|
|
|
# Details:
|
|
|
|
# - selection: it will generate one file with the checksums and filenames
|
|
|
|
# (and with paths if they are in another directory)
|
|
|
|
# output checksum filename format: checksum_timestamp.checksum_type
|
|
|
|
# - file: if the file is a checksum, the plugin does the verification
|
2019-10-31 20:58:25 +00:00
|
|
|
# if the file is not a checksum, checksum will be generated for it
|
|
|
|
# the output checksum filename will be filename.checksum_type
|
2021-05-15 17:32:01 +00:00
|
|
|
# - directory: recursively calculates checksum for all the files in the dir
|
2019-10-31 20:58:25 +00:00
|
|
|
# the output checksum filename will be directory.checksum_type
|
2019-06-20 01:39:23 +00:00
|
|
|
#
|
|
|
|
# Shell: POSIX compliant
|
2020-05-06 05:12:29 +00:00
|
|
|
# Authors: ath3, Arun Prakash Jana
|
2019-06-20 01:39:23 +00:00
|
|
|
|
2020-04-24 04:42:24 +00:00
|
|
|
selection=${NNN_SEL:-${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection}
|
2019-06-20 01:39:23 +00:00
|
|
|
resp=f
|
|
|
|
chsum=md5
|
|
|
|
|
|
|
|
checksum_type()
|
|
|
|
{
|
|
|
|
echo "possible checksums: md5, sha1, sha224, sha256, sha384, sha512"
|
2019-11-21 20:44:25 +00:00
|
|
|
printf "create md5 (m), sha256 (s), sha512 (S) (or type one of the above checksums) [default=m]: "
|
|
|
|
read -r chsum_resp
|
2019-06-20 01:39:23 +00:00
|
|
|
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
|
2019-11-21 20:44:25 +00:00
|
|
|
printf "work with selection (s) or current file (f) [default=f]: "
|
|
|
|
read -r resp
|
2019-06-20 01:39:23 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$resp" = "s" ]; then
|
|
|
|
checksum_type
|
2019-08-23 16:13:32 +00:00
|
|
|
sed 's|'"$PWD/"'||g' < "$selection" | xargs -0 -I{} ${chsum}sum {} > "checksum_$(date '+%Y%m%d%H%M').$chsum"
|
2021-03-27 11:37:58 +00:00
|
|
|
|
|
|
|
# Clear selection
|
|
|
|
if [ -p "$NNN_PIPE" ]; then
|
|
|
|
printf "-" > "$NNN_PIPE"
|
|
|
|
fi
|
2019-10-31 20:58:25 +00:00
|
|
|
elif [ -n "$1" ]; then
|
|
|
|
if [ -f "$1" ]; then
|
2019-06-20 01:39:23 +00:00
|
|
|
for chks in md5 sha1 sha224 sha256 sha384 sha512
|
|
|
|
do
|
2019-11-21 20:44:25 +00:00
|
|
|
if echo "$1" | grep -q \.${chks}$; then
|
2019-06-20 01:39:23 +00:00
|
|
|
${chks}sum -c < "$1"
|
2019-11-21 20:44:25 +00:00
|
|
|
read -r _
|
2023-01-25 19:07:08 +00:00
|
|
|
exit
|
2019-06-20 01:39:23 +00:00
|
|
|
fi
|
|
|
|
done
|
2019-06-27 00:42:27 +00:00
|
|
|
checksum_type
|
|
|
|
file=$(basename "$1").$chsum
|
|
|
|
${chsum}sum "$1" > "$file"
|
2019-10-31 20:58:25 +00:00
|
|
|
elif [ -d "$1" ]; then
|
|
|
|
checksum_type
|
|
|
|
file=$(basename "$1").$chsum
|
|
|
|
find "$1" -type f -exec ${chsum}sum "{}" + > "$file"
|
2019-06-20 01:39:23 +00:00
|
|
|
fi
|
|
|
|
fi
|