3 # Description: Create and verify checksums
5 # Note: On macOS, install the relevant checksum packages from Homebrew with:
6 # brew install coreutils
9 # - selection: it will generate one file with the checksums and filenames
10 # (and with paths if they are in another directory)
11 # output checksum filename format: checksum_timestamp.checksum_type
12 # - file: if the file is a checksum, the plugin does the verification
13 # if the file is not a checksum, checksum will be generated for it
14 # the output checksum filename will be filename.checksum_type
15 # - directory: recursively calculates checksum for all the files in the dir
16 # the output checksum filename will be directory.checksum_type
18 # Shell: POSIX compliant
19 # Authors: ath3, Arun Prakash Jana
21 selection=${NNN_SEL:-${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection}
27 echo "possible checksums: md5, sha1, sha224, sha256, sha384, sha512"
28 printf "create md5 (m), sha256 (s), sha512 (S) (or type one of the above checksums) [default=m]: "
30 for chks in md5 sha1 sha224 sha256 sha384 sha512
32 if [ "$chsum_resp" = "$chks" ]; then
37 if [ "$chsum_resp" = "s" ]; then
39 elif [ "$chsum_resp" = "S" ]; then
44 if [ -s "$selection" ]; then
45 printf "work with selection (s) or current file (f) [default=f]: "
49 if [ "$resp" = "s" ]; then
51 sed 's|'"$PWD/"'||g' < "$selection" | xargs -0 -I{} ${chsum}sum {} > "checksum_$(date '+%Y%m%d%H%M').$chsum"
54 if [ -p "$NNN_PIPE" ]; then
55 printf "-" > "$NNN_PIPE"
57 elif [ -n "$1" ]; then
59 for chks in md5 sha1 sha224 sha256 sha384 sha512
61 if echo "$1" | grep -q \.${chks}$; then
68 file=$(basename "$1").$chsum
69 ${chsum}sum "$1" > "$file"
70 elif [ -d "$1" ]; then
72 file=$(basename "$1").$chsum
73 find "$1" -type f -exec ${chsum}sum "{}" + > "$file"