Commit 187d364e authored by Danny SMc's avatar Danny SMc 🎧

Update uAPI

parent 1acb8ec0
-- Written by DannySMc
-- Twitter: @dannysmc95
-- uAPI Version: 2.1
-- uAPI Version: 3.1b
-- [[ Updates - utilizes encryption and decryption and checksum addons getting rid of the old encryption,
deletes browser functions as uncontinued program, some programs may not work with this, type of encryption!
-- ]]
function getVer()
uVer = "1.4"
return uVer
end
-- Configuration File Protocols
function saveConfig(table, file)
......@@ -15,7 +23,6 @@ function loadConfig(file)
return ret
end
-- Beaming Technology
function beam(username, xcord, ycord, zcord, blockSide)
command = peripheral.wrap(blockSide)
......@@ -113,6 +120,8 @@ function printC(Text, Line, NextLine, Color, BkgColor) -- print centered
end
function printL(Text, Line, NextLine, Color, BkgColor) -- print line
local x, y = term.getSize()
if ((term.isColor) and (term.isColor() == false) and (Text == " ")) then Text = "-" end
for i = 1, x do
......@@ -126,6 +135,7 @@ function printL(Text, Line, NextLine, Color, BkgColor) -- print line
if Color then resetCol(Color, BkgColor) end
return true
end
function printA(Text, xx, yy, NextLine, Color, BkgColor) -- print anywhere
term.setCursorPos(xx,yy)
if Color then setCol(Color, BkgColor) end
......@@ -188,24 +198,6 @@ function getPBFile(PBCode, uPath) -- pastebin code of the file, and path to save
end
end
-- Browser Functions
function webRequest(webAddress)
webFile = http.get("http://summotion.netai.net/uweb/"..textutils.urlEncode(webAddress)..".txt")
return webFile
end
function delWebDat()
if fs.exists("webDat") then
if fs.delete("webDat") == true then
return true
else
return "Delete Failed"
end
else
return false
end
end
-- Database Functions
db = {}
db.__index = db
......@@ -269,97 +261,9 @@ function db.insertString(Filename, AString)
return true
end
-- Encryption
function encrypt(Input, keyN) -- takes a string and returns an encrypted version, if keyN 1 - 9 returns encrypted string with solid number hash
local inType = type(Input)
local keystore = {}
local i = 1
local dec = ""
local key
if inType == "number" then
Input = tostring(Input)
end
repeat
if keyN then
key = keyN
else
key = math.random(9)
end
table.insert(keystore, key)
local tstr = string.char(Input:byte(i) + key)
dec = dec .. tstr
i = i + 1
until Input:byte(i) == nil
local cin = ""
for i = 1, #keystore do
cin = cin..keystore[i]
end
if keyN then
return dec
else
return dec, cin
end
end
function decrypt(Input, Hash, keyY) -- takes encrypted string with either hash (or solid hash if keyY is true) and converts it back to readable text
local keystore, tstr, i, dec
if keyY then
keystore = {Hash}
else
keystore = hashSplit(Hash)
end
i = 1
dec = ""
repeat
if keyY then
tstr = string.char(Input:byte(i) - keystore[1])
else
tstr = string.char(Input:byte(i) - keystore[i])
end
dec = dec .. tstr
i = i + 1
until Input:byte(i) == nil
return dec
end
function repCrypt(Input, numHash)
local keystore = {}
local i = 1
local dec = ""
local keyLength = #tostring(numHash)
numHash = tonumber(numHash)
local posCou = 1
repeat
if posCou > keyLength then posCou = 1 end
key = tonumber(string.sub(numHash, posCou, posCou))
posCou = posCou + 1
table.insert(keystore, key)
local tstr = string.char(Input:byte(i) - key)
dec = dec .. tstr
i = i + 1
until Input:byte(i) == nil
return dec
end
function repdeCrypt(Input, numHash)
local i = 1
local dec = ""
local keyLength = #tostring(numHash)
numHash = tonumber(numHash)
local posCou = 1
repeat
if posCou > keyLength then posCou = 1 end
key = tonumber(string.sub(numHash, posCou, posCou))
posCou = posCou + 1
tstr = string.sub(Input, i, i)
tstr = string.char(Input:byte(i) + key)
dec = dec .. tstr
i = i + 1
until Input:byte(i) == nil
return dec
end
-- Serial Generator
function serialGen(digits) -- seems to become unstable above 18 digits long
local serial
for i = 1, digits do
if i == 1 then
......@@ -371,3 +275,177 @@ function serialGen(digits) -- seems to become unstable above 18 digits long
serial = tonumber(serial)
return serial
end
-- Encryption & Checksum!
local function zfill(N)
N=string.format("%X",N)
Zs=""
if #N==1 then
Zs="0"
end
return Zs..N
end
local function serializeImpl(t)
local sType = type(t)
if sType == "table" then
local lstcnt=0
for k,v in pairs(t) do
lstcnt = lstcnt + 1
end
local result = "{"
local aset=1
for k,v in pairs(t) do
if k==aset then
result = result..serializeImpl(v)..","
aset=aset+1
else
result = result..("["..serializeImpl(k).."]="..serializeImpl(v)..",")
end
end
result = result.."}"
return result
elseif sType == "string" then
return string.format("%q",t)
elseif sType == "number" or sType == "boolean" or sType == "nil" then
return tostring(t)
elseif sType == "function" then
local status,data=pcall(string.dump,t)
if status then
return 'func('..string.format("%q",data)..')'
else
error()
end
else
error()
end
end
local function split(T,func)
if func then
T=func(T)
end
local Out={}
if type(T)=="table" then
for k,v in pairs(T) do
Out[split(k)]=split(v)
end
else
Out=T
end
return Out
end
local function serialize( t )
t=split(t)
return serializeImpl( t, tTracking )
end
local function unserialize( s )
local func, e = loadstring( "return "..s, "serialize" )
local funcs={}
if not func then
return e
end
setfenv( func, {
func=function(S)
local new={}
funcs[new]=S
return new
end,
})
return split(func(),function(val)
if funcs[val] then
return loadstring(funcs[val])
else
return val
end
end)
end
local function sure(N,n)
if (l2-n)<1 then N="0" end
return N
end
local function splitnum(S)
Out=""
for l1=1,#S,2 do
l2=(#S-l1)+1
CNum=tonumber("0x"..sure(string.sub(S,l2-1,l2-1),1) .. sure(string.sub(S,l2,l2),0))
Out=string.char(CNum)..Out
end
return Out
end
local function wrap(N)
return N-(math.floor(N/256)*256)
end
function checksum(S,num) -- args strInput and intPassNumber
local sum=0
for char in string.gmatch(S,".") do
for l1=1,(num or 1) do
math.randomseed(string.byte(char)+sum)
sum=sum+math.random(0,9999)
end
end
math.randomseed(sum)
return sum
end
local function genkey(len,psw)
checksum(psw)
local key={}
local tKeys={}
for l1=1,len do
local num=math.random(1,len)
while tKeys[num] do
num=math.random(1,len)
end
tKeys[num]=true
key[l1]={num,math.random(0,255)}
end
return key
end
function encrypt(data,psw) -- args strInput and strPassword
data=serialize(data)
local chs=checksum(data)
local key=genkey(#data,psw)
local out={}
local cnt=1
for char in string.gmatch(data,".") do
table.insert(out,key[cnt][1],zfill(wrap(string.byte(char)+key[cnt][2])),chars)
cnt=cnt+1
end
return string.sub(serialize({chs,table.concat(out)}),2,-3)
end
function decrypt(data,psw) -- args strInput and strPassword
local oData=data
data=unserialize("{"..data.."}")
if type(data)~="table" then
return oData
end
local chs=data[1]
data=data[2]
local key=genkey((#data)/2,psw)
local sKey={}
for k,v in pairs(key) do
sKey[v[1]]={k,v[2]}
end
local str=splitnum(data)
local cnt=1
local out={}
for char in string.gmatch(str,".") do
table.insert(out,sKey[cnt][1],string.char(wrap(string.byte(char)-sKey[cnt][2])))
cnt=cnt+1
end
out=table.concat(out)
if checksum(out or "")==chs then
return unserialize(out)
end
return oData,out,chs
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment