473,320 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Duplicate Microsoft FCIV MD5 file hash ?

I have a VB 5 module that duplicates the FCIV.exe from Microsoft. I need to
move an application forward to C#, but the samples for MD5 hash using the
framework I tried gave different hashes. What do I feed the framework and
how do I get the same values ??

The following code DOES NOT GIVE THE FCIV 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[i]);
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[i].ToString("X2"));
}

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

}


--
Andrew
Jun 24 '06 #1
5 3468
andrewcw <an******@acw.com> wrote:
I have a VB 5 module that duplicates the FCIV.exe from Microsoft. I need to
move an application forward to C#, but the samples for MD5 hash using the
framework I tried gave different hashes. What do I feed the framework and
how do I get the same values ??


You're producing two hashes from the same string, but you're encoding
the string in a different way. (In fact, in one case you're not
encoding it at all - you're just getting the hash of an array of 0s.)
Why would you expect that to give the same result?

It's not at all clear why you're trying to get a hashcode of a
representation of the length of the file though - for one thing, that's
bound to be precisely representable in less data than the hash itself.

Are you aware that neither of your hash mechanisms is actually looking
at the *contents& of the file?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 24 '06 #2
Jon,

Thanks for pointing out my misunderstanding. Does anyone know how the hash
would be created as MS does in FCIV ? The VB code does but exactly what it
is doing and how that maps to the .NET frameworks - I dont know. Any idea ??
--
Andrew
"Jon Skeet [C# MVP]" wrote:
andrewcw <an******@acw.com> wrote:
I have a VB 5 module that duplicates the FCIV.exe from Microsoft. I need to
move an application forward to C#, but the samples for MD5 hash using the
framework I tried gave different hashes. What do I feed the framework and
how do I get the same values ??


You're producing two hashes from the same string, but you're encoding
the string in a different way. (In fact, in one case you're not
encoding it at all - you're just getting the hash of an array of 0s.)
Why would you expect that to give the same result?

It's not at all clear why you're trying to get a hashcode of a
representation of the length of the file though - for one thing, that's
bound to be precisely representable in less data than the hash itself.

Are you aware that neither of your hash mechanisms is actually looking
at the *contents& of the file?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jun 24 '06 #3
Jon,

Here's how I re-wrote the code based on my limited understanding - but it
still is different - ideas ?? It seems odd to me that the encoding wants a
char array, so I go through some contortions to the file read as a char
array. I am also uneasy that when I read, I am coercing the value to be an
integer ( the file length ).

public string getFileHash(string filePath)
{
string retVal = "";
try
{
FileInfo fi = new FileInfo(filePath);
long fileLength = fi.Length;
char[] chars = new char[fileLength * 2];
TextReader streamReader = new StreamReader(filePath);
streamReader.Read(chars, 0, (int)fileLength);
MD5 md5 = MD5CryptoServiceProvider.Create();
//Encoding.Default.GetBytes(chars);
byte[] dataMd5 =
md5.ComputeHash(Encoding.Default.GetBytes(chars));
StringBuilder sb = new StringBuilder();
for (int i = 0; i < dataMd5.Length; i++)
sb.AppendFormat("{0:x2}", dataMd5[i]);
return sb.ToString();
retVal = Md5Hash(filePath);
}
--
Andrew
"Jon Skeet [C# MVP]" wrote:
andrewcw <an******@acw.com> wrote:
I have a VB 5 module that duplicates the FCIV.exe from Microsoft. I need to
move an application forward to C#, but the samples for MD5 hash using the
framework I tried gave different hashes. What do I feed the framework and
how do I get the same values ??


You're producing two hashes from the same string, but you're encoding
the string in a different way. (In fact, in one case you're not
encoding it at all - you're just getting the hash of an array of 0s.)
Why would you expect that to give the same result?

It's not at all clear why you're trying to get a hashcode of a
representation of the length of the file though - for one thing, that's
bound to be precisely representable in less data than the hash itself.

Are you aware that neither of your hash mechanisms is actually looking
at the *contents& of the file?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jun 25 '06 #4
I guess that was a good enough shove to get me thinking - here's the code
that duplicate the FCIV program for binary file ( too ).

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[i]);
retVal= sb.ToString();

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

}

--
Andrew
"andrewcw" wrote:
Jon,

Here's how I re-wrote the code based on my limited understanding - but it
still is different - ideas ?? It seems odd to me that the encoding wants a
char array, so I go through some contortions to the file read as a char
array. I am also uneasy that when I read, I am coercing the value to be an
integer ( the file length ).

public string getFileHash(string filePath)
{
string retVal = "";
try
{
FileInfo fi = new FileInfo(filePath);
long fileLength = fi.Length;
char[] chars = new char[fileLength * 2];
TextReader streamReader = new StreamReader(filePath);
streamReader.Read(chars, 0, (int)fileLength);
MD5 md5 = MD5CryptoServiceProvider.Create();
//Encoding.Default.GetBytes(chars);
byte[] dataMd5 =
md5.ComputeHash(Encoding.Default.GetBytes(chars));
StringBuilder sb = new StringBuilder();
for (int i = 0; i < dataMd5.Length; i++)
sb.AppendFormat("{0:x2}", dataMd5[i]);
return sb.ToString();
retVal = Md5Hash(filePath);
}
--
Andrew
"Jon Skeet [C# MVP]" wrote:
andrewcw <an******@acw.com> wrote:
I have a VB 5 module that duplicates the FCIV.exe from Microsoft. I need to
move an application forward to C#, but the samples for MD5 hash using the
framework I tried gave different hashes. What do I feed the framework and
how do I get the same values ??


You're producing two hashes from the same string, but you're encoding
the string in a different way. (In fact, in one case you're not
encoding it at all - you're just getting the hash of an array of 0s.)
Why would you expect that to give the same result?

It's not at all clear why you're trying to get a hashcode of a
representation of the length of the file though - for one thing, that's
bound to be precisely representable in less data than the hash itself.

Are you aware that neither of your hash mechanisms is actually looking
at the *contents& of the file?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jun 25 '06 #5
andrewcw <an******@acw.com> wrote:
I guess that was a good enough shove to get me thinking - here's the code
that duplicate the FCIV program for binary file ( too ).

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);


Once you've done that, just use md5.ComputeHash (fs);
There's no need for you to read all the data in yourself.

Note that you should use a "using" statement for your stream to make
sure it gets closed even if there's an exception.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 25 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Lowell Kirsh | last post by:
I have a script which I use to find all duplicates of files within a given directory and all its subdirectories. It seems like it's longer than it needs to be but I can't figure out how to shorten...
44
by: Xah Lee | last post by:
here's a large exercise that uses what we built before. suppose you have tens of thousands of files in various directories. Some of these files are identical, but you don't know which ones are...
11
by: google_groups3 | last post by:
Hi all. I currently have 2 text files which contain lists of file names. These text files are updated by my code. What I want to do is be able to merge these text files discarding the...
3
by: andrewcw | last post by:
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...
1
by: John Wright | last post by:
I want to create a file hash on my exe files and store the hash signature in a database so I can retrieve the value to compare the file hash to ensure I have an untampered file. How can this be...
9
by: Tom_F | last post by:
To comp.databases.ms-access -- I just discovered, to my more than mild dismay, that some tables in my Microsoft Access 2003 database have duplicate numbers in the "AutoNumber" field. (Field...
1
by: st12iker | last post by:
I managed to write a file to a hash. However the file contains multiple keys and value in the format listed. Note how the values are sorted in descending order. i.e. of file written to hash ...
1
by: Abandoned | last post by:
Hi.. I'm working a search engine project now. And i have a problem. My problem is Duplicate Contents.. I can find the percentage of similarity between two pages but i have a 5 millions index and...
9
by: DotNetNewbie | last post by:
Hello, I need a simple hash algorithm that will detect duplicate content in my application. I want to hash not just the content, but a few other parameters also like EmployeeID and...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.