LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-14-2009, 09:55 AM   #1
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,695

Rep: Reputation: 56
About checking hash : sha1


Hi folks,


Debian 5.0

Please advise how to check "Hash: SHA1" of .iso image.

If running;

$ sha1sum /path/to/.iso

yields a completely different output


Google brought me following URF;

sha1 hash check free download
http://3d2f.com/tags/sha1/hash/check/download/

Which software shall I install?


B.R.
satimis
 
Old 12-14-2009, 06:22 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
A "completely different output" to what? The checksum provided at the download site? If that number and the one you get from sha1sum are different, then something has changed or corrupted the file. Assuming both numbers are the same kind of checksum, of course--they should both have the same number of digits.

A while back I wrote a script that will compare the checksums of either two files, or a file and a pre-calculated string. Maybe you'll find it useful.

Code:
#!/bin/bash
# comparisum--a script for comparing checksums.


checktype="sha1sum"	#Sets the default check type.  String is the name
			#of the checksum program you want it to use.

EX_HELPOUT=79		#User-defined exit.  Successful output of usage information.

EX_USAGE=64		#Standard C exit code for bad arguments.
			#See /usr/include/sysexits.h

IFS='
'

BCYAN='\e[1;36m'	#Define some colors.
BGREEN='\e[1;32m'
BRED='\e[1;31m'
BBLUE='\e[1;34m'
BMAGENTA='\e[1;35m'
RESET='\e[0m' #disable any colors



