Skip to content

Latest commit

 

History

History
104 lines (77 loc) · 2.3 KB

README.md

File metadata and controls

104 lines (77 loc) · 2.3 KB

README

Demonstrate how to id files using md5 and md5sum

TODO:

  • How to verify a download

Help

# md5 – calculate a message-digest fingerprint (checksum) for a file
md5

# bsd version
md5sum --version
md5sum --help

# from coreutils
gmd5sum --version
gmd5sum --help

Calculate hash only

# file type
file ./README.md  

# generate the md5
cat ./README.md | md5 

# trim out filenames
md5sum ./README.md | tr " " "\n" | head -n 1   

Verify md5

# generate random numbers
dd if=/dev/urandom of=random.bin bs=1024 count=10
md5sum ./random.bin > ./random.bin.md5
md5sum --check ./random.bin.md5 

# regenerate
dd if=/dev/urandom of=random.bin bs=1024 count=10
# check again
md5sum --check ./random.bin.md5 

Verify md5 (without .md5 file)

Simple little verification for a file against an md5 value.

function verify_md5() {
    : ${1?"${FUNCNAME[0]}(url) - missing url argument"}
    : ${2?"${FUNCNAME[0]}(url) - missing md5 argument"}

    local URL=$1
    local MD5=$2
    local FILENAME="./$(basename $URL)"
    curl -s -o $FILENAME $URL 
    local FILE_MD5=$(cat $FILENAME | md5)
    if [[ $FILE_MD5 == $MD5 ]]; then
        echo "SUCCESS: MD5 for $FILENAME matches $MD5"
    else
        >&2 echo "FAIL: MD5 for $FILENAME does not match $MD5 - actual $FILE_MD5"
    fi 
}

MD5="d04fba2d9245e661f245de0577f48a33"
URL=https://nchc.dl.sourceforge.net/project/sox/sox/14.4.2/sox-14.4.2.tar.gz 
verify_md5 "$URL" "$MD5"

MD5="00000000000000000000000000000000"
verify_md5 "$URL" "$MD5"

Create md5 values

find . -maxdepth 1 -type f -exec md5sum {} \; 

Generate json document for md5

TEMPFILE=$(mktemp)
find .. -maxdepth 2 -type f -exec md5sum {} \; > ${TEMPFILE}
echo '{ "files":[] }' > ./md5s.json
while IFS=" " read -r md5value filepath
do
    size=$(gstat --printf="%s" ${filepath})
    echo "md5value:${md5value} filepath:${filepath} size:${size}"

    jq --arg md5value "${md5value}" --arg size "${size}" --arg filepath "${filepath}" '.files += [{md5: $md5value, filepath: $filepath, size: $size}]' ./md5s.json > ./out_md5s.json
    mv ./out_md5s.json ./md5s.json
done < <(cat ${TEMPFILE})

Resources

  • Only get hash value using md5sum (without filename) here