View on GitHub

bash-funk

bash-funk is a collection of useful commands for Bash 3.2 or higher.

Bash-Funk “crypto” module

The following commands are available when this module is loaded:

  1. -md5sum
  2. -sha256sum
  3. -test-all-crypto
  4. -verify-tar-md5

License

SPDX-FileCopyrightText: © Vegard IT GmbH (https://vegardit.com)
SPDX-License-Identifier: Apache-2.0

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-md5sum

Usage: -md5sum [OPTION]... PATH_TO_FILE

Calculates the MD5 hash of the given file.

Parameters:
  PATH_TO_FILE (required, file)
      The file.

Options:
    --help
        Prints this help.
    --tracecmd
        Enables bash debug mode (set -x).
    --selftest
        Performs a self-test.
    --
        Terminates the option list.

Implementation:

# use md5sum if available
if hash md5sum &>/dev/null; then
   md5sum $_PATH_TO_FILE | cut -d ' ' -f1

# use perl if available
elif hash perl &>/dev/null; then
   perl << EOF
use Digest::MD5;
open(my \$FILE, '$_PATH_TO_FILE');
binmode(\$FILE);
print Digest::MD5->new->addfile(\$FILE)->hexdigest, "\n";
close(\$FILE);
EOF

# use python as last resort
else
   python -c "import hashlib
print(hashlib.md5(open('$_PATH_TO_FILE').read()).hexdigest())"
fi

-sha256sum

Usage: -sha256sum [OPTION]... PATH_TO_FILE

Calculates the SHA256 hash of the given file.

Parameters:
  PATH_TO_FILE (required, file)
      The file.

Options:
    --help
        Prints this help.
    --tracecmd
        Enables bash debug mode (set -x).
    --selftest
        Performs a self-test.
    --
        Terminates the option list.

Implementation:

# use sha256 if available
if hash sha256 &>/dev/null; then
   sha256 $_PATH_TO_FILE | cut -d ' ' -f1

# use perl if available
elif hash perl &>/dev/null; then
   perl << EOF
use Digest::SHA;
open(my \$FILE, '$_PATH_TO_FILE');
binmode(\$FILE);
print Digest::SHA->new(256)->addfile(\$FILE)->hexdigest, "\n";
close(\$FILE);
EOF

# use python as last resort
else
   python -c "import hashlib
print(hashlib.sha256(open('$_PATH_TO_FILE').read()).hexdigest())"
fi

-test-all-crypto

Usage: -test-all-crypto [OPTION]...

Performs a selftest of all functions of this module by executing each function with option '--selftest'.

Options:
    --help
        Prints this help.
    --tracecmd
        Enables bash debug mode (set -x).
    --selftest
        Performs a self-test.
    --
        Terminates the option list.

Implementation:

-md5sum --selftest && echo || return 1
-sha256sum --selftest && echo || return 1
-verify-tar-md5 --selftest && echo || return 1

-verify-tar-md5

Usage: -verify-tar-md5 [OPTION]... [PATH_TO_ARCHIVE]...

Verifies the MD5 sum of tar files with embedded checksum information. Usually Android firmware archives, see https://fileinfo.com/extension/tar.md5.

Parameters:
  PATH_TO_ARCHIVE (file)
      The tar.md5 file.

Options:
    --help
        Prints this help.
    --tracecmd
        Enables bash debug mode (set -x).
    --selftest
        Performs a self-test.
    --
        Terminates the option list.

Implementation:

local path mismatch=0
echo "Verifying..."
for path in "${_PATH_TO_ARCHIVE[@]}"; do
   echo -n "$path "
   local embedded_md5=$(tail -1 "$path" | cut -f1 -d' ' | tr '[:upper:]' '[:lower:]')
   local actual_md5=$(head -n -1 "$path" | md5sum | cut -f1 -d' ')
   if [[ $embedded_md5 == $actual_md5 ]]; then
      echo "OK"
   else
      echo "FAILED"
      mismatch=1
      echo "  -> embedded MD5 sum: $embedded_md5"
      echo "  ->   actual MD5 sum: $actual_md5"
   fi
done

if [[ $mismatch == 1 ]]; then
   return 1
fi