MD5/SHA1 Comparison : Automation Discussions

MD5/SHA1 Comparison

Experiences, small talk, and other automation gossip.

MD5/SHA1 Comparison

Postby Billfred » Fri Jun 24, 2011 11:42 am

Hi,

Just started writing a few tests in Ranorex Studio 3 and am fairly new to this so do excuse me if this a stupid question.

The test I am writing involves comparing two files, one before upload and then the resulting file after it has been uploaded to a website. I thought the easiest way of doing this would be comparing the hash value of the two files. I can do this manually using Hashtab or something like that but there are many files that need to be compared and it would probably be a fairly long way of doing things.

Is there a simple way of comparing the hash values in Ranorex or is it easier just to compare the file sizes instead?

Any suggestions would be great, thanks.
Billfred
 
Posts: 1
Joined: Fri Jun 24, 2011 11:36 am

Re: MD5/SHA1 Comparison

Postby Ciege » Fri Jun 24, 2011 5:07 pm

I've not used HashTab or done what you are asking but what comes to mind is to just make a single method that does the hash check for you and call it from within a loop of all your files you want to check.

I would suggest checking to see if HashTab has an open API you can call or even a command line interface. If not, search for another utility that does.

Then, get a List of all your files you want to check, run them through a foreach() loop and pass each one, in turn, to your HashCheck method.

It sounds pretty easy to do and a great use for an automated function (although it does not necessarily need to be using an automated test utility as you can do all of that through standard development).
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...
User avatar
Ciege
 
Posts: 965
Joined: Thu Oct 16, 2008 7:46 pm
Location: Arizona, USA

Re: MD5/SHA1 Comparison

Postby sdaly » Sat Jun 25, 2011 2:14 pm

Depends on what you are trying to compare? If you just want to know the file has changed then comparing a hash of the file before and after would be an easy way. Here is a simple function to do so, just put this in a user code module. I use SHA1 over MD5 as it is faster to compute.

Code: Select all
   
using System.IO;
using System.Security.Cryptography;

public string CalculateSHA1Hash(string filePath)
   {
      try {
         SHA1CryptoServiceProvider SHA1 = new SHA1CryptoServiceProvider();
         FileStream fs = new FileStream(filePath, FileMode.Open,FileAccess.Read, FileShare.ReadWrite);
         byte[] hashValue = SHA1.ComputeHash(fs);
         fs.Close();
         string strHashData = BitConverter.ToString(hashValue);
         strHashData = strHashData.Replace("-", "");
         return(strHashData);
      } catch (Exception) {
         return "Could not calculate";
      }
}

If you need to verify a value inside the file then you would be better reading the file into a string then doing something like

Code: Select all
Ranorex.Validate.IsTrue(File.ReadAllText(@"c:\test.txt).Contains("Expected changed value"), "Validating file contains value whatever");
User avatar
sdaly
 
Posts: 213
Joined: Mon May 10, 2010 12:04 pm
Location: Dundee, Scotland


Return to Automation Discussions

Who is online

Users browsing this forum: No registered users and 0 guests