Register
It is currently Sat Oct 25, 2014 7:26 am

md5 for strings


All times are UTC - 6 hours


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Tue Sep 21, 2010 9:39 am   

Joined: Tue Sep 21, 2010 9:29 am
Posts: 3
Hi :),

I'm a novice.

I want to make a script which calculate, for a string given, the hash MD5 of it.

My method is :
1/ I create a file which contains the string
2/ I apply md5sum on it
3/ I print the result

It works, but the hash isn't correct, so, can you help me to find where is the problem, please ?

Code:
#!/bin/bash

if [ "$#" -lt "1" ]
then
  echo "Syntaxe : mymd5sum -s chaine"
  exit -1
  if [ "$#" -eq "1" ] && [ $1 = "-s" ] ; then
     # eq à mymd5sum -s (chaine vide)
     `touch tmp_file_md5`
     md5=`md5sum tmp_file_md5 | cut -c1-32`
     echo "Le MD5 de \"\" donne : $md5"
     `rm -f tmp_file_md5`
  else
    echo "Syntaxe : mymd5sum -s chaine"
    exit -1
  fi
else if [ $1 = "-s" ] ; then
     `echo $2>tmp_file_md5`
     md5=`md5sum tmp_file_md5 | cut -c1-32`
     echo "Le MD5 de \"$2\" donne : $md5"
     `rm -f tmp_file_md5`
   else
     echo "Syntaxe : mymd5sum -s chaine"
   fi
fi
exit 0


(Yes, i'm not english and sorry for my language level :/ )


Top
 Profile  
 PostPosted: Tue Sep 21, 2010 12:21 pm   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
Why so complicated and not as simple as this?
Code:
echo "Hello world" | md5sum


Top
 Profile  
 PostPosted: Wed Sep 22, 2010 2:04 am   

Joined: Tue Sep 21, 2010 9:29 am
Posts: 3
Yes, but...

The real MD5 of "Hello World" is : b10a8db164e0754105b7a99be72e3fe5
Tests on http://www.md5.fr/ and verification with the RFC of MD5

The MD5 obtained with md5.c compiled linux of "Hello World" is :
Code:
./md5 -s"Hello World"
MD5 ("Hello World") = f2877a72c4494318c4b5bb436c582

The MD5 got with your command is :
Code:
echo "Hello world" | md5sum
f0ef7081e1539ac00ef5b761b4fb01b3  -


The MD5 of "Hello World" with my script :
Code:
./mymd5sum.sh -s "Hello World"
Le MD5 de "Hello World" donne : e59ff97941044f85df5297e1c302d260


So, there is a problem. I know that md5.c doesn't answer the same thing with my linux, cause of the endianess. But, with your command, I should find at less one of the two posible correct MD5 :/ (Note that my command is also wrong and different...).

(My real probleme is : I want to find the correct MD5, like it is in the RFC. I try by compiling the source in C, but the endianness of my system blocks me. So, I want to try by using md5sum, because md5sum is not affect by the endianess of any PC, but I but for now I can't.)

Thanks for your help :)

Thenkior


Top
 Profile  
 PostPosted: Wed Sep 22, 2010 2:52 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
With md5 the string must be EXACT, Patsie's example string was not exactly the same. When I run the same with the correct string I get the same result as you do, HOWEVER echo includes a return character so needs -n...

Code:
~]# echo -n "Hello World" | md5sum
b10a8db164e0754105b7a99be72e3fe5  -


OH look!! the same as the RFC
:D


Top
 Profile  
 PostPosted: Wed Sep 22, 2010 3:34 am   

Joined: Tue Sep 21, 2010 9:29 am
Posts: 3
Ok, thanks and gg :D

Merci beaucoup, comme on dit ici :)


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP