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