How to get the same as MS FCIV

G

Guest

I have a VB 5 program that computes an MD5HASH on a file.
I can get the same number using Microsoft FCIV.

But this code does not. ( What more should I do to get the file's hash as
the legacy and MS FCIV tool ??

In my attempts at code I get the file length and then convert that to a
string which I pass to 2 methods to create a hash - each off the net, but
they are different and not the desired hash value.

public string getFileHash(string filePath)
{
string retVal = "";
// open file
try
{
FileInfo fi = new FileInfo(filePath);
long fileLength = fi.Length;
string fileString = Convert.ToString(fileLength);
retVal = Md5Hash(fileString); // method 1
string retVal2 = Md5Hash2(fileString); // method 2
retVal = retVal + " : " + retVal2;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
retVal = "";
}
return retVal;

}

public static string Md5Hash(string pass)
{
MD5 md5 = MD5CryptoServiceProvider.Create();
byte[] dataMd5 = md5.ComputeHash(Encoding.Default.GetBytes(pass));
StringBuilder sb = new StringBuilder();
for (int i = 0; i < dataMd5.Length; i++)
sb.AppendFormat("{0:x2}", dataMd5);
return sb.ToString();

}
public static string Md5Hash2(string str)
{

// Create a buffer large enough to hold the string
byte[] unicodeText = new byte[str.Length * 2];
Encoder enc = System.Text.Encoding.Unicode.GetEncoder();


// Now that we have a byte array we can ask the CSP to hash it
MD5 md5 = new MD5CryptoServiceProvider();
byte[] result = md5.ComputeHash(unicodeText);

// Build the final string by converting each byte
// into hex and appending it to a StringBuilder
StringBuilder sb = new StringBuilder();
for (int i = 0; i < result.Length; i++)
{
sb.Append(result.ToString("X2"));
}

// And return it
return sb.ToString();

}
 
G

Guest

OK finally solved it :

This works for binary files:

public string getFileHashBinary(string filePath)
{
string retVal = "";
try
{ // this works for binary files
FileInfo fi = new FileInfo(filePath);
long fileLength = fi.Length;
MD5 md5 = MD5CryptoServiceProvider.Create();
FileStream fs = new FileStream(filePath, FileMode.Open);
Byte[] bBuffer = new byte[fileLength];
int something= fs.Read(bBuffer, 0,(int)fileLength);
byte[] dataMd5 = md5.ComputeHash(bBuffer);
fs.Close();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < dataMd5.Length; i++)
sb.AppendFormat("{0:x2}", dataMd5);
retVal= sb.ToString();

}
catch (Exception e)
{
Console.WriteLine(e.Message);
retVal = "";
}
return retVal;

}
 
T

Tom Spink

andrewcw said:
OK finally solved it :

This works for binary files:

public string getFileHashBinary(string filePath)
{
string retVal = "";
try
{ // this works for binary files
FileInfo fi = new FileInfo(filePath);
long fileLength = fi.Length;
MD5 md5 = MD5CryptoServiceProvider.Create();
FileStream fs = new FileStream(filePath, FileMode.Open);
Byte[] bBuffer = new byte[fileLength];
int something= fs.Read(bBuffer, 0,(int)fileLength);
byte[] dataMd5 = md5.ComputeHash(bBuffer);
fs.Close();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < dataMd5.Length; i++)
sb.AppendFormat("{0:x2}", dataMd5);
retVal= sb.ToString();

}
catch (Exception e)
{
Console.WriteLine(e.Message);
retVal = "";
}
return retVal;

}


Hi Andrew,

You could actually stream-line it, if you wanted:

public string GetFileHashBinary ( string filePath )
{
StringBuilder retVal = new StringBuilder();

try
{
MD5 md5 = MD5.Create();

byte[] fileHash;
using ( FileStream fs = File.OpenRead( filePath ) )
fileHash = md5.ComputeHash( fs );

foreach ( byte b in fileHash )
retVal.AppendFormat( "{0:x2}", b );
}
catch ( Exception e )
{
Console.WriteLine( e.Message );
return string.Empty;
}

return retVal.ToString();
}
 
G

Guest

Yes that is much cleaner - thank you !
--
Andrew


Tom Spink said:
andrewcw said:
OK finally solved it :

This works for binary files:

public string getFileHashBinary(string filePath)
{
string retVal = "";
try
{ // this works for binary files
FileInfo fi = new FileInfo(filePath);
long fileLength = fi.Length;
MD5 md5 = MD5CryptoServiceProvider.Create();
FileStream fs = new FileStream(filePath, FileMode.Open);
Byte[] bBuffer = new byte[fileLength];
int something= fs.Read(bBuffer, 0,(int)fileLength);
byte[] dataMd5 = md5.ComputeHash(bBuffer);
fs.Close();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < dataMd5.Length; i++)
sb.AppendFormat("{0:x2}", dataMd5);
retVal= sb.ToString();

}
catch (Exception e)
{
Console.WriteLine(e.Message);
retVal = "";
}
return retVal;

}


Hi Andrew,

You could actually stream-line it, if you wanted:

public string GetFileHashBinary ( string filePath )
{
StringBuilder retVal = new StringBuilder();

try
{
MD5 md5 = MD5.Create();

byte[] fileHash;
using ( FileStream fs = File.OpenRead( filePath ) )
fileHash = md5.ComputeHash( fs );

foreach ( byte b in fileHash )
retVal.AppendFormat( "{0:x2}", b );
}
catch ( Exception e )
{
Console.WriteLine( e.Message );
return string.Empty;
}

return retVal.ToString();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top