md5 hash png files?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
OmarShehata
Party member
Posts: 259
Joined: Tue May 29, 2012 6:46 pm
Location: Egypt
Contact:

md5 hash png files?

Post by OmarShehata »

I'm making an automatic updater for my game, and in order to compare whether two files are the same (so I only upload the file that have been changed), I'm using an md5 hash with kikito's library https://github.com/kikito/md5.lua

This system works really well. Except there seems to be a small problem with hashing png files. It's not a problem on the md5 library because I used different ones and I'm getting the same thing.

The problem is, any png file I hash gives me the same hash: 658e8dc0bf8b9a09b36994abf9242099

I think it's a problem with the way I'm reading the file. I'm doing:

Code: Select all

local file = io.open("MyFile.png");
local data = file:read("*all");
And then hashing that. I searched around a bit and it looks like someone was having the exact same problem (getting that exact same hash) in python:

https://groups.google.com/forum/#!topic ... UsR8GcqZ08

And the solution was to use io.open("MyFile.png","rb") as a read mode. When I do that however, the hashing takes an extremely long time and just outputs all 0's.

Is it just not practical to do hashing with Lua like that? (This is all with LuaJIT by the way)
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: md5 hash png files?

Post by Robin »

Could you show us your code?
Help us help you: attach a .love.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: md5 hash png files?

Post by bartbes »

Indeed you need binary mode, so the "rb" mode is what you want. And, [manual=file:read]your pattern to file:read is wrong[/manual].
I also can't be sure what's wrong without seeing the code, but I do know that kikito's library doesn't use luajit's bit library which would make it a lot faster.
User avatar
OmarShehata
Party member
Posts: 259
Joined: Tue May 29, 2012 6:46 pm
Location: Egypt
Contact:

Re: md5 hash png files?

Post by OmarShehata »

Here's a zip of a .love file plus a test image outside of it:

http://4urentertainment.org/storage/HashTest.zip

This is all the code inside main.lua

Code: Select all

function love.load(args)
    local file = io.open("BioloJoeSheet.png","rb");
    local data = file:read("*a");
    local hasher = require("hash")
    local hash = hasher.sumhexa(data)
    print(hash)
end
All I get are 0's. If i remove "rb" I get that specific hash I mentioned in the first post. I guess the speed issue can be fixed by using luajit's bit library as bartbes says, so I'm just trying to figure out why I'm getting 0's now.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: md5 hash png files?

Post by Robin »

OK, that's weird. I suspect a bug in the bitlib replacement used by md5.lua.
Help us help you: attach a .love.
User avatar
OmarShehata
Party member
Posts: 259
Joined: Tue May 29, 2012 6:46 pm
Location: Egypt
Contact:

Re: md5 hash png files?

Post by OmarShehata »

I rewrote some bit functions to use LuaJIT's bit library instead, and it runs super fast, but I still get all 0's.

I tried using the hash library used by Vapor, and it works correctly!

https://github.com/josefnpat/vapor/blob ... b/hash.lua

Thanks Robin and Bartbes!
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: md5 hash png files?

Post by kikito »

I used to work in two separate environments: Lua 5.1 and Luajit. Now everything I do is in Luajit, so those ugly bit hacks should go away.
When I write def I mean function.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 5 guests