Different images gives same MD5 - why? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Albireo
Posts: 1756
Joined: 16 Oct 2013, 13:53

Different images gives same MD5 - why?

07 May 2021, 17:24

From a PDF file, 3 different images are identified and created. (in this case logo, text, and ...)
One image is in jpg format and the two other is in ppm format.
the two ppm files is converted to jpg (with irfanView cmd)
Then the MD5-values is read on this three files - but all three got the same MD5-value.
how? / why?

Code: Select all

	Loop Files, % temp.dir "\" tmpImage.name "-" temp.allJpg, F
	{	tmpImage["name" A_Index] := A_LoopFileName
		tmpImage["md5_" A_Index] := Calc_MD5(A_LoopFileName)
		MsgBox ,, %A_ScriptName% - Rad %A_LineNumber%, % A_LoopFileName "`n" Calc_MD5(A_LoopFileName)
	}
function Calc_MD5
User avatar
Smile_
Posts: 858
Joined: 03 May 2020, 00:51

Re: Different images gives same MD5 - why?

07 May 2021, 18:25

Is there a possibility to share these images?
teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: Different images gives same MD5 - why?

07 May 2021, 21:18

The function is wrong, check here.
Perhaps this one is correct:

Code: Select all

filePath := "D:\Images\MyPicture.png"
File := FileOpen(filePath, "r")
File.Pos := 0
File.RawRead(buff, len := File.Length)
File.Close()

MsgBox, % Calc_MD5(&buff, len)

Calc_MD5(pData, size)
{
   VarSetCapacity(MD5_CTX, 104, 0)
   DllCall("advapi32\MD5Init", "Ptr", &MD5_CTX)
   DllCall("advapi32\MD5Update", "Ptr", &MD5_CTX, "Ptr", pData, "UInt", size)
   DllCall("advapi32\MD5Final", "Ptr", &MD5_CTX)
   loop 16
      md5 .= Format("{:02x}", *(&MD5_CTX + 87 + A_Index))
   Return md5
}
Albireo
Posts: 1756
Joined: 16 Oct 2013, 13:53

Re: Different images gives same MD5 - why?

08 May 2021, 02:16

Thank you!
I don't know how to handle buff / &buff - what happens?

Is it possible to put open / close the file in the function() Calc_MD5? (like this)

Code: Select all

Calc_MD5(file)
{	File := FileOpen(file, "r")
	File.Pos := 0
	File.RawRead(buff, len := File.Length)
	File.Close()

   VarSetCapacity(MD5_CTX, 104, 0)
   DllCall("advapi32\MD5Init", "Ptr", &MD5_CTX)
   DllCall("advapi32\MD5Update", "Ptr", &MD5_CTX, "Ptr", pData, "UInt", size)
   DllCall("advapi32\MD5Final", "Ptr", &MD5_CTX)
   loop 16
      md5 .= Format("{:02x}", *(&MD5_CTX + 87 + A_Index))
   Return md5
}
( the use of Calc_MD5 can be performed with only one instruction )
teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: Different images gives same MD5 - why?  Topic is solved

08 May 2021, 05:48

Code: Select all

Calc_MD5(filePath)
{	
   File := FileOpen(filePath, "r")
   File.Pos := 0
   File.RawRead(buff, len := File.Length)
   File.Close()

   VarSetCapacity(MD5_CTX, 104, 0)
   DllCall("advapi32\MD5Init", "Ptr", &MD5_CTX)
   DllCall("advapi32\MD5Update", "Ptr", &MD5_CTX, "Ptr", &buff, "UInt", len)
   DllCall("advapi32\MD5Final", "Ptr", &MD5_CTX)
   loop 16
      md5 .= Format("{:02x}", *(&MD5_CTX + 87 + A_Index))
   Return md5
}
Albireo
Posts: 1756
Joined: 16 Oct 2013, 13:53

Re: Different images gives same MD5 - why?

08 May 2021, 06:45

Yes! :dance:
But how can I get a MD5-value if the image is missing?
Now it always seems to be d41d8cd98f00b204e9800998ecf8427e When the image is missing.
Would have been better if the MD5 value became 0 or nothing.

Maybe like this

Code: Select all

Calc_MD5(filePath)
{	; Version 8 maj 2021
	; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=90278&p=398512#p398512
	; By teadrinker
   If !FileExist(filepath)
		Return "0"
		
	File := FileOpen(filePath, "r")
   File.Pos := 0
   File.RawRead(buff, len := File.Length)
   File.Close()

   VarSetCapacity(MD5_CTX, 104, 0)
   DllCall("advapi32\MD5Init", "Ptr", &MD5_CTX)
   DllCall("advapi32\MD5Update", "Ptr", &MD5_CTX, "Ptr", &buff, "UInt", len)
   DllCall("advapi32\MD5Final", "Ptr", &MD5_CTX)
   loop 16
      md5 .= Format("{:02x}", *(&MD5_CTX + 87 + A_Index))
   Return md5
}
teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: Different images gives same MD5 - why?

08 May 2021, 06:52

As I understand, d41d8cd98f00b204e9800998ecf8427e is a md5 of an empty buffer.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: doanmvu, RSable and 393 guests