chksum (2256B)
1 #!/usr/bin/env sh 2 3 # Description: Create and verify checksums 4 # 5 # Details: 6 # - selection: it will generate one file with the checksums and filenames 7 # (and with paths if they are in another directory) 8 # output checksum filename format: checksum_timestamp.checksum_type 9 # - file: if the file is a checksum, the plugin does the verification 10 # if the file is not a checksum, checksum will be generated for it 11 # the output checksum filename will be filename.checksum_type 12 # - directory: recursively calculates checksum for all the files in the dir 13 # the output checksum filename will be directory.checksum_type 14 # 15 # Shell: POSIX compliant 16 # Authors: ath3, Arun Prakash Jana 17 18 selection=${NNN_SEL:-${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection} 19 resp=f 20 chsum=md5 21 22 checksum_type() 23 { 24 echo "possible checksums: md5, sha1, sha224, sha256, sha384, sha512" 25 printf "create md5 (m), sha256 (s), sha512 (S) (or type one of the above checksums) [default=m]: " 26 read -r chsum_resp 27 for chks in md5 sha1 sha224 sha256 sha384 sha512 28 do 29 if [ "$chsum_resp" = "$chks" ]; then 30 chsum=$chsum_resp 31 return 32 fi 33 done 34 if [ "$chsum_resp" = "s" ]; then 35 chsum=sha256 36 elif [ "$chsum_resp" = "S" ]; then 37 chsum=sha512 38 fi 39 } 40 41 if [ -s "$selection" ]; then 42 printf "work with selection (s) or current file (f) [default=f]: " 43 read -r resp 44 fi 45 46 if [ "$resp" = "s" ]; then 47 checksum_type 48 sed 's|'"$PWD/"'||g' < "$selection" | xargs -0 -I{} ${chsum}sum {} > "checksum_$(date '+%Y%m%d%H%M').$chsum" 49 50 # Clear selection 51 if [ -p "$NNN_PIPE" ]; then 52 printf "-" > "$NNN_PIPE" 53 fi 54 elif [ -n "$1" ]; then 55 if [ -f "$1" ]; then 56 for chks in md5 sha1 sha224 sha256 sha384 sha512 57 do 58 if echo "$1" | grep -q \.${chks}$; then 59 ${chks}sum -c < "$1" 60 read -r _ 61 return 62 fi 63 done 64 checksum_type 65 file=$(basename "$1").$chsum 66 ${chsum}sum "$1" > "$file" 67 elif [ -d "$1" ]; then 68 checksum_type 69 file=$(basename "$1").$chsum 70 find "$1" -type f -exec ${chsum}sum "{}" + > "$file" 71 fi 72 fi