kikito / md5.lua

MD5 sum in pure Lua, with no C and no external dependencies
MIT License
326 stars 151 forks source link

Add incremental MD5 function (feature request) #8

Closed ghost closed 8 years ago

ghost commented 8 years ago

The suggestion by @stejacob in https://github.com/kikito/md5.lua/issues/4#issuecomment-158112037 would not have fixed the issue reported there, but it is sound. Usual C implementations use md5_init, md5_update and md5_done (the names may vary), where md5_update can be called as often as necessary, and md5_done does the padding and calculates the final result. With Lua, the API can be something that is used like this:

-- this should print the MD5 of "Hello, World!" which is 65a8e27d8879283831b664bd8b7f0ad4
state = md5.new()
state:update("Hello")
state:update(", World!")
result = state:finish()
print(md5.tohex(result))

The advantage is that when the files are long, it's not necessary to send the whole file in one go: it can be calculated on the fly instead (only the last A,B,C,D and up to 63 bytes of buffer are necessary to be kept between calls, thus saving memory). This would allow calculating the MD5 of files larger than those that fit in memory

tst2005 commented 8 years ago

Hello,

I think the md5.lua is an old implementation. For a more modern implementation take a look at lockbox's md5 and his test. It use update, finish and ashex like you asked.

Regards,

kikito commented 8 years ago

I am not opposed to this, but the time I have very little time to spare on this project. If someone sends me a pull request I can review it and accept it, but I honestly don't have the time to develop a new feature such as this on my own.

kikito commented 8 years ago

Implemented in #10