Encrypting a database in Lua - lua

I am working with a database I am trying to make secure. I have been able to create the database and access the information within it. However once I try to encrypt the individual pieces of information within the database I run into trouble. I am using sqlite3 for my database and openssl to try to encrypt the database.
local users = [[CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, username, password);]]
db:exec( users )
local nameData = cipher:encrypt ( nameField.text, "sbs_math_key" )
local passData = cipher:encrypt ( passwordField.text, "sbs_math_key" )
local tablefill =[[INSERT INTO users VALUES (NULL, ']].. nameData ..[[',']].. passData ..[['); ]]
db:exec( tablefill )
This works if I am inserting into an existing database, however if I am creating a new database it won't let me insert the encrypted information.

Try this:
local nameData = mime.b64 ( cipher:encrypt ( nameField.text, "sbs_math_key") )
local passData = mime.b64 ( cipher:encrypt ( passwordField.text, "sbs_math_key") )
print( "Before : "..nameData )
local tablefill =[[INSERT INTO users VALUES (NULL, ']].. nameData ..[[',']].. passData ..[['); ]]
db:exec( tablefill )
message = cipher:decrypt ( mime.unb64 ( nameData ), "sbs_math_key" )
print( "After : "..message )

Related

LUA|MTA attempt to index global "zoneName" (a nil value)

I'm encountering a problem in a Lua script that I'm learning from (i am new to Lua) this error got me heavily confused, when I run the code it gives me this following error:
attempt to index global "zoneName" (a nil value)
this is my code:
local zoneName = zoneName:gsub ( "'", "" )
if dbExec ( handler, "INSERT INTO `safeZones` (`rowID`, `zoneName`, `zoneX`, `zoneY`, `zoneWidth`, `zoneHeight`) VALUES (NULL, '".. tostring ( zoneName ) .."', '".. tostring ( zoneX ) .."', '".. tostring ( zoneY ) .."', '".. zoneWidth .."', '".. zoneHeight .."');" ) then
createSafeZone ( { [ "zoneName" ] = zoneName, [ "zoneX" ] = zoneX, [ "zoneY" ] = zoneY, [ "zoneWidth" ] = zoneWidth, [ "zoneHeight" ] = zoneHeight } )
outputDebugString ( "Safe Zones: Safe zone created name: ".. tostring ( zoneName ) )
return true
else
return false, "Unable to create the safe zone"
end
You reference zoneName already in it's definition, you code equals to
local zoneName = nil:gsub("'", "")
hence the error (zoneName is not yet defined when Lua tries to execute zoneName:gsub()).
Either define zoneName before the gsub() call or use string.gsub()

How to Iterate string_table and modify lines

For building a dynamic SELECT I am iterating over a field list and adding an alias.
However, that clears the list.
REPORT ZITER_TEST.
CONSTANTS lc_alias TYPE string VALUE 'ref'.
DATA lt_field TYPE string_table.
lt_field = VALUE string_table(
( CONV string( 'PERNR' ) )
( CONV string( 'SUBTY' ) )
).
" lt_field: ['PERNR','SUBTY']
lt_field = VALUE string_table( FOR <lv_tmp> IN lt_field
( lc_alias && `~` && <lv_tmp> )
).
" lt_field: [] instead of: ['ref~PERNR','ref~SUBTY']
The ABAP documentation of VALUE says:
In assignments to a data object, the target variable is used directly and no temporary data object is created. This variable is initialized or overwritten in full before the assignment of the values specified in the parentheses. Its original value, however, is still available in an optional LET expression.
So, instead of:
lt_field = VALUE string_table( FOR <lv_tmp> IN lt_field
( lc_alias && `~` && <lv_tmp> )
).
Use:
lt_field = VALUE string_table( LET lt_field_temp = lt_field IN
FOR <lv_tmp> IN lt_field_temp
( lc_alias && `~` && <lv_tmp> )
).
You just have to use some temporary table. It works in my case:
REPORT ZITER_TEST.
CONSTANTS lc_alias TYPE string VALUE 'ref'.
DATA: lt_field_temp TYPE string_table,
lt_field TYPE string_table.
lt_field_temp = VALUE string_table(
( CONV string( 'PERNR' ) )
( CONV string( 'SUBTY' ) )
).
lt_field = VALUE string_table( FOR <lv_tmp> IN lt_field_temp
( lc_alias && '~' && <lv_tmp> )
).
Looping table with field-symbol will be significantly faster than using temporary table in either form (LET or direct assign):
LOOP AT lt_field ASSIGNING FIELD-SYMBOL(<field>)
<field> = lc_alias && `~` && <field>.
ENDLOOP.

What's a way to retrive the "path" portion of dxlHere() and go up two dirs?

