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