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(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 3 2546
OK finally solved it :
This works for binary files:
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: 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(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
andrewcw wrote: OK finally solved it :
This works for binary files:
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;
}
Hi Andrew,
You could actually stream-line it, if you wanted:
public string GetFileHashBina ry ( 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.AppendFo rmat( "{0:x2}", b );
}
catch ( Exception e )
{
Console.WriteLi ne( e.Message );
return string.Empty;
}
return retVal.ToString ();
}
--
-- Tom Spink
Yes that is much cleaner - thank you !
--
Andrew
"Tom Spink" wrote: andrewcw wrote:
OK finally solved it :
This works for binary files:
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;
}
Hi Andrew,
You could actually stream-line it, if you wanted:
public string GetFileHashBina ry ( 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.AppendFo rmat( "{0:x2}", b ); } catch ( Exception e ) { Console.WriteLi ne( e.Message ); return string.Empty; }
return retVal.ToString (); }
-- -- Tom Spink This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Remon Huijts |
last post by:
Hi,
When I use php mail() in a script on my localhost to send an HTML
message to an e-mailaccount on my online host, all is fine.
When I use php mail() in a script on my online host to send an HTML
message to some e-mailaccount other than on my online host, all is fine.
But when I use php mail() in a script on my online host to send an HTML
message to an e-mailaccount on that same online host, I receive the
message code as plain text.
|
by: Uwe Mayer |
last post by:
Hi,
sorry for the lack of source code.
Here again:
I use struct.unpack() to unpack data from a binary file and pass the
returned tuple as parameter to __init__ of a class that's supposed to
handle the data:
class DataWrapper():
|
by: Megan |
last post by:
Hi Everybody-
I know that this is a really, really long post, but I wanted to try to
give you as much background as possible.
So here's a quick overview of the issues I'm asking for help with:
1.) I'm trying to create a many to many relationship, and I get the
following Error when I try to enforce referential integrity.
|
by: bq |
last post by:
In the code
int a;
int b = -1;
does ANSI C guarantee that "b" is located in memory right after "a"
so that "a" refers to "b"?
Thanks.
bq
|
by: Daniel Goldman |
last post by:
Hi,
Any advice about both a BinaryReader and BinaryWriter
containing same FileStream at same time? Like:
Stream fs = new FileStream("output.dbf", FileMode.Create);
BinaryReader br = new BinaryReader(fs);
BinaryWriter bw = new BinaryWriter(fs);
/* BinaryWriter writes, flushes, etc */
/* BinaryReader seeks, reads, etc */
| |
by: WayneD |
last post by:
Hi All,
Just got started in C#...
Here's some C# code:
public MyClass
{
private MyThingy m_Thingy;
|
by: Sharon |
last post by:
Hi all,
I've implemented a TCP server using the Socket async methods.
When connecting to the server from 3 instances of hyper terminal,
i've noticed that each of the newly created server sockets, uses the same
server port.
I assumed that a new connection will receive a unique port.
If this is the way its suppose to work, is it a performance issue?
Is it possible that connections from the same IP will connect on the same
server port?
I...
|
by: andrewcw |
last post by:
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 = "";
|
by: SzH |
last post by:
Suppose that there is a program that takes two files as its command
line arguments. Is there a (cross platform) way to decide whether the
two files are the same? Simple string comparison is not enough as the
two files might be specified as "file.txt" and "./file.txt", or one of
them may be a symlink to the other.
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |