Defcrypt -- string encrypt/decrypt tool for Defold

3 Likes

Thanks for sharing! I noticed that you’re declaring global functions in the crypt Lua module. I would recommend that the functions in crypt.lua are local to the module to avoid code in other places to accidentally replace your functions or vice versa. Like this:

-- crypt.lua
local M = {}

local function repeat_key(key, length)
	-- the code from your global repeat_key function here
end

local function xor(num1, num2)
	-- the code from your global xor function here
end

function M.crypt(message, key)
	-- the code from your global crypt function here
end

return M

The module can now be used this way:

local crypt = require("crypt")

crypt.crypt(message, key)

To avoid the double crypt.crypt you can do like this:

local crypt = require("crypt").crypt
crypt(message, key)

Or your crypt.lua can define a meta method for the call meta function:

-- crypt.lua
local M = {}

...

function M.crypt(message, key)
	-- the code from your global crypt function here
end

return setmetatable(M, {
	__call = function(t, ...)
		return M.crypt(...)
	end
})

Which allows you to do this:

local crypt = require("crypt")
crypt(message, key)

Finally, if you’re only ever going to have one public function you might as well return it, like this:

-- crypt.lua
local function repeat_key(key, length)
	-- the code from your global repeat_key function here
end

local function xor(num1, num2)
	-- the code from your global xor function here
end

local function crypt(message, key)
	-- the code from your global crypt function here
end

return crypt

And use it like before:

local crypt = require("crypt")
crypt(message, key)
5 Likes

Hi @COCO !

Nice resource!
I would suggest that you specify in the readme that this is using the XOR technique to encrypt/decrypt the string.

I’d like to make it as an editor script, so we could encrypt secret messages and add them to our scripts automatically. And then, whenever we need them, decrypt them on runtime, game hackers will have difficulties getting or changing them. Like domain white list for html5 games.

1 Like