Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need MD5 hash for an in memory System.Drawing.Image

Tags:

c#

.net

image

hash

md5

Need MD5 hash for an in memory System.Drawing.Image

like image 685
Ronnie Overby Avatar asked Aug 04 '10 22:08

Ronnie Overby


People also ask

How do I find the MD5 hash of an image?

Type the following command: md5sum [type file name with extension here] [path of the file] -- NOTE: You can also drag the file to the terminal window instead of typing the full path. Hit the Enter key. You'll see the MD5 sum of the file.

What is the MD5 hash of the image file?

MD5 is a one-way hash algorithm as defined by RFC1321 and can be used to help determine the integrity of a file by providing a 128 bit digital signature. This digital signature is like a fingerprint for a file; changing just one single byte in a file will result in a different MD5 hash.

How do I create an MD5 hash?

An MD5 hash is created by taking a string of an any length and encoding it into a 128-bit fingerprint. Encoding the same string using the MD5 algorithm will always result in the same 128-bit hash output.

Does MD5 hash include metadata?

A hash value does not appear on the face of a document or file but is part of the file's metadata. MD5 and SHA are common types of hash values. Documents with matching hash values are exact duplicates.


1 Answers

Here is a basic snippet. See also @JaredReisinger 's comment for some questions.

using System.Security.Cryptography;
using System.Text;
using System.Drawing.Imaging;
// ...

// get the bytes from the image
byte[] bytes = null;
using( MemoryStream ms = new MemoryStream() )
{
    image.Save(ms,ImageFormat.Gif); // gif for example
    bytes =  ms.ToArray();
}

// hash the bytes
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] hash = md5.ComputeHash(bytes);

// make a hex string of the hash for display or whatever
StringBuilder sb = new StringBuilder();
foreach (byte b in hash)
{
   sb.Append(b.ToString("x2").ToLower());
} 
like image 51
µBio Avatar answered Sep 26 '22 22:09

µBio