Getting the MD5 hash of an image file

S

sympatico

Hi, I am trying to compare 2 images to check if they are exactly identical
(in terms of data), I thought this would be quicker than analysing pixels of
the images. I have found lots of examples for encrypting text strings using
MD5 hash, but I cannot find a way to get this to work with images that exist
in picture boxes. I am trying to get a hash for the image file, but not
from the file but from the picture box as there is some editing of the image
being done prior to trying to find it's hash.
I would greatly appreciate it if someone could point me in the right
direction on how to do this or even perhaps just advice of which hashing
algorithm would be the fastest and still most reliable way to do this, if
not MD5.
 
D

Derek Harmon

sympatico said:
Hi, I am trying to compare 2 images to check if they are exactly identical
(in terms of data), I thought this would be quicker than analysing pixels
of
the images.

The hash function is going to look at every bit in the image, therefore it's
not
going to be any faster than an efficient pixel-by-pixel comparison. One
draw-
back is, while unlikely, it's possible two different images could have the
same
hash value (resulting in a false positive). There's less information
contained
in the hash value then there are in the pictures, after all.
I have found lots of examples for encrypting text strings using MD5 hash,
but I cannot find a way to get this to work with images that exist in
picture
boxes. I am trying to get a hash for the image file, but not from the
file but
from the picture box as there is some editing of the image being done
prior
to trying to find it's hash.

Here's some code that finds the MD5 digest of an Image from the PictureBox
(it stores it into a MemoryStream from the System.IO namespace, and then
from that you can get the Byte( ) buffer that MD5 or another CSP can work
with to produce it's hash).

- - - CompareImageDigests.vb (excerpt)
Imports System
Imports System.Drawing
Imports System.IO
Imports System.Security.Cryptography
' . . .
Private Sub CompareButton_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles CompareButton.Click

Dim LeftBitStream As MemoryStream = New MemoryStream( )
Dim RightBitStream As MemoryStream = New MemoryStream( )

Me.PictureBox1.Image.Save(LeftBitStream, _
System.Drawing.Imaging.ImageFormat.Bmp)
Me.PictureBox2.Image.Save(RightBitStream, _
System.Drawing.Imaging.ImageFormat.Bmp)

Dim LeftImageBytes() As Byte = LeftBitStream.GetBuffer( )
Dim RightImageBytes() As Byte = RightBitStream.GetBuffer( )

Dim MD5csp As New MD5CryptoServiceProvider( )

Dim LeftImageHash( ) As Byte = MD5csp.ComputeHash(LeftImageBytes)
Dim RightImageHash( ) As Byte = MD5csp.ComputeHash(RightImageBytes)

Dim N As Integer = 0
Dim Identical As Boolean = True
While (N < LeftImageHash.Length And Identical)
If (N < RightImageHash.Length) Then
If (LeftImageHash(N) = RightImageHash(N)) Then
Identical = Identical And True
Else
Identical = False
End If
Else
Identical = False
End If
N = N + 1
End While

If (Identical) Then
MessageBox.Show("Images are Identical.", "Image Comparison", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show("Images are Different.", "Image Comparison", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
End If

End Sub
' . . .
- - -

Of course, to be more reliable, you could dispense with the hashing al-
together and just use the byte-by-byte comparison While loop above
to compare the two buffer Byte( ) arrays of both images. There are
also a number of shortcuts that you can take right off the bat, such as
testing the length of the two Byte( ) arrays (if they have different lengths
than they must be different images, right?) and checking the horizontal
and vertical dimensions of both Images.


Derek Harmon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top