MD5 is very useful to verify the consistency of file, specially after a file transfer. Below I show you how can you calculate the MD5 of file using Lua/Corona.
local function calculateMD5 (filename, systemDirectory)
systemDirectory = systemDirectory or system.DocumentsDirectory -- using DocumentsDirectory as default if param not provided
local crypto = require "crypto"
local rfilePath = system.pathForFile( filename, systemDirectory )
if rfilePath == nil then -- this can be nil when using ResourceDirectory. If using DocumentsDirectory or TemporaryDirectory, it will return "not nil" even if the file does not exist
print("Error: file does not exist") -- error
return false
end
local rfh = io.open( rfilePath, "rb" )
if rfh == nil then
print("Error: file does not exist") -- error
return false
end
local data = rfh:read( "*a" )
if not data then
print( "Error: read error!" )
return false
end
io.close( rfh )
local hash = crypto.digest( crypto.md5, data )
return ( hash )
end