sistema_progs

Programas para customizar o meu entorno de traballo nos meus equipos persoais
Log | Files | Refs

chksum (2370B)


      1 #!/usr/bin/env sh
      2 
      3 # Description: Create and verify checksums
      4 #
      5 # Note:  On macOS, install the relevant checksum packages from Homebrew with:
      6 #            brew install coreutils
      7 #
      8 # Details:
      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
     17 #
     18 # Shell: POSIX compliant
     19 # Authors: ath3, Arun Prakash Jana
     20 
     21 selection=${NNN_SEL:-${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection}
     22 resp=f
     23 chsum=md5
     24 
     25 checksum_type()
     26 {
     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]: "
     29     read -r chsum_resp
     30     for chks in md5 sha1 sha224 sha256 sha384 sha512
     31     do
     32         if [ "$chsum_resp" = "$chks" ]; then
     33             chsum=$chsum_resp
     34             return
     35         fi
     36     done
     37     if [ "$chsum_resp" = "s" ]; then
     38         chsum=sha256
     39     elif [ "$chsum_resp" = "S" ]; then
     40         chsum=sha512
     41     fi
     42 }
     43 
     44 if [ -s "$selection" ]; then
     45     printf "work with selection (s) or current file (f) [default=f]: "
     46     read -r resp
     47 fi
     48 
     49 if [ "$resp" = "s" ]; then
     50     checksum_type
     51     sed 's|'"$PWD/"'||g' < "$selection" | xargs -0 -I{} ${chsum}sum {} > "checksum_$(date '+%Y%m%d%H%M').$chsum"
     52 
     53     # Clear selection
     54     if [ -p "$NNN_PIPE" ]; then
     55         printf "-" > "$NNN_PIPE"
     56     fi
     57 elif [ -n "$1" ]; then
     58     if [ -f "$1" ]; then
     59         for chks in md5 sha1 sha224 sha256 sha384 sha512
     60         do
     61             if echo "$1" | grep -q \.${chks}$; then
     62                 ${chks}sum -c < "$1"
     63                 read -r _
     64                 exit
     65             fi
     66         done
     67         checksum_type
     68         file=$(basename "$1").$chsum
     69         ${chsum}sum "$1" > "$file"
     70     elif [ -d "$1" ]; then
     71         checksum_type
     72         file=$(basename "$1").$chsum
     73         find "$1" -type f -exec ${chsum}sum "{}" + > "$file"
     74     fi
     75 fi