We are regularly setting up new DOORS installations on standalone networks, and each of these networks use slightly different drive mappings and installation directories. We have a set of DXL scripts that we copy over to each network that uses DOORS, but these DXL scripts reference some Microsoft Word templates that are used as the basis for custom-developed module export scripts.
We no longer have a DXL expert in-house, and I'm trying to make the scripts more portable so that they no
longer contain hard-coded file paths. Because we copy all of the templates and DXL files in a pre-defined directory structure, I can use the dxlHere() function to figure out the execution path of the DXL script, which would print something like this:
<C:\path\to\include\file\includeFile.inc:123>
<C:\path\to\include\file\includeFile.inc:321>
<Line:2>
<Line:5>
<Line:8>
What I'd like to do is extract everything before file\includeFile.inc:123>, excluding the starting <. Then I want to append templates\template.dotx.
For example, the final result would be:
C:\path\to\inclue\template.dotx
Are there any built-in DXL functions to handle string manipulation like this? Is regex the way to go? If so, what regexp would be appropriate to handle this?
Thanks!
I got this... kind of working.
dxlHere is something I don't work with much, but this seems to work- as long as it's saved to a an actual dxl or inc file (i.e. not just run from the editor)
string s = dxlHere()
string s2 = null
string s3 = null
Regexp r = regexp2 ( "\\..*:.*> $" )
Regexp r2 = regexp2 ( "/" )
if ( r s ) {
s2 = s[ 1 : ( ( start ( 0 ) ) - 1 ) ]
s3 = s[ 1 : ( ( start ( 0 ) ) - 1 ) ]
int x = 0
while ( r2 s2 ) {
x++
s2 = s2[ ( ( start ( 0 ) ) + 1 ) : ]
}
int z = 0
for ( y = 0; y <= length( s3 ); y++ ){
if ( s3[y] == '/' ) {
z++
if ( z == ( x - 2 ) ) {
s = s3[ 0 : y ]
break
}
}
}
}
print s
So we're doing a single regexp to check if we have a valid 'location', then running through it to find ever '/' character, then leaving off the last 2 of them.
Hope this helps!

corona sdks - Why ain't my database updating count?

I seem to be having an issue with my database in corona sdk. I want to store the play counts every time a player plays the game. my project doesn't have a gamescene and plays only through menu.lua. I have set up a highscore database (which is separate from the other one but exactly the same) and it works fine and updates/loads data fine. My play count data base however doesn't work.
At first the play count increases by 1 when the game returns to menu again once its removed. But tries after that doesn't increase the value, it's like it has reset.
Globals.lua
M.plays = 0
--------------------------
local function setupDatabase2()
local dbPath = system.pathForFile("appInfo.db3", system.DocumentsDirectory)
local db = sqlite3.open( dbPath )
local tablesetup1 = [[
CREATE TABLE played (id INTEGER PRIMARY KEY, plays);
INSERT INTO played VALUES (NULL, '0');
]]
db:exec( tablesetup1 ) --Create it now.
db:close() --Then close the database
end
setupDatabase2()
M.loadAppInfo = function()
local dbPath = system.pathForFile("appInfo.db3", system.DocumentsDirectory)
local db = sqlite3.open(dbPath)
for row in db:nrows("SELECT * FROM played WHERE id = 1") do
M.plays = tonumber(row.plays)
end
db:close()
end
M.saveAppInfo = function()
local dbPath = system.pathForFile("appInfo.db3", system.DocumentsDirectory)
local db = sqlite3.open(dbPath)
local update = "UPDATE played SET plays='" .. M.plays .."' WHERE id=1"
db:exec(update)
db:close()
end
return M
menu.lua
local utils = require("helpers.globals")
local play = 0
----------------------------------------
function scene:createScene( event )
local group = self.view
utils.loadHighscoreInfo() -- Separate db which is working fine
utils.loadAppInfo()
function beginGame( event )
timerSrc = timer.performWithDelay(400, createBlock, -1)
Runtime:addEventListener("enterFrame", gameLoop)
play = play +1
utils.plays = play
utils.saveAppInfo()
end
Your SQL statement is doing all of that. Your current sql creates a table everytime it is called. Try this:
local tablesetup1 = [[
CREATE TABLE IF NOT EXISTS played (id INTEGER PRIMARY KEY, plays);
INSERT INTO played VALUES (NULL, '0');]]
I guess that statement is pretty much self explanatory :)

Lua: Table inside Object

I'm using OO abstraction in LUA and I'm having trouble with tables inside objects.
I'm using this approach to implement OO support: Object Orientation Tutorial
If i define a table inside the Class like this:
fields = {}
It will be shared by all instances of that Class. Something like an static attribute in Java. However, I'd like to get a normal class attribute instead.
That sounds odd to me, since it works correctly for non-table items like this:
attribute = 0
That is not going to be shared between all instances.
Here is a complete example:
local function C1Constructor( self )
print( "C1: Constructor... " )
self.fields = {}
end
local function C1SetName( self, name )
self.name = name
end
local function C1Add( self, name, value )
print( "C1: (" .. self.name .. ") :: Add: " .. name .. " = " .. value )
self.fields[ name ] = value
end
local function C1Show( self )
print( "C1: (" .. self.name .. ") :: Show:" )
for name, value in pairs( self.fields ) do
print(" " .. name .. " = " .. value )
end
end
C1 = {
name = "",
constructor = C1Constructor,
setName = C1SetName,
add = C1Add,
show = C1Show,
fields = {},
}
function C1.new( o )
o = o or { }
setmetatable( o, { __index = C1 } )
o:constructor()
return o;
end
c1a = C1.new()
c1b = C1.new()
c1a:setName( "Obj A" )
c1b:setName( "Obj B" )
c1a:show()
c1b:show()
c1a:add( "k1", "v1" )
c1b:add( "k2", "v2" )
c1a:show()
c1b:show()
I know how to fix it. But I don't know what is happening behind the scenes:
local function C1Constructor( self )
print( "C1: Constructor... " )
self.fields = {}
end
What am I missing here?
Thanks very much,
Tables are referenced values. When you store a table in a variable you are storing a reference to the table and not a primitive value (like for a string or a number). As such if you create one table and share it among instances you only have one table. When you create a table per-instance (e.g. in your constructor) then you have one table per-instance and not a shared table.

Resources