-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

>> Get new attached version and if you have some spare time, check it
> There wasn't anything attached...
Again?! :-D

# EOF
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFFDZwuoXByE0GKaE0RAglTAJ9GNzxdPIXIWhrOc3Pgl6PIyU/XAQCfR4Uy
zIbeDEZMKjVatimdHEg9I5M=
=LqHe
-----END PGP SIGNATURE-----
#!/bin/bash
# 
# Mimics {md5,sha1}sum coreutils' apps using openssl.
# 
# 2006-09-17    Mordae <[EMAIL PROTECTED]>
#       * Added options --digest and -d for digest selection.
#       * --binary, -b, --text, -t added to specify I/O type of file in digest
#       * --list-digests, -l now lists digests in openssl (not reliable)
# 

# File separator
sep=$(echo -ne '\a')

# Collected files (separated by ${sep})
files=''

action='sum'
quiet='false'
selDigest=''

# Either ' ' or '*', space means text and star binary...
binary=' '
while [ $# -gt 0 ]; do
        case "${1}" in
                (--)
                        shift
                        while [ $# -gt 0 ]; do
                                files="${files}${sep}${1}"
                                shift
                        done
                        ;;
                (--*)
                        case "${1#??}" in
                                (check)         action='check'          ;;
                                (status)        quiet='true'            ;;
                                (help)          action='help'           ;;
                                (version)       action='version'        ;;
                                (binary)        binary='*'              ;;
                                (text)          binary=' '              ;;
                                (list-digests)  action='list'           ;;
                                (digest)
                                                selDigest=${2}
                                                shift
                                                ;;
                                (warn)
                                        echo "${0}: ignored -- ${1}" >&2
                                        ;;
                                (*)
                                        echo "${0}: unknown option -- ${1}" >&2
                                        ;;
                        esac
                        ;;
                (-*)
                        for opt in $(echo "${1#?}" | sed 's#.#\0 #g'); do
                                case "${opt}" in
                                        (w)
                                                echo "${0} ignored: -${opt}" >&2
                                                ;;
                                        (d)
                                                selDigest=${2}
                                                shift
                                                ;;
                                        (l)     action='list'           ;;
                                        (b)     binary='*'              ;;
                                        (t)     binary=' '              ;;
                                        (c)     action='check'          ;;
                                        (*)
                                                echo "${0}: unknown option -- 
-${opt}" >&2
                                                ;;
                                esac
                        done
                        ;;
                (*)
                        files="${files}${sep}${1}"
                        ;;
        esac
        shift
done

if [ -z "${files}" ]; then
        files='-'
fi

list_digests ( )
{
        echo $(openssl dgst --help 2>&1 |
                grep -A 100 '^-md5' |
                sed -r 's#^-([a-zA-Z0-9]+).*#\1#')
}

openssl_sum ( )
{
        if [ x"${1}" = x'-' ]; then
                openssl dgst "-${digest}" || return 1
        else
                openssl dgst "-${digest}" <"${1}" || return 1
        fi
}

do_sum ( )
{
        local sum=$(openssl_sum "${1}") || return 1
        echo "${sum} ${binary}${1}"
}

check ( )
{
        curr_sum=$(openssl_sum "${1}") || return 1
        if [ x"${curr_sum}" = x"${2}" ]; then
                return 0
        fi
        return 1
}

do_check ( )
{
        local sum=''
        local res=0
        
        exec 9< "${1}"
        while read line <&9; do
                sum=${line%% *}
                file=${line#* }
                [[ "${file}" =~ '^\*' ]] && file=${file#?}
                (( check_count++ ))
                if check "${file}" "${sum}"; then
                        echo "${file}: OK"
                else
                        echo "${file}: FAILED"
                        (( check_failed++ ))
                        res=1
                fi
        done
        exec 9<&-
        return ${res}
}

case "${action}" in
        (list)
                list_digests
                ;;
        (sum|check)
                me=${0##*/}
                digest=${me%sum}
                if ! [[ "${me}" =~ '.+sum$' ]] && [ -z "${selDigest}" ]; then
                        echo "No digest selected, use --help to learn how to 
specify." >&2
                        exit 3
                fi
                [ -n "${selDigest}" ] && digest=${selDigest}
                
                if ! echo 'test' | openssl dgst "-${digest}" &>/dev/null; then
                        echo "${0}: digest ${digest} not supported by openssl" 
>&2
                        echo 'Supported digests:' $(list_digests) >&2
                        exit 2
                fi
                
                res=0
                check_count=0
                check_failed=0
                IFS=${sep}
                for file in ${files}; do
                        [ -z "${file}" ] && continue
                        [ x"${file}" = x'-' -o -f "${file}" ] || {
                                echo "${0}: ${file}: No such file or directory" 
>&2
                                res=1
                                continue
                        }
                        if [ "${action}" = 'sum' ]; then
                                do_sum "${file}"
                        else
                                do_check "${file}" || res=$?
                        fi
                done
                if [ ${check_failed} -gt 0 ]; then
                        echo "${0}: WARNING: ${check_failed} of ${check_count} 
computed checksum did NOT match" >&2
                fi
                exit ${res}
                ;;
        (help)
                cat <<EOT
Usage: ${0##*/} [OPTION] [FILE]...
Print or check file checksums using openssl's message digest algorythms.
With no FILE, or when FILE is -, read standard input.

  -b, --binary            read in binary mode
  -c, --check             read digest sums from the FILEs and check them
  -t, --text              read in text mode (default)

Following options are specific to this particular script and are not supported
by original md5sum and sha1sum executables:

  -d, --digest            Selects digest to use, use \`${0##*/} --list-digests\`
                          to get list of all suported digests.
                          If evoked as md5sum, defaults to md5, same for others.

  -l, --list-digests      Lists available digests. This depends on openssl's
                          output formatting and may not be reliable.

The following two options are useful only when verifying checksums:
      --status            don't output anything, status code shows success

      --help     display this help and exit
      --version  output version information and exit

The default mode is to print a line with checksum, a character indicating type
(\`*' for binary, \` ' for text), and name for each FILE.

Report bugs to <[email protected]>.
EOT
                ;;
        (version)
                cat <<EOT
${me} (HLFS) 1.20060917

Written by Jan Dvorak for HLFS project.
You may redistribute copies of this script under the terms of
the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
EOT
                ;;
esac

# EOF
-- 
http://linuxfromscratch.org/mailman/listinfo/hlfs-dev
FAQ: http://www.linuxfromscratch.org/faq/
Unsubscribe: See the above information page

Reply via email to