Compare commits

..
6 Commits
Author SHA1 Message Date
juhnsa 3ebb2d08d6 Fix issue 2025-08-27 13:37:00 +02:00
juhnsa b76c21bf74 Add feedback 2025-08-27 13:31:20 +02:00
juhnsa 1ad962d223 Fix issues 2025-08-27 13:27:36 +02:00
Juhn-Vitus Saß dca01a4116 Merge pull request 'issue-2 Resolves #2' (#4) from issue-2 into main
Reviewed-on: #4
2025-08-27 13:11:27 +02:00
juhnsa c16da21d63 Add cli to add and remove cards 2025-08-27 13:09:04 +02:00
juhnsa b01b0d4eb5 outsource hash function 2025-08-26 16:14:10 +02:00
2 changed files with 52 additions and 14 deletions

No files matched your search

+7
View File
@@ -0,0 +1,7 @@
function hash(str)
local hash = 0
for i = 1, #str do
hash = (hash * 31 + string.byte(str, i)) % 65536
end
return string.format("%04X", hash)
end
+45 -14
View File
@@ -1,5 +1,6 @@
local fullPath = shell.getRunningProgram()
local dir = fullPath:match("^(.*/)") or ""
local args = {...}
local config = dofile(dir..'taskguard.cfg')
@@ -13,14 +14,7 @@ local function split(str, divider)
return t
end
local function hash(str)
local hash = 0
for i = 1, #str do
hash = (hash * 31 + string.byte(str, i)) % 65536
end
return string.format("%04X", hash)
end
dofile(dir..'hash.lua')
local function check_for_outdated_sessions()
local expired = 0
@@ -37,30 +31,27 @@ end
local function validate_key(card_holder, card_clearance, random_chars)
local card_hash = hash(card_holder..','..card_clearance..','..random_chars)
local key_file = fs.open(dir..config.key_file, 'r')
local card_authentic = false
while true do
local trusted_hash = key_file.readLine()
if trusted_hash == card_hash then
print("Valid card "..card_hash)
card_authentic = true
key_file.close()
return true, card_hash
end
if not line then break end -- Stop when EOF reached
if not trusted_hash then break end -- Stop when EOF reached
end
key_file.close()
if card_authentic == false then
print("Unknown card "..card_hash)
return false
end
end
if args[1] == "serve" then
rednet.open(config.modem_side)
rednet.host('tg', config.name)
print("Server started, waiting for requests...")
while true do
local id, msg, proto = rednet.receive('tg')
check_for_outdated_sessions()
@@ -116,3 +107,43 @@ while true do
print("Transaction finished")
end
end
elseif args[1] == "add_card" then
if args[2] then
local new_card = args[2]
local new_card_parts = split(new_card, ',')
local card_holder = new_card_parts[1]
local card_hash = hash(new_card)
local key_file = fs.open(card_hash..'.key', 'w')
key_file.write(new_card..'\n')
key_file.close()
local trusted_keys = fs.open(dir..config.key_file, 'a')
trusted_keys.write(card_hash)
trusted_keys.close()
print("Created key "..card_hash..".key in root dir")
end
elseif args[1] == "remove_card" then
if args[2] then
local key_hash = args[2]
local trusted_keys = fs.open(dir..config.key_file, 'r')
local new_key_table = {}
while true do
local trusted_hash = trusted_keys.readLine()
if trusted_hash ~= key_hash then
table.insert(new_key_table, trusted_hash)
else
print("Removed "..key_hash)
end
if not trusted_hash then break end -- Stop when EOF reached
end
trusted_keys.close()
local trusted_keys = fs.open(dir..config.key_file, 'w+')
for _, entry in ipairs(new_key_table) do
trusted_keys.write(entry .. '\n')
end
trusted_keys.close()
end
end