Recent

Author Topic: MD5 doesn't like me  (Read 8618 times)

csyde

  • New member
  • *
  • Posts: 9
MD5 doesn't like me
« on: January 31, 2015, 10:33:56 pm »
I seem to be unable to create a working MD5 test. The MD5 functions in the snippet of code below keep returning the same garbage string no matter what input you feed them and I'm not really sure how to handle it short of rewriting it as an external program. (Also, should I be using SHA1 instead?)

Code: [Select]
{ takes a filename fn and a user-input MD5 hash phash as input, compares with an MD5 hash generated from fn. Not working on OS X Yosemite. }

function testMd5(fn, phash: string): boolean;
var lhash: string;

begin
lhash := MD5Print(MD5File(fn));
writeln(lhash);
if lhash = phash
then testMd5 := true
else testMd5 := false;
end;

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: MD5 doesn't like me
« Reply #1 on: February 01, 2015, 03:52:31 am »
Is it d41d8cd98f00b204e9800998ecf8427e? if yes then the file fn does *not* exist. Otherwise try -dMD5PASCAL
« Last Edit: February 01, 2015, 04:06:09 am by engkin »

ChrisF

  • Hero Member
  • *****
  • Posts: 542
Re: MD5 doesn't like me
« Reply #2 on: February 01, 2015, 04:19:49 am »
I agree with engkin: the value he has given is for an empty buffer/file, or for a file not found. You may eventually check the path of your file...

A very basic test program (as proposed at http://wiki.freepascal.org/hash) gives a correct value to me.

Code: [Select]
program TestMD5;

uses md5;

const MyFile = '.'+DirectorySeparator+'MyTestFile.txt';

begin
  WriteLn('MD5 Hash = '+MD5Print(MD5File(Myfile)));
end.

Quote from: Reference
Hashed File: MyTestFile.txt  (1483 bytes)

 CRC32 Value: 550D9B13
 MD5 Hash: 486FDE35C9910762F5D51C6687F26446
 SHA-1 Hash: 5881EE7FE9DAFAA165303C9370518E0BCF2770A5
 SHA-224 Hash: E1ED79275AACAD57DC9363789C8539EF875BB9BAC10E538D02584781
 SHA-256 Hash: BCC59DB88B22A027B50DDDE13CEDD9C05FCE778620353C3FE3BBB830FE431206
 SHA-384 Hash: E8F6F2DCE438D62A03B3F2271A21B8703CE1645A7DCC44DF05BF81949BB4D59D255D8387000A6CD5FF7B8C026964B5FF
 SHA-512 Hash: D5A40B59226804EA5540F8FC21161540C05E6D8D262CA4089CE602B3CFCEDE16718B2D8B89F29AE22EBE5EE0D762596932ECD1AF7DF8C8DA1F7C9D3CF8FA821A
 SHA-512/224 Hash: E514FF1C91A5D4D6A79AB49EC21ADC5A86B86D194B7C1DCA47C772DD
 SHA-512/256 Hash: 25B2CA0D742AEE7A7492F74E2AF3A754A523D2AF3E26C980228802D46DDB883A
 
 
 Hashed File: Empty.txt  (0 bytes)

 CRC32 Value: 00000000
 MD5 Hash: D41D8CD98F00B204E9800998ECF8427E
 SHA-1 Hash: DA39A3EE5E6B4B0D3255BFEF95601890AFD80709
 SHA-224 Hash: D14A028C2A3A2BC9476102BB288234C415A2B01F828EA62AC5B3E42F
 SHA-256 Hash: E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855
 SHA-384 Hash: 38B060A751AC96384CD9327EB1B1E36A21FDB71114BE07434C0CC7BF63F6E1DA274EDEBFE76F65FBD51AD2F14898B95B
 SHA-512 Hash: CF83E1357EEFB8BDF1542850D66D8007D620E4050B5715DC83F4A921D36CE9CE47D0D13C5D85F2B0FF8318D2877EEC2F63B931BD47417A81A538327AF927DA3E
 SHA-512/224 Hash: 6ED0DD02806FA89E25DE060C19D3AC86CABB87D6A0DDD05C333B84F4
 SHA-512/256 Hash: C672B8D1EF56ED28AB87C3622C5114069BDD3AD7B8F9737498D0C01ECEF0967A

Quote from: TestMD5
(MyTestFile.txt)
MD5 Hash = 486fde35c9910762f5d51c6687f26446

(Empty.txt)
MD5 Hash = d41d8cd98f00b204e9800998ecf8427e

« Last Edit: February 01, 2015, 04:23:58 am by ChrisF »

csyde

  • New member
  • *
  • Posts: 9
Re: MD5 doesn't like me
« Reply #3 on: February 01, 2015, 05:32:45 am »
Hm... looks like the MD5 function isn't my problem. Turns out what was happening is that I've been dragging files from the Finder into the terminal window, which puts spurious spaces in fn. Looks like sysutils.trim() is the fix.
« Last Edit: February 01, 2015, 05:42:32 am by csyde »

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: MD5 doesn't like me
« Reply #4 on: February 01, 2015, 03:38:49 pm »
D41D8CD98F00B204E9800998ECF8427E and DA39A3EE5E6B4B0D3255BFEF95601890AFD80709 are the default initialisation values returned for any zero byte data due to them being  Merkle–Damgård constructions. See http://en.wikipedia.org/wiki/Merkle%E2%80%93Damg%C3%A5rd_construction and http://superuser.com/questions/557925/how-can-zero-byte-files-generate-a-hash-value and http://sourceforge.net/projects/quickhash


csyde

  • New member
  • *
  • Posts: 9
Re: MD5 doesn't like me
« Reply #5 on: February 02, 2015, 12:33:44 am »
So is this a thing to file a bug report about? It seems weird that a library function isn't smart enough to strip extraneous whitespace from a pathname.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: MD5 doesn't like me
« Reply #6 on: February 02, 2015, 01:40:18 am »
So is this a thing to file a bug report about? It seems weird that a library function isn't smart enough to strip extraneous whitespace from a pathname.

I assume you are talking about MD5File. It is meant for speed, I guess. On the other hand, what added "extraneous whitespace" to the file name should be considered a problem that may need to be fixed. Care to show the smallest possible example project (source code only) that would re-produce the problem so other members can double check?  :)