## function that outputs usage info ##
function ShowHelp(){

if [ "$1" = "short" ]; then

   cat <<FOOBAR

        Usage:  ${0##*/} [-t <type>] <file1|checksum1> <file2|checksum2>
	        For detailed help use: ${0##*/} -h

FOOBAR

elif [ "$1" = "long" ]; then

   cat <<FOOBAR

        Usage:  ${0##*/} [-t <type>] <file1|checksum1> <file2|checksum2>
		${0##*/} -h

	     -h       : Display this help message and exit.

	     -t <type>: Use checksum <type> instead of the default.
	     		(Only works with file-to-file comparisons.)
			Valid types: md5 sha1 sha224 sha256 sha384 sha512

	This script will compare the sha1, sha2, or md5 checksums of
	two files, one file and a previously-generated checksum, or two
	checksums.
	
	If two filenames are given it will calculate and compare their
	checksums.  It uses sha1 by default (this can be changed in the script).
	You can tell it to use a different checksum type with the -t option.
	If multiple types are given, only the last one will be used.

	The -t option is ignored when comparing a file to a checksum.  The 
	type of	checksum to use is instead automatically determined by the
	length of the string given.
	
	If neither argument is a file, then it will compare the two entries
	as literal text strings (may be useful for comparing long checksums).
	In this case the strings can be of any length.

FOOBAR

fi

}


## function that calculates checksums for files ##
function Check_Sum(){

sum1=$($checktype "$1")
echo "${sum1%% *}"

}


## function that compares the checksums of two files ##
function Compare2Files(){

filename1=${1##*/}  #strip leading directories
filename2=${2##*/}  # for display purposes

filesum1=$(Check_Sum "$1")
filesum2=$(Check_Sum "$2")

echo	
echo -e "Comparing files ${BCYAN}$filename1${RESET} and ${BCYAN}$filename2${RESET}."
echo -e "Using ${BCYAN}$checktype${RESET} for comparison."
echo
echo -e "Sum of $filename1 is:\n${BBLUE}$filesum1${RESET}"
echo -e "Sum of $filename2 is:\n${BBLUE}$filesum2${RESET}"
echo
if [ "$filesum1" = "$filesum2" ]; then
   echo -e "${BGREEN}The two files match.${RESET}"
else
   echo -e "${BRED}The two files are different.${RESET}"
fi
}


## function that compares a file checksum to a given string ##
function CompareFile2Sum(){

if [ -f $1 ]; then	#determine the order of arguments
    file=$1
    filename=${1##*/}
    compstring=$2
else 
    file=$2
    filename=${2##*/}
    compstring=$1
fi

#Determine if $compstring is a valid checksum, and which type it is.
#First decide if it's hex, then test for length.

if [[ "$compstring" =~ [^0123456789abcdef] ]]; then
    echo
    echo -e "${BMAGENTA}String to compare is not a valid checksum.  Exiting.${RESET}" >&2
    ShowHelp short >&2
    exit $EX_USAGE
fi

case ${#compstring} in
    32) checktype=md5sum ;;
    40) checktype=sha1sum ;;
    56) checktype=sha224sum ;;
    64) checktype=sha256sum ;;
    96) checktype=sha384sum ;;
   128) checktype=sha512sum ;;
     *) echo
	echo -e "${BMAGENTA}String to compare is not a valid checksum.  Exiting.${RESET}" >&2
	ShowHelp short >&2
	exit $EX_USAGE ;;
esac

filesum=$(Check_Sum $file)

echo
echo -e "Comparing ${BCYAN}$filename${RESET} to the checksum given."
echo -e "Using ${BCYAN}$checktype${RESET} for comparison."
echo
echo -e "Sum of $filename is:\n${BBLUE}$filesum${RESET}"
echo -e "Sum to compare against is:\n${BBLUE}$compstring${RESET}"
echo
if [ "$filesum" = "$compstring" ]; then
   echo -e "${BGREEN}The two checksums match.${RESET}"
else
   echo -e "${BRED}The two checksums are different.${RESET}"
fi

}

## function that compares two strings ##
function Compare2Strings(){

echo
echo "Comparing text string one to text string two."
echo
echo -e "Text string one is:\n${BBLUE}$1${RESET}"
echo -e "Text string two is:\n${BBLUE}$2${RESET}"
echo
if [ "$1" = "$2" ]; then
   echo -e "${BGREEN}The two text strings match.${RESET}"
else
   echo -e "${BRED}The two text strings are different.${RESET}"
fi

}



###### Start main script operations ######
##########################################

## Process input options ##
while getopts ":ht:" option; do
  case $option in
    h) ShowHelp long >&2
       exit $EX_HELPOUT
     ;;
    t) case $OPTARG in
	  md5|sha1|sha224|sha256|sha384|sha512) checktype="${OPTARG}sum"
	  ;;
	  *) echo
	     echo -e "${BMAGENTA}Invalid type \"${BRED}$OPTARG${BMAGENTA}\".${RESET}" >&2
	     echo -e "${BMAGENTA}Valid types are ${BRED}md5 sha1 sha224 sha256 sha384 sha512${RESET}" >&2
	     ShowHelp short >&2
	     exit $EX_USAGE
	  ;;
       esac
     ;;
    \?) echo -e "${BMAGENTA}Invalid option \"${BRED}-$OPTARG${BMAGENTA}\".  Ignoring.${RESET}" >&2
     ;;
  esac
done

shift $((OPTIND-1)); OPTIND=1


## Analyze input arguments and call the appropriate functions ##
if [[ ! $1 || ! $2 || $3 ]]; then

	echo
	echo -e "${BMAGENTA}Invalid number of arguments.${RESET}" >&2
	ShowHelp short >&2
	exit $EX_USAGE

elif [[ -f  $1 && -f $2 ]]; then

	Compare2Files $1 $2

elif [[ ! -f  $1 && ! -f $2 ]]; then

	Compare2Strings $1 $2	     

elif [[ ! -f $1 || ! -f $2 ]]; then

	CompareFile2Sum $1 $2

fi

echo

exit 0
 
Old 12-14-2009, 09:06 PM   #3
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,695

Original Poster
Rep: Reputation: 56
Quote:
Originally Posted by David the H. View Post
A "completely different output" to what? The checksum provided at the download site? If that number and the one you get from sha1sum are different, then something has changed or corrupted the file. Assuming both numbers are the same kind of checksum, of course--they should both have the same number of digits.

