LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-09-2011, 08:19 AM   #1
derchris
LQ Newbie
 
Registered: Feb 2011
Posts: 27

Rep: Reputation: 0
SHA1-Hashing differs when writing to a file


Hey guys,

I have a weird problem, it is about the program sha1sum to create SHA1-hashes.
As you probably know, SHA1-Hashes do have the length 20 byte. So when I just type:
Code:
sha1sum myfile
it produces an output of
Code:
(some20byte) myfile
just as it should. Now I want to store the 20byte hash in another file, I use this command:
Code:
sha1sum myfile | awk "{print \$1}" >> myhash
Unfortunately I'm not familiar with awk, but this should cut off the end of the sha1sum output, which is the name of the file again. The problem here is: The newly created file myhash has the size 41 bytes, and printing it out I can see that it is not the original hash (I wrote a little program to print it bytewise).
And I cannot understand why :/ Any ideas?
 
Old 03-09-2011, 08:26 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 678Reputation: 678Reputation: 678Reputation: 678Reputation: 678Reputation: 678
Code:
> sha1sum Chet\ Atkins\ _amp 
53b3d1019001b2403545836a26c91ad71e543085  Chet Atkins _amp
jschiwal@qosmio:~> sha1sum Chet\ Atkins\ _amp  | awk '{print $1}'
53b3d1019001b2403545836a26c91ad71e543085
I'm not seeing a difference in the hash.
 
Old 03-09-2011, 08:28 AM   #3
derchris
LQ Newbie
 
Registered: Feb 2011
Posts: 27

Original Poster
Rep: Reputation: 0
Yeah, up until this point there is none. But no try to write the hash to a file. The filesize is larger than 20bytes, what I cannot understand. Plus my little output script shows a difference then



FWIW. here's my outputscript:

Code:
int i;
FILE *fp = fopen(name, "r");
	if(fp == NULL) {
		printf("fopen");
		return -1;
	}
	while((i = getc(fp)) != EOF) {
		printf("0x%02X, ", (unsigned char) i );
	}

Last edited by derchris; 03-09-2011 at 08:30 AM.
 
Old 03-09-2011, 08:30 AM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 678Reputation: 678Reputation: 678Reputation: 678Reputation: 678Reputation: 678
What if you use > instead of >>. You are appending to the file, not creating a new file. Maybe there were characters in the file originally.

You can use "wc -c" to count the characters in a file or string. Or simply use "ls -l".
You can use the od command to print out hex characters for binary blobs.

Last edited by jschiwal; 03-09-2011 at 08:32 AM.
 
1 members found this post helpful.
Old 03-09-2011, 08:56 AM   #5
derchris
LQ Newbie
 
Registered: Feb 2011
Posts: 27

Original Poster
Rep: Reputation: 0
Ok, thank you, using od I can see that they are indeed the same. It looks like wc and the ls should not bytes but half bytes, that confused me
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Writing an ISO file for cd audio from a cue and wave file? spaz-atk Linux - Software 5 12-01-2008 02:52 AM
How come file size differs between ls and df? felixrabe Linux - General 6 01-06-2008 07:26 PM
File size differs procfs General 3 06-06-2006 10:14 AM
checking sha1 file trueromance Fedora - Installation 3 12-25-2005 12:17 PM
File opening/reading speed differs first time and after that Not now, John! Linux - General 2 08-05-2004 10:37 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:16 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