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(str ing filePath)
{
string retVal = "";
// open file
try
{
FileInfo fi = new FileInfo(filePa th);
long fileLength = fi.Length;
string fileString = Convert.ToStrin g(fileLength);
retVal = Md5Hash(fileStr ing); // method 1
string retVal2 = Md5Hash2(fileSt ring); // method 2
retVal = retVal + " : " + retVal2;
}
catch (Exception e)
{
Console.WriteLi ne(e.Message);
retVal = "";
}
return retVal;
}
public static string Md5Hash(string pass)
{
MD5 md5 = MD5CryptoServic eProvider.Creat e();
byte[] dataMd5 = md5.ComputeHash (Encoding.Defau lt.GetBytes(pas s));
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.Enc oding.Unicode.G etEncoder();
// Now that we have a byte array we can ask the CSP to hash it
MD5 md5 = new MD5CryptoServic eProvider();
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(resul t[i].ToString("X2") );
}
// And return it
return sb.ToString();
}
--
Andrew 5 3516
andrewcw <an******@acw.c om> 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.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jon,
Thanks for pointing out my misunderstandin g. 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.c om> 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.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too
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(str ing filePath)
{
string retVal = "";
try
{
FileInfo fi = new FileInfo(filePa th);
long fileLength = fi.Length;
char[] chars = new char[fileLength * 2];
TextReader streamReader = new StreamReader(fi lePath);
streamReader.Re ad(chars, 0, (int)fileLength );
MD5 md5 = MD5CryptoServic eProvider.Creat e();
//Encoding.Defaul t.GetBytes(char s);
byte[] dataMd5 =
md5.ComputeHash (Encoding.Defau lt.GetBytes(cha rs));
StringBuilder sb = new StringBuilder() ;
for (int i = 0; i < dataMd5.Length; i++)
sb.AppendFormat ("{0:x2}", dataMd5[i]);
return sb.ToString();
retVal = Md5Hash(filePat h);
}
--
Andrew
"Jon Skeet [C# MVP]" wrote: andrewcw <an******@acw.c om> 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.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too
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 getFileHashBina ry(string filePath)
{
string retVal = "";
try
{ // this works for binary files
FileInfo fi = new FileInfo(filePa th);
long fileLength = fi.Length;
MD5 md5 = MD5CryptoServic eProvider.Creat e();
FileStream fs = new FileStream(file Path, FileMode.Open);
Byte[] bBuffer = new byte[fileLength];
int something= fs.Read(bBuffer , 0,(int)fileLeng th);
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.WriteLi ne(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(str ing filePath) { string retVal = ""; try { FileInfo fi = new FileInfo(filePa th); long fileLength = fi.Length; char[] chars = new char[fileLength * 2]; TextReader streamReader = new StreamReader(fi lePath); streamReader.Re ad(chars, 0, (int)fileLength ); MD5 md5 = MD5CryptoServic eProvider.Creat e(); //Encoding.Defaul t.GetBytes(char s); byte[] dataMd5 = md5.ComputeHash (Encoding.Defau lt.GetBytes(cha rs)); StringBuilder sb = new StringBuilder() ; for (int i = 0; i < dataMd5.Length; i++) sb.AppendFormat ("{0:x2}", dataMd5[i]); return sb.ToString(); retVal = Md5Hash(filePat h); }
-- Andrew
"Jon Skeet [C# MVP]" wrote:
andrewcw <an******@acw.c om> 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.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too
andrewcw <an******@acw.c om> 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 getFileHashBina ry(string filePath) { string retVal = ""; try { // this works for binary files FileInfo fi = new FileInfo(filePa th); long fileLength = fi.Length; MD5 md5 = MD5CryptoServic eProvider.Creat e(); FileStream fs = new FileStream(file Path, 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.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 it. Perhaps
there are some python features or libraries I'm not taking advantage of.
The way it works is that it puts references to all the files in a
dictionary with file size being the key. The dictionary can hold
multiple values per key....
|
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
identical with which. Write a program that prints out which file are
redundant copies.
Here's the spec.
--------------------------
The program is to be used on the command line. Its arguments are one or
|
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 duplicates.
And to make it harder (or not???!!) my criteria for defining the
duplicate is the left 15 (or so) characters of the file path.
|
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 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.
|
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 done in VB.NET 2005
john
| |
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 Size is set to "Long Integer", and New
Values is set to "Increment".) I know that an old version of the Jet
database engine can cause this problem, but my version of msjet40.dll
is 4.0.8618.0, which is supposedly bug-free in this respect. I am...
|
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
ASDF 5.908
ASDF 5.902
ASDF 5.123
QWER 3.098
QWER 3.023
|
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 i search 5 million page contents to find one
duplicate :(
I want to a idea for how can i find duplicate pages quickly and fast ?
Please help me, i'm sorry my bad english.
|
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 DepartmentID.
So something like:
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |