@XenonBL
Hi, sure, here’s some sample code to begin with:
[lua]crypt = require(“crypto”)
function game:loadStatus()
------------------ HASH SECURITY - LOAD
local hpath = system.pathForFile(“hashFilename”,system.DocumentsDirectory)
local hfile = io.open(hpath)
local shash
local function okButt(event)
self:resetGame() – you’d eventually want to reset game after the alert
end
if not hfile then – hash file must be there, unless it’s “first launch”
native.showAlert(“Error”,“File not found! Press OK to continue.”,{“Ok”},okButt)
return
else
shash = hfile:read() – reads content of hash file…which is the hash string
hfile:close()
end
local fpath = system.pathForFile(“saveDataFilename”,system.DocumentsDirectory)
local cfile = io.open(fpath) – loads save data file
if not cfile then – data file must be there, unless it’s “first launch”
native.showAlert(“Error”,“File not found! Press OK to continue.”,{“Ok”},okButt)
return
else
local content = cfile:read("*a") – reads save data file - it’s a text file in this case
local hash = crypt.digest(crypt.md5,content) – generates md5 hash of the read data file
if hash == shash then – if the two hash strings are equal…
cfile:seek(“set”) – seek to the beginning of data file…so you can begin reading it normally
else – otherwise close the file. Someone cheated!
cfile:close()
native.showAlert(“Error”,“Someone tried to cheat! Press OK to continue”,{“Ok”},okButt)
return
end
– READ THE DATA FILE SAFELY AFTER THIS POINT
– …
end
end
function game:saveStatus()
------------------ HASH SECURITY - SAVE
local fpath = system.pathForFile(“saveDataFilename”,system.DocumentsDirectory)
local cfile = io.open(fpath)
– …
– SAVE ALL THE GAME DATA HERE
– …
cfile:close() – close the file first!
cfile = io.open(fpath) – reopen data file
local content = cfile:read("*a") – and read it all in a string
local hash = crypt.digest(crypt.md5,content) – generate a hash of the data string
cfile:close()
local hpath = system.pathForFile(“hashFilename”,system.DocumentsDirectory)
local hfile = io.open(hpath) – open the hash file
if not hfile then – if doesn’t exist, create it
hfile = io.open(fpath,“w”)
else
hfile:close() – otherwise open it in update mode - and overwrite it
hfile = io.open(fpath,“w+”)
end
hfile:write(hash) – save the hash string
hfile:close()
end[/lua]
It is actually quite easy. You can also easily make a couple of generic functions out of this.
Note that this is only for files the app saves/reads in the Temp or Documents directory…since Lua scripts cannot be modified as they’re compiled by the Ansca builder.
The only thing you need to take care of within this sample code is the “first launch” situation. It’s not uncommon to save a dummy “launched” file and then check for it later on.
I hope this helps. [import]uid: 5750 topic_id: 4224 reply_id: 13814[/import]