A while back I wrote a script that will compare the checksums of either two files, or a file and a pre-calculated string. Maybe you'll find it useful.

Code:
#!/bin/bash
# comparisum--a script for comparing checksums.


checktype="sha1sum"	#Sets the default check type.  String is the name
			#of the checksum program you want it to use.

EX_HELPOUT=79		#User-defined exit.  Successful output of usage information.

EX_USAGE=64		#Standard C exit code for bad arguments.
			#See /usr/include/sysexits.h

IFS='
'

BCYAN='\e[1;36m'	#Define some colors.
BGREEN='\e[1;32m'
BRED='\e[1;31m'
BBLUE='\e[1;34m'
BMAGENTA='\e[1;35m'
RESET='\e[0m' #disable any colors



## function that outputs usage info ##
function ShowHelp(){

if [ "$1" = "short" ]; then

   cat <<FOOBAR

        Usage:  ${0##*/} [-t <type>] <file1|checksum1> <file2|checksum2>
	        For detailed help use: ${0##*/} -h

FOOBAR

elif [ "$1" = "long" ]; then

   cat <<FOOBAR

        Usage:  ${0##*/} [-t <type>] <file1|checksum1> <file2|checksum2>
		${0##*/} -h

	     -h       : Display this help message and exit.

	     -t <type>: Use checksum <type> instead of the default.
	     		(Only works with file-to-file comparisons.)
			Valid types: md5 sha1 sha224 sha256 sha384 sha512

	This script will compare the sha1, sha2, or md5 checksums of
	two files, one file and a previously-generated checksum, or two
	checksums.
	
	If two filenames are given it will calculate and compare their
	checksums.  It uses sha1 by default (this can be changed in the script).
	You can tell it to use a different checksum type with the -t option.
	If multiple types are given, only the last one will be used.

	The -t option is ignored when comparing a file to a checksum.  The 
	type of	checksum to use is instead automatically determined by the
	length of the string given.
	
	If neither argument is a file, then it will compare the two entries
	as literal text strings (may be useful for comparing long checksums).
	In this case the strings can be of any length.

FOOBAR

fi

}


## function that calculates checksums for files ##
function Check_Sum(){

sum1=$($checktype "$1")
echo "${sum1%% *}"

}


## function that compares the checksums of two files ##
function Compare2Files(){

filename1=${1##*/}  #strip leading directories
filename2=${2##*/}  # for display purposes

filesum1=$(Check_Sum "$1")
filesum2=$(Check_Sum "$2")

echo	
echo -e "Comparing files ${BCYAN}$filename1${RESET} and ${BCYAN}$filename2${RESET}."
echo -e "Using ${BCYAN}$checktype${RESET} for comparison."
echo
echo -e "Sum of $filename1 is:\n${BBLUE}$filesum1${RESET}"
echo -e "Sum of $filename2 is:\n${BBLUE}$filesum2${RESET}"
echo
if [ "$filesum1" = "$filesum2" ]; then
   echo -e "${BGREEN}The two files match.${RESET}"
else
   echo -e "${BRED}The two files are different.${RESET}"
fi
}


## function that compares a file checksum to a given string ##
function CompareFile2Sum(){

if [ -f $1 ]; then	#determine the order of arguments
    file=$1
    filename=${1##*/}
    compstring=$2
else 
    file=$2
    filename=${2##*/}
    compstring=$1
fi

#Determine if $compstring is a valid checksum, and which type it is.
#First decide if it's hex, then test for length.

if [[ "$compstring" =~ [^0123456789abcdef] ]]; then
    echo
    echo -e "${BMAGENTA}String to compare is not a valid checksum.  Exiting.${RESET}" >&2
    ShowHelp short >&2
    exit $EX_USAGE
fi

case ${#compstring} in
    32) checktype=md5sum ;;
    40) checktype=sha1sum ;;
    56) checktype=sha224sum ;;
    64) checktype=sha256sum ;;
    96) checktype=sha384sum ;;
   128) checktype=sha512sum ;;
     *) echo
	echo -e "${BMAGENTA}String to compare is not a valid checksum.  Exiting.${RESET}" >&2
	ShowHelp short >&2
	exit $EX_USAGE ;;
esac

filesum=$(Check_Sum $file)

echo
echo -e "Comparing ${BCYAN}$filename${RESET} to the checksum given."
echo -e "Using ${BCYAN}$checktype${RESET} for comparison."
echo
echo -e "Sum of $filename is:\n${BBLUE}$filesum${RESET}"
echo -e "Sum to compare against is:\n${BBLUE}$compstring${RESET}"
echo
if [ "$filesum" = "$compstring" ]; then
   echo -e "${BGREEN}The two checksums match.${RESET}"
else
   echo -e "${BRED}The two checksums are different.${RESET}"
fi

}

## function that compares two strings ##
function Compare2Strings(){

echo
echo "Comparing text string one to text string two."
echo
echo -e "Text string one is:\n${BBLUE}$1${RESET}"
echo -e "Text string two is:\n${BBLUE}$2${RESET}"
echo
if [ "$1" = "$2" ]; then
   echo -e "${BGREEN}The two text strings match.${RESET}"
else
   echo -e "${BRED}The two text strings are different.${RESET}"
fi

}



###### Start main script operations ######
##########################################

## Process input options ##
while getopts ":ht:" option; do
  case $option in
    h) ShowHelp long >&2
       exit $EX_HELPOUT
     ;;
    t) case $OPTARG in
	  md5|sha1|sha224|sha256|sha384|sha512) checktype="${OPTARG}sum"
	  ;;
	  *) echo
	     echo -e "${BMAGENTA}Invalid type \"${BRED}$OPTARG${BMAGENTA}\".${RESET}" >&2
	     echo -e "${BMAGENTA}Valid types are ${BRED}md5 sha1 sha224 sha256 sha384 sha512${RESET}" >&2
	     ShowHelp short >&2
	     exit $EX_USAGE
	  ;;
       esac
     ;;
    \?) echo -e "${BMAGENTA}Invalid option \"${BRED}-$OPTARG${BMAGENTA}\".  Ignoring.${RESET}" >&2
     ;;
  esac
