What's new

Cryptographic hash script

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

smasher

Regular Contributor
I got tired of typing "openssl sha1" every time I needed to hash a file, so I wrote a script to hash files and stdin using the following hashes: sha1, sha224, sha256, sha384, sha512, sha, ripemd160, md5, md4, whirlpool

Here's the script:
Code:
#!/bin/sh

case $( basename ${0} ) in
    sha1|sha1sum)
        hash=sha1
        ;;
    sha224|sha224sum)
        hash=sha224
        ;;
    sha256|sha256sum)
        hash=sha256
        ;;
    sha384|sha384sum)
        hash=sha384
        ;;
    sha512|sha512sum)
        hash=sha512
        ;;
    sha|shasum)
        hash=sha
        ;;
    ripemd160)
        hash=ripemd160
        ;;
    ripemd)
        hash=ripemd
        ;;
    md5|md5sum)
        hash=md5
        ;;
    md4|md4sum)
        hash=md4
        ;;
    whirlpool)
        hash=whirlpool
        ;;
    *)
        hash=sha1
        ;;
esac

unset fault_status

for n in "${@}"
do
    ## test the file exists
    [ -e "${n}" ] || {
        echo "${n}: No such file or directory" 1>&2
        fault_status=1
        continue
    }
    ## test the file can be read
    [ -r "${n}" ] || {
        echo "${n}: Permission denied" 1>&2
        fault_status=1
        continue
    }
    openssl_hash=$( openssl ${hash} "${n}" )
    echo -n ${openssl_hash##*?=}'  '
    echo ${openssl_hash%)=?*} | cut -d'(' -f2-
done

## test if there's input being piped in
[ -t 0 ] || {
        openssl_hash=$( openssl ${hash} )
        echo ${openssl_hash##*?=}'  -'
}

## non-zero exit status indicates one or more files could not be read
## this test lets the script exit with the proper return status
[ 0 -eq ${fault_status:=0} ]
Install that as "/jffs/bin/sha1", then create symlinks to it:
Code:
ln -vs /jffs/bin/sha1 /jffs/bin/md4
ln -vs /jffs/bin/sha1 /jffs/bin/md4sum
ln -vs /jffs/bin/sha1 /jffs/bin/md5
ln -vs /jffs/bin/sha1 /jffs/bin/md5sum
ln -vs /jffs/bin/sha1 /jffs/bin/ripemd
ln -vs /jffs/bin/sha1 /jffs/bin/ripemd160
ln -vs /jffs/bin/sha1 /jffs/bin/sha1sum
ln -vs /jffs/bin/sha1 /jffs/bin/sha224
ln -vs /jffs/bin/sha1 /jffs/bin/sha224sum
ln -vs /jffs/bin/sha1 /jffs/bin/sha256
ln -vs /jffs/bin/sha1 /jffs/bin/sha256sum
ln -vs /jffs/bin/sha1 /jffs/bin/sha384
ln -vs /jffs/bin/sha1 /jffs/bin/sha384sum
ln -vs /jffs/bin/sha1 /jffs/bin/sha512
ln -vs /jffs/bin/sha1 /jffs/bin/sha512sum
ln -vs /jffs/bin/sha1 /jffs/bin/sha
ln -vs /jffs/bin/sha1 /jffs/bin/shasum
ln -vs /jffs/bin/sha1 /jffs/bin/whirlpool
Now, running the script with any of those names will produce expected results.

For ease of use, set PATH to
Code:
PATH=${PATH}:/jffs/bin

Tested and working spectacularly on an AC68U.
 
Last edited:

Similar threads

Latest threads

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top