csyde

  • New member
  • *
  • Posts: 9
Re: MD5 doesn't like me
« Reply #7 on: February 02, 2015, 02:03:42 am »
This is the full program. It's BSD-licensed, so I don't have a problem sharing it.

https://github.com/csyde/alyxproject/blob/master/lash.pas

The build environment is Mac OS X Yosemite. Comment out line 69 (that's where I added trim() ) and then run the program. When you get to the Validate item on the System menu (it's all text-mode menus, very early 80s), instead of entering the filename by hand or pasting it in, drag the file to be validated into the terminal window. I think the Finder pastes in a trailing space and CRLF or something along those lines, and that's what MD5File chokes on.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: MD5 doesn't like me
« Reply #8 on: February 02, 2015, 02:18:51 am »
Thanks for sharing!  Unfortunately I am a Windows person. It is up to other members with similar environment "Mac OS X Yosemite" to test.

I assume the Finder could be used to drag and drop more than one file. How would you know their number and the name of each file individually? Anyway, thanks again for sharing your code.

csyde

  • New member
  • *
  • Posts: 9
Re: MD5 doesn't like me
« Reply #9 on: February 02, 2015, 02:40:50 am »
On the whole, I kick a lot back to the shell. The MD5 part was a bit of an anomaly since I couldn't find an already-written program that would do the trick the way I wanted, so I went to the FPC library instead. In this particular case it doesn't really matter since you can't really test more than one file at once, although obviously my error handling leaves a lot to be desired.

I may break it out into a separate program anyway. I'm pretty sure I need to add SHA1 but I wasn't exactly sure how to do it without adding yet another level of menus.

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: MD5 doesn't like me
« Reply #10 on: February 03, 2015, 08:34:38 am »
See my project, http://SourceForge.net/projects/quickhash for cross platform examples

 

TinyPortal © 2005-2018