done

shift $((OPTIND-1)); OPTIND=1


## Analyze input arguments and call the appropriate functions ##
if [[ ! $1 || ! $2 || $3 ]]; then

	echo
	echo -e "${BMAGENTA}Invalid number of arguments.${RESET}" >&2
	ShowHelp short >&2
	exit $EX_USAGE

elif [[ -f  $1 && -f $2 ]]; then

	Compare2Files $1 $2

elif [[ ! -f  $1 && ! -f $2 ]]; then

	Compare2Strings $1 $2	     

elif [[ ! -f $1 || ! -f $2 ]]; then

	CompareFile2Sum $1 $2

fi

echo

exit 0
Hi David,

Thanks for your advice.

I solved my problem running;
Code:
$ sha256sum /path/to/.iso
The printout is exactly the same as the hash indicated on that website.

B.R.
satimis
 
Old 12-15-2009, 01:04 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Glad you figured it out. I kind of suspected you were using the wrong kind of checksum.

Give my script a try though. Simply give it both the number and the filename, and it will automatically detect which type needs to be run when it compares them.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Using hash value as key for other hash in Perl scuzzman Programming 6 02-14-2006 05:08 PM
password hash storage (md5, sha1...) aneroid Programming 6 12-30-2005 10:27 PM
checking sha1 file trueromance Fedora - Installation 3 12-25-2005 11:17 AM
checking MD5 hash issey Linux - Newbie 4 03-31-2005 01:15 AM
Getting SHA1... Red Guy Linux - Software 0 07-22-2003 10:16 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration