.Net (C#) MD5 hash different on Windows 7 (x86) than other operating systems
-
9. března 2011 18:56
In .NET we have a function that accepts a pathway to an image and returns the MD5 Hash.
The .NET code works fine on all operating systems and returns the same md5 for the test image.
Calling this function from XP or Vista machine produces the same md5hash as running the .NET code alone would produce. However, when you run it on windows 7 (x86) the md5 hash is different.
Do anyone know why this is happening?using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.IO;
namespace MD5Hash
{
public class cHash
{
static MD5 md5 = new MD5CryptoServiceProvider();
public cHash()
{
}
public string MD5hash(string path)
{
string hexaHash = "";
foreach (byte b in md5.ComputeHash(BmpToBytes_Unsafe(new Bitmap(path))))
{
hexaHash += String.Format("{0:x2}", b);
}
using (StreamWriter osw = new StreamWriter(@"c:\md5hash.log", true))
{
osw.WriteLine(String.Format("{0} - {1}", System.DateTime.Now, hexaHash));
}
return hexaHash;
}
static private unsafe byte[] BmpToBytes_Unsafe(Bitmap bmp)
{
BitmapData bData = bmp.LockBits(new Rectangle(new Point(), bmp.Size),
ImageLockMode.ReadOnly,
PixelFormat.Format24bppRgb);
// number of bytes in the bitmap
int byteCount = bData.Stride * bmp.Height;
byte[] bmpBytes = new byte[byteCount];
// Copy the locked bytes from memory
Marshal.Copy(bData.Scan0, bmpBytes, 0, byteCount);
// don't forget to unlock the bitmap!!
bmp.UnlockBits(bData);
return bmpBytes;
}
}
}- Přesunutý Max Wang_Chinasoft 28. dubna 2011 17:32 forum consolidation (From:Developer Discussions)