I am trying to create a md5 string from the byte array of an image uploaded by users at my web application..
This is because i want the images spread out in different folders.
And I dont have to use the userID as the folder name. Looks more professional.
The result would be something like:
/images/ 'first-two-char-of-md5' / 'the-complete-md5-string'.[jpg,png,bmp....]
Does this sound as a good solution to handle the images?
So.
My code ( Stuff from the internet.):
protected void btnUpload_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
if (CheckFileType(FileUpload1.FileName))
{
const int BUFFER_SIZE = 255;
Byte[] Buffer = new Byte[BUFFER_SIZE];
Stream theStream = FileUpload1.PostedFile.InputStream;
nBytesRead = theStream.Read(Buffer, 0, BUFFER_SIZE);
System.Text.ASCIIEncoding ASCIIEncoding = new ASCIIEncoding();
System.Text.UTF8Encoding utf8 = new UTF8Encoding();
//Just trying some stuff to see the output...
Label1.Text = ASCIIEncoding.GetString(CalculateMD5(theStream)) + "<br>" + utf8.GetString(CalculateMD5(theStream)) + "<br>" + Convert.ToBase64String(CalculateMD5(theStream));
}
}
}
private static byte[] _emptyBuffer = new byte[0];
public static byte[] CalculateMD5(Stream stream)
{
return CalculateMD5(stream, 64 * 1024);
}
public static byte[] CalculateMD5(Stream stream, int bufferSize)
{
MD5 md5Hasher = MD5.Create();
byte[] buffer = new byte[bufferSize];
int readBytes;
while ((readBytes = stream.Read(buffer, 0, bufferSize)) > 0)
{
md5Hasher.TransformBlock(buffer, 0, readBytes, buffer, 0);
}
md5Hasher.TransformFinalBlock(_emptyBuffer, 0, 0);
return md5Hasher.Hash;
}
The result. I get some output from the “calculateMD5()” but when i am trying to put it to the label1. to see what is happening. there are just a bunch of wierd characters.
What am I doing wrong here? i want it to be htmlsafe… a-z, A-Z,0-9 only.
The hash is being returned as an array of bytes. You need to convert this to a human readable form, e.g.
73868cb1848a216984dca1b6b0ee37bc. You can use something like the following:This iterates through the list of bytes returned from the hashing operation and converts each byte to hex. You can find more information on the format strings you can use for the
bytetype on MSDN.To answer the first part of your question:
It should be sufficient, although if 2 users upload the same image then it will result in the same hash. You could try salting the data with the username and maybe a timestamp to mitigate this.
You would also need some checking, because even though the probability is extremely small, you may generate the same hash (a ‘collision’) for different images/users, and you wouldn’t want a user to overwrite another user’s image. You could prevent this by generating a hash of the image, checking if it already exists, and if it does, add some bytes to the pre-hashed data, repeating until the hash is unique.