Skip to content

Instantly share code, notes, and snippets.

@SammyForReal
Created April 7, 2023 13:29
Show Gist options
  • Save SammyForReal/a612bf4947c16f6fa66652cb722adf2a to your computer and use it in GitHub Desktop.
Save SammyForReal/a612bf4947c16f6fa66652cb722adf2a to your computer and use it in GitHub Desktop.
Network tools for ComputerCraft (not recommended).
--[[
This is a small api to make wireless messages somewhat secure.
It is not recommended to use this in production as this is just awful. Use something strong.
However, this may be worth a look to understand what kind of steps could be used to secure a system.
]]
local tools = {}
---Gets a "random" number in hex format.
---@return string salt The number as string in hex format.
function tools.salt()
-- Using djb2
local salt = 9551
for i=1,100 do
salt = (salt/math.random())%696969
end
return string.format("%x", math.floor((salt*100)%898989))
end
tools.END = "EOF;;;"
tools.ANF = ":=:{"
---Xor encryption/decryption. Salt will be put on each end and seperated with escape chars.
---@param plaintext string The message to encrypt
---@param key string The key to encrypt the message. Ideally should be as long as the message for max strength.
---@param bDecode boolean whenever the message should be encrypted or decrypted.
---@return unknown
function tools.xor(plaintext, key, bDecode)
-- Salt
if not bDecode then
plaintext =
tools.salt()..tools.salt()..tools.salt()..tools.salt()..
tools.ANF..plaintext..tools.END
..tools.salt()..tools.salt()..tools.salt()..tools.salt()
end
-- Setup key/message
key = string.rep(key, math.ceil(#plaintext / #key))
ciphertext = ""
--Xor
local cursor = 1
for i=1, #plaintext do
ciphertext = ciphertext .. string.char( bit32.bxor(plaintext:byte(i), key:byte(i)) )
end
--Cut salt out if decrypting.
if bDecode then
local _,nAnf = ciphertext:find(tools.ANF)
if nAnf then
ciphertext = ciphertext:sub(nAnf+1)
end
local nEnd,_ = ciphertext:find(tools.END)
if nEnd then
ciphertext = ciphertext:sub(1,nEnd-1)
end
end
return ciphertext
end
tools.chunkSize = 16
---Xor encryption but with multible layers, as long as the key is longer than the message.
---@param msg string The message to encrypt.
---@param key string The key. should be as long as possible for max strength, as this will create more layers.
---@param bDecrypt boolean whenever the message should be decrypted or not.
---@return string result The encrypted/decrypted message.
function tools.multiXor(msg, key, bDecrypt)
local keys = {}
for i=1,#key,tools.chunkSize do
table.insert(keys, string.sub(key, i, i+tools.chunkSize-1))
end
local nStart,nEnd,nSkip = 1,#keys,1
if bDecrypt then nStart,nEnd,nSkip = #keys,1,-1 end
for i=nStart,nEnd,nSkip do
msg = tools.xor(msg, keys[i], bDecrypt)
end
return msg
end
---TOTP. By giving a token and the current time (os.time()), a number representing a port will be returned.
---@param sToken string The secret token.
---@param nTime number os.time()
---@return number port
function tools.totp(sToken, nTime)
sToken = '@'..sToken..';'
nTime = math.floor(nTime*10)
-- djb2 algorithm
local port = 5381
for i=1, #sToken do
port = bit32.bxor(bit32.lshift(port, 5), port) + string.byte(sToken, i)
end
port = ((port%65535)^nTime)%65535
return port
end
tools.type = {
UPGRADE = 0x006900,
PING = 0x133700,
BODY = 0x007777,
CLOSE = 0x010101,
}
function tools.newPackage(nToID, nType, tData)
return {
-- Header
fromID = os.getComputerID(),
toID = nToID or (-1),
type = nType or (-1),
-- Body
body = tData or {tools.salt(),tools.salt(),tools.salt()},
-- Salt (To fuck with others)
["."..tools.salt()] = tools.salt(),
["@"..tools.salt()] = tools.salt(),
["."..tools.salt()] = tools.salt(),
["@"..tools.salt()] = tools.salt(),
["."..tools.salt()] = tools.salt(),
["@"..tools.salt()] = tools.salt(),
}
end
return tools
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment