in reply to MD 5 hash comparison/checker

I have an MD5 hash perl tool. It currently allows the user to input two file paths and it then displays the md5 hash of each file.

Why would a user want to do that?

I was wondering if it's possible to tell the user if there are differences or if they are the same

Look up the unix/linux "cmp" and "diff" commands. They were designed to do just that.

Here's the code...

I'm always baffled when I see these SoPW posts where the code is set up to ask questions of the user and read the user's responses from STDIN. It's almost always true that the information being asked for could/should be provided as command line args, so that the script gets them from @ARGV instead of reading them from STDIN.

Once you understand that every CLI shell worth using keeps a command history, and interprets the "up-arrow" key as "go back to the previous command line", you'll see how much nicer it is to use command-line args to convey run-time instructions to your script (e.g. names of files, optional parameters, etc). If you are using a shell that doesn't support command history and recall, get another shell.

As for the actual task that you're trying to accomplish: if it involves looking for duplicate file content, build a table that has path/filename, file_byte_count and file_md5 for the files of interest, sort the table on md5 and file size, and just do a byte-for-byte comparison (à la 'diff', 'cmp' or ikegami's code snippet) on the files that have identical sizes and md5s. (It's entirely possible that two files of the same size may have the same md5 sig, despite having different content.)

Replies are listed 'Best First'.
Re^2: MD 5 hash comparison/checker
by daggy (Novice) on May 07, 2010 at 03:12 UTC

    HI,

    thanks for the reply...

    It's for an assignment, so it's more a hypothetical scenario, as opposed to a practical one.

    Do you know whay the perl module closes at the end of the code?

    It closes too soon, so I'm unable to actually read the results.

    Whereas if I run it through CMD it works fine.

      The console closes as soon as no program is running in it ...by default. You can configure the console to stay open.
        Or pause , like
        { print "\n\n press enter to finish ... "; scalar <STDIN>; }