加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
security.lua 22.92 KB
一键复制 编辑 原始数据 按行查看 历史
dwdcth 提交于 2019-06-01 12:00 . security
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
-- 转载请注明出处
-- 作者: 无言独影
-- 联系QQ: 692118238
-- 2016年7月18日 20:14:23
-------------安全函数
--xxTEA加密及附加部分
function security.urlEncode(str)--url编码
if (str) then
str = string.gsub (str, "\n", "\r\n")
str = string.gsub (str, "([^%w ])",
function (c) return string.format ("%%%02X", string.byte(c)) end)
str = string.gsub (str, " ", "+")
end
return str
end
function security.urlDecode(str)--url解码
if (str) then
str = string.gsub (str, "+", " ")
str = string.gsub(str, "%%(%x%x)",
function(h) return string.char(tonumber(h, 16)) end)
return str
end
end
function security.url_safe_base64_encode(str)
local data = security.base64enc(str)
data = string.gsub(data, '+', '-')
data = string.gsub(data, '/', '_')
data = string.gsub(data, '=', '.')
return data
end
function security.url_safe_base64_decode(str)
-- logcat(str)
local data = string.gsub(str, '-', '+')
data = string.gsub(data, '_', '/')
data = string.gsub(data, '%.', '=')
-- logcat(str)
data = security.base64dec(data)
return data;
end
function security.xxTEAEncrypt(str, key, toBase64)
toBase64 = toBase64 or true
if (str == "") then
return ""
end
local v = _str2long(str, true)
local k = _str2long(key, false)
if (#k < 4) then
for i = #k+1, 4, 1 do
k [i] = 0
end
end
local n = #v
local z = v [n]
local y = v [1]
local delta = -0x61C88647
local q = math.floor(6 + 52 / (n)) --设定加密轮数
local sum = 0
while 0 < q do
q = q -1
sum = _int32(sum + delta)
e = bit.band(bit.rshift(sum , 2), 3)
local p = 0
for p = 0, n-2 do
y = v [p + 2]
mx = bit.bxor(_int32(bit.bxor(bit.band(bit.rshift(z, 5), 0x07ffffff), bit.lshift(y , 2)) + bit.bxor(bit.band(bit.rshift(y, 3), 0x1fffffff), bit.lshift(z, 4))), _int32(bit.bxor(sum, y) + bit.bxor(k[bit.bxor(bit.band((p), 3), e)+1], z)))
z = _int32(v[p+1] + mx)
v [p+1] = z
end
p = #v - 1
y = v [1]
mx = bit.bxor(_int32(bit.bxor(bit.band(bit.rshift(z, 5), 0x07ffffff), bit.lshift(y , 2)) + bit.bxor(bit.band(bit.rshift(y, 3), 0x1fffffff), bit.lshift(z, 4))), _int32(bit.bxor(sum, y) + bit.bxor(k[bit.bxor(bit.band((p), 3), e)+1], z)))
z = _int32(v[n] + mx)
v [n] = z
end
if (toBase64) then
local r = security.url_safe_base64_encode(_long2str(v, false))
return r
end
return _long2str(v, false)
end
function security.xxTEADecrypt(str, key, toBase64)
toBase64 = toBase64 or true
if (str == "") then
return ""
end
-- logcat(str)
if toBase64 then
str = security.url_safe_base64_decode(str)
end
local v = _str2long(str, false)
local k = _str2long(key, false)
if (#k < 4) then
for i = #k+1, 4, 1 do
k [i] = 0
end
end
local n = #v - 1
local z = v [n]
local y = v [1]
local delta = -0x61C88647
local q = math.floor(6 + 52 / (n+1)) --设定加密轮数
local sum = _int32(q * delta)
while (sum ~= 0) do
e = bit.band(bit.rshift(sum , 2), 3)
-- test_result = test_result .."e: "..e.."\n"
-- logcat ("e: "..e)
local p = n
for p = n, 1, -1 do
z = v [p]
-- test_result = test_result .."z: "..z.."p-1: "..(p-1).."\n"
-- logcat ("z: "..z.."p-1: "..p-1)
-- test_result = test_result .."y: "..y.."\n"
-- logcat ("y: "..y)
-- test_result = test_result .."p: "..p.."\n"
-- logcat ("p: "..p)
-- test_result = test_result .."k: "..(k[bit.bxor(bit.band((p), 3), e)+1]).."\n"
-- logcat ("k: "..k[bit.bxor(bit.band((p), 3), e)+1])
mx = bit.bxor(_int32(bit.bxor(bit.band(bit.rshift(z, 5), 0x07ffffff), bit.lshift(y , 2)) + bit.bxor(bit.band(bit.rshift(y, 3), 0x1fffffff), bit.lshift(z, 4))), _int32(bit.bxor(sum, y) + bit.bxor(k[bit.bxor(bit.band((p), 3), e)+1], z)))
-- test_result = test_result .."mx: "..mx.."\n"
-- logcat ("mx: "..mx)
y = _int32(v [p+1] - mx)
-- test_result = test_result .."y: "..y.."p: "..p.."\n"
-- logcat ("y: "..y.."p: "..p)
v [p+1] = y
end
p = 0
z = v[n+1]
-- test_result = test_result .."z2: "..z.."\n"
-- logcat ("z2: "..z)
mx = bit.bxor(_int32(bit.bxor(bit.band(bit.rshift(z, 5), 0x07ffffff), bit.lshift(y , 2)) + bit.bxor(bit.band(bit.rshift(y, 3), 0x1fffffff), bit.lshift(z, 4))), _int32(bit.bxor(sum, y) + bit.bxor(k[bit.bxor(bit.band((p), 3), e)+1], z)))
-- test_result = test_result .."mx2: "..mx.."\n"
-- logcat ("mx2: "..mx)
y = _int32(v[1] - mx)
-- test_result = test_result .."y2: "..y.."\n"
-- logcat ("y2: "..y)
v [1] = y
sum = _int32(sum - delta)
end
return _long2str(v, true)
end
function _long2str(v, w)
local len = #v
local n = bit.lshift((len - 1) , 2)
if (w) then
local m = v [len]
if ((m < n - 3) or (m > n)) then
return false
end
n = m
end
local s = {}
for i = 1 , len do
s [i] = string.packL(v[i])
end
if (w) then
return string.sub(table.concat(s, ''), 0, n)
else
return table.concat(s, '')
end
end
function _str2long(s, w)
local v = string.unpackL(s .. string.rep("\0", (4 - bit.band((string.len(s) % 4) , 3))))
if (w) then
v [#v+1] = string.len(s)
end
return v
end
function _int32(n)
while (n >= 2147483649) do
n = n - 4294967296
end
while (n <= -2147483649) do
n = n + 4294967296
end
return n
end
function security.tableToCryptJson(tb, key)
local tbx ={}
-- logcat_r(tb)
for k, v in pairs(tb) do
tbx[security.rmEncode(k)] = security.rmEncode(v)
-- logcat(k.." "..security.rmEncode(v))
end
local re = json.encode(tbx)
-- logcat(re)
return security.xxTEAEncrypt(re, key)
end
function security.cryptJsonToTable(str, key)
str = security.xxTEADecrypt(str, key)
local tbtemp = json.decode(str)
local tbx = {}
for k, v in pairs(tbtemp) do
tbx[security.rmDecode(k)] = security.rmDecode(v)
end
return tbx
end
--rmEncode/Decode最初由红运天猫提供,略微修改,简单的字符移位,需注意该方法加密后的字符串每次都不同。
--加盐md5处理
function security.hashCat(a, salt)
return security.md5(a .. salt)
end
-------------字符串函数
function string.packL(hex)--转为字符串
if hex == nil or hex == null then
return nil
end
if (type(hex)~="number") then
return nil, "str2hex invalid input type"
end
_int32(hex)
local result = ""
local index = 0
local str = bit.tohex(hex)
if tonumber(str, 16) < 257 then
str = string.sub(str, str:len()-1, str:len())
end
for index = str:len(), 1, -2 do
result=result..string.char(tonumber(str:sub(index-1, index), 16))
end
return result
end
function string.unpackL(str)--转为长整型数组
--判断输入类型
if (type(str)~="string") then
return nil, "hex2str invalid input type"
end
local i = 1
local result = ""
local tb = {}
local tmptb = {}
local hex = ""
while str:byte(i) ~= nil do
tmptb[i] = str:byte(i)
if (i % 4 == 0) then
for j = #tmptb, #tmptb-3, -1 do
hex = bit.tohex(tmptb[j])
-- logcat(hex)
hex = string.sub(hex, hex:len()-1, hex:len())
result = result.. hex
-- logcat("["..#tb.."] => "..result)
end
result = tonumber(result, 16)
if result ~= 0 then
tb[#tb+1] = _int32(result)
-- logcat("["..#tb.."] => "..tb[#tb])
-- else
--
-- logcat("["..#tb.."] => "..result)
end
result = ""
end
i = i + 1
end
result = ""
if (i % 4 ~= 0) then
for j = #tmptb, #tmptb-i % 4 +2, -1 do
hex = bit.tohex(tmptb[j])
logcat(hex)
hex = string.sub(hex, hex:len()-1, hex:len())
result = result.. hex
-- result=result.. string.format("%02X", tmptb[j])
end
if tonumber(result, 16) ~= 0 then
tb[#tb+1] = tonumber(result, 16)
end
end
return tb
end
function string.toByteTable(str)
local tb = {}
local i = 1
while str:byte(i) ~= nil do
tb[i] = str:byte(i)
i = i + 1
end
return tb
end
-------------网络函数
function net.get(url, timeout)
t = os.clock()
local http= require("socket.http");
local request_body = request_body
local response_body = {}
http.TIMEOUT = timeout or 5
local res, code, response_headers = http.request{
url = url,
method = "GET",
sink = ltn12.sink.table(response_body),
}
return table.concat(response_body, ""), code, os.clock() - t
end
function net.post(url, request_body, timeout)
t = os.clock()
local http=require("socket.http");
-- local socket = require "socket"
local request_body = request_body
local response_body = {}
http.TIMEOUT = timeout or 5
local res, code, response_headers = http.request{
url = url,
method = "POST",
headers =
{
["Content-Type"] = "application/x-www-form-urlencoded";
["Content-Length"] = string.len(request_body);
},
source = ltn12.source.string(request_body),
sink = ltn12.sink.table(response_body),
}
return table.concat(response_body, ""), code, os.clock() - t
end
--得到某个页面中第一个标签内的内容
function net.getFistTable(source, tag)
--source :网页源代码
--tag: 例如 title body
-- return source:match("<"..tag..".*>%s*(.*)%s*</"..tag..">")--匹配标签
-- toast2("<"..tag.."[.*>](.*)</"..tag..">")
local t = source:match("<"..tag.."[.*%>]?(.*)</"..tag..">")
t = string.trim(t)
t = string.gsub(t, "\n", "")
t = string.gsub(t, "</br>", "\n")
-- (还可以对常用标签进进行字符串处理)
return string.trim(t)--匹配标签
--返回的是标签下第一行的正文内容
-- lua的匹配模式非常简单
-- http://cloudwu.github.io/lua53doc/manual.html#6.4.1
-- 单个字符类跟一个 '*', 将匹配零或多个该类的字符。 这个条目总是匹配尽可能长的串;
-- 单个字符类跟一个 '+', 将匹配一或更多个该类的字符。 这个条目总是匹配尽可能长的串;
-- 单个字符类跟一个 '-', 将匹配零或更多个该类的字符。 和 '*' 不同, 这个条目总是匹配尽可能短的串;
-- 单个字符类跟一个 '?', 将匹配零或一个该类的字符。 只要有可能,它会匹配一个;
end
-------------设备应用函数
function system.openBrowerUrl(url)
if url:find("http://") == nil then url = "http://"..url end
-- os.execute(string.format("am start -a android.intent.action.VIEW -d "..url))
os.execute("su -c '"..string.rep(" ", 3000).."am start -a android.intent.action.VIEW -d "..url.."'")
toast("网址已经打开!")
xscript.stop()
end
function system.setIME(pattern)
local iRet, sRet = pcall(function()
if pattern == 0 then --百度小米版
os.execute("ime set com.baidu.input_mi/.ImeService ")
elseif pattern == 1 then --讯飞
os.execute("ime set com.iflytek.inputmethod/.FlyIME")
elseif pattern == 2 then --谷歌
os.execute("ime set com.google.android.inputmethod.pinyin")
elseif pattern == 3 then --手心
os.execute("ime set com.xinshuru.inputmethod/.FTInputService")
elseif pattern == 4 then --GO输入法
os.execute("ime set com.jb.gokeyboard/.GoKeyboard")
elseif pattern == 5 then --触宝输入法
os.execute("ime set com.cootek.smartinputv5.tablet/com.cootek.smartinput5.TouchPalIME")
elseif pattern == 6 then --QQ拼音
os.execute("ime set com.tencent.qqpinyin/.QQPYInputMethodService ")
elseif pattern == 7 then --百度输入法
os.execute("ime set com.baidu.input/.ImeService")
elseif pattern == 8 then --章鱼输入法
os.execute("ime set com.komoxo.octopusime/com.komoxo.chocolateime.LatinIME")
elseif pattern ==9 then --设置按键精灵输入法
os.execute("ime set com.cyjh.mobileanjian/.input.inputkb")
elseif pattern == 10 then --设置搜狗输入法
os.execute("ime set com.sohu.inputmethod.sogou/.SogouIME")
end
end)
if iRet == true then
return sRet
else
print(sRet)
return ""
end
end
-------------设备信息函数
function systeminfo.isEmulate()
local result = false
local s = string.find(os.exec("cat /proc/cpuinfo"), "model name")
if s ~= nil then
result = true
end
if xutil.isFileExists("/proc/irq/10/bstcamera") then
result = true
end
if xutil.isFileExists("/proc/bstid") then
result = true
end
if xutil.isFileExists("/proc/irq/20/memuguest") then
-- logcat("逍遥模拟器用户")
result = true
end
if xutil.isFileExists("/proc/irq/20/vboxguest") then
-- logcat("夜神模拟器用户")
result = true
end
return result
end
function systeminfo.getCid()
local cid=os.exec("cat /sys/block/mmcblk0/device/cid")
cid = string.gsub(cid, "%s*", "")
if tostring(cid) == "nil" then
cid = ""
end
return cid
end
local tempPath = xscript.scriptDir().."/.tmp"
--------------获取shell执行结果
function os.exec(exec)
os.execute(string.format("%s > %s", exec, tempPath))
local file = io.open(tempPath, "r")
local result=string.format("%s", file:read("*all"))
file:close()
os.remove(tempPath)
return result
end
--数组查找
function table.find(tb, str)
local function _find(tb)
for k,v in pairs(tb) do
if v == str then
return true
end
if type(v) == "table" then
_find(v)
end
end
return false
end
return _find(tb)
end
--返回数组长度
function table.getn(tb)
local sum = 0
local function countT(tb)
for k,v in pairs(tb) do
sum = sum + 1
if type(v) == "table" then
countT(v)
end
end
end
countT(tb)
return sum
end
-------图色重写类方法(可用作自适应分辨率变化,自用,屏蔽以防止冲突)
-- ratiow,ratioh = 1,1
-- --*为转换到实际像素,/为转换回脚本开发时的像素
-- local _findcolors = find.colors
-- function find.colors(tb,f,x1,y1,x2,y2)
-- if x1 == nil then
-- local isR,x,y,tb = _findcolors(tb,f)
-- if isR then
-- x = math.round(x / ratiow)
-- y = math.round(y / ratioh)
-- end
-- return isR,x,y,tb
-- end
-- x1 = math.round(x1 * ratiow)
-- y1 = math.round(y1 * ratioh)
-- x2 = math.round(x2 * ratiow)
-- y2 = math.round(y2 * ratioh)
-- for i = 1, #tb do
-- if xutil.isTable(tb[i]) then
-- tb[i][1] = tb[i][1] * ratiow
-- tb[i][2] = tb[i][2] * ratioh
-- end
-- end
-- local isR,x,y,tb = _findcolors(tb,f,x1,y1,x2,y2)
-- if isR then
-- x = math.round(x / ratiow)
-- y = math.round(y / ratioh)
-- end
-- return isR,x,y,tb
-- end
-- local _findcolor = find.color
-- function find.color(c,f,x1,y1,x2,y2)
-- x1 = math.round(x1 * ratiow)
-- y1 = math.round(y1 * ratioh)
-- x2 = math.round(x2 * ratiow)
-- y2 = math.round(y2 * ratioh)
-- local isR,x,y = _findcolor(c,f,x1,y1,x2,y2)
-- if isR then
-- x = math.round(x / ratiow)
-- y = math.round(y / ratioh)
-- end
-- return isR,x,y
-- end
-- local _iscolor = iscolor
-- function iscolor(c,f,x,y)
-- x = math.round(x * ratiow)
-- y = math.round(y * ratioh)
-- return _iscolor(x, y, c, f)
-- end
-- local _getcolor = getcolor
-- function getcolor(x,y)
-- x = math.round(x * ratiow)
-- y = math.round(y * ratioh)
-- local c, a, r, g, b = _getcolor(x, y)
-- c = tonumber(bit.tohex(r, 2)..bit.tohex(g, 2)..bit.tohex(b, 2), 16)
-- return c, a, r, g, b
-- end
-- local _touchclick = touch.click
-- function touch.click(x,y,id,p)
-- x = math.round(x * ratiow)
-- y = math.round(y * ratioh)
-- if id == nil and p == nil then
-- _touchclick(x,y)
-- elseif p == nil then
-- _touchclick(x,y,id)
-- else
-- _touchclick(x,y,id,p)
-- end
-- end
-- local _touchclick2 = touch.click2
-- function touch.click2(x,y)
-- x = math.round(x * ratiow)
-- y = math.round(y * ratioh)
-- if id == nil and p == nil then
-- _touchclick2(x,y)
-- elseif p == nil then
-- _touchclick2(x,y,id)
-- else
-- _touchclick2(x,y,id,p)
-- end
-- end
-- local _touchdown = touch.down
-- function touch.down(x,y,id,p)
-- x = math.round(x * ratiow)
-- y = math.round(y * ratioh)
-- if id == nil and p == nil then
-- _touchdown(x,y)
-- elseif p == nil then
-- _touchdown(x,y,id)
-- else
-- _touchdown(x,y,id,p)
-- end
-- end
-- local _touchmove = touch.move
-- function touch.move(x,y,id,p)
-- x = math.round(x * ratiow)
-- y = math.round(y * ratioh)
-- if id == nil and p == nil then
-- _touchmove(x,y)
-- elseif p == nil then
-- _touchmove(x,y,id)
-- else
-- _touchmove(x,y,id,p)
-- end
-- end
-- local _touchswipe = touch.swipe
-- function touch.swipe(x1,y1,x2,y2,time)
-- x1 = math.round(x1 * ratiow)
-- y1 = math.round(y1 * ratioh)
-- x2 = math.round(x2 * ratiow)
-- y2 = math.round(y2 * ratioh)
-- if time == nil then
-- _touchswipe(x1,y1,x2,y2)
-- else
-- _touchswipe(x1,y1,x2,y2,time)
-- end
-- end
-- local _touchup = touch.up
-- function touch.up(x,y,id)
-- if x == nil and y == nil and id == nil then
-- _touchup()
-- elseif y == nil and id == nil then
-- _touchup(x)
-- else
-- x = math.round(x * ratiow)
-- y = math.round(y * ratioh)
-- if id == nil then
-- _touchup(x,y)
-- else
-- _touchup(x,y,id)
-- end
-- end
-- end
-- local _ocrdama2decodeScreen = ocr.dama2.decodeScreen
-- function ocr.dama2.decodeScreen(user,pass,x1,y1,x2,y2,type,timeOut)
-- x1 = math.round(x1 * ratiow)
-- y1 = math.round(y1 * ratioh)
-- x2 = math.round(x2 * ratiow)
-- y2 = math.round(y2 * ratioh)
-- return _ocrdama2decodeScreen(user,pass,x1,y1,x2,y2,type,timeOut)
-- end
-- local _ocrscreen = ocr.screen
-- function ocr.screen(x1,y1,x2,y2,dictPath,lang,option)
-- x1 = math.round(x1 * ratiow)
-- y1 = math.round(y1 * ratioh)
-- x2 = math.round(x2 * ratiow)
-- y2 = math.round(y2 * ratioh)
-- return _ocrscreen(x1,y1,x2,y2,dictPath,lang,option)
-- end
-- local _screencap = screencap
-- function screencap(path,x1,y1,x2,y2)
-- if path == nil then
-- _screencap()
-- elseif x1 == nil then
-- _screencap(path)
-- else
-- x1 = math.round(x1 * ratiow)
-- y1 = math.round(y1 * ratioh)
-- x2 = math.round(x2 * ratiow)
-- y2 = math.round(y2 * ratioh)
-- _screencap(path,x1,y1,x2,y2)
-- end
-- end
-- local _getscreencap = getscreencap
-- function getscreencap(x1,y1,x2,y2)
-- if x1 == nil then
-- return _getscreencap()
-- else
-- x1 = math.round(x1 * ratiow)
-- y1 = math.round(y1 * ratioh)
-- x2 = math.round(x2 * ratiow)
-- y2 = math.round(y2 * ratioh)
-- return _getscreencap(path,x1,y1,x2,y2)
-- end
-- end
-- function setscale(x,y)
-- ratiow,ratioh = x,y
-- -- if wx==480 and hx==854 then
-- -- end
-- -- if wx==480 and hx==800 then
-- -- end
-- end
-------其它重写类方法
local toastDefalut = toast
function toast(text, delay, toast2Pro, isDelay)
-- toast2Pro = toast2Pro or {y = 0.45, x = 0.2}
toast2Pro = toast2Pro or {y = 0.65}
delay = delay or 2200
isDelay = isDelay or false
toastDefalut(text, delay, toast2Pro)
if isDelay then
sleep(delay)
end
end
function click(x,y,delay)
logcat(x..","..y)
delay = delay or 40
delay = math.random(delay - 10, delay)
if math.random(1, 2) == 1 then
touch.down(x + math.random(0, 2), y + math.random(0, 2))
else
touch.down(x - math.random(0, 2), y - math.random(0, 2))
end
sleep(delay)
--sleep(5000)
for i = 1, math.random(0, 2) * 2 do
if i % 2 == 0 then
x = x + math.random(1, 3)
y = y - math.random(1, 3)
else
x = x - math.random(1, 3)
y = y + math.random(1, 3)
end
touch.move(x, y)
sleep(1)
end
touch.up()
return(x..","..y)
end
--------输出函数
function logcat_r(tb)--打印table
if type(tb) ~= "table" then
logcat(tostring(tb))
return
end
local space, deep = string.rep(' ', 4), 0
local function _print(t)
local temp = {}
for k, v in pairs(t) do
local key = tostring(k)
if type(v) == "table" then
deep = deep + 2
logcat(string.format("%s[%s] => Table\n%s(",
string.rep(space, deep - 1),
key,
string.rep(space, deep)
)
) --print.
_print(v)
logcat(string.format("%s)", string.rep(space, deep)))
deep = deep - 2
else
logcat(string.format("%s[%s] => %s",
string.rep(space, deep + 1),
key,
v
)
) --print.
end
end
end
logcat(string.format("Table\n("))
_print(tb)
logcat(string.format(")"))
end
local table.print = logcat_r
function logcat_iniSave(path, title, msg, isCrypt, key)--配置记录
if path == nil then
error("path is nil")
end
-- if msg == nil or title == nil or title == "" or
isCrypt = isCrypt or false
local num, content = xutil.getFileLine(path, title .. "=")
if content ~= nil then
-- xutil.removeFileLine(path, xutil.getFileLine(path, title))
xutil.removeFileLine(path, num)
end
if isCrypt then
msg = security.xxTEAEncrypt(msg, key)
end
msg = string.trim(tostring(msg))
-- local file = io.open(path,"a+")
-- file:write(title .. "=" .. msg.."\n")
-- file:close()
xutil.appendToFile(path, title .. "=" .. msg.."\n")
end
function logcat_iniRead(path, title, isCrypt, key)--配置记录
if path == nil then
error("path is nil")
end
isCrypt = isCrypt or false
local num, content = xutil.getFileLine(path, title .. "=")
if content == nil then return nil end
content = string.sub(content, string.find(content, "=") + 1, content:len())
if isCrypt then
content = security.xxTEADecrypt(content, key)
end
return string.trim(content)
end
----------小工具
--返回指定文本在文件中的行数,失败返回-1,成功返回行号及查找的文本内容
function xutil.getFileLine(path, str, init)
file = io.open(path)
assert(file, "file open failed")
init = init or 1
if init < 1 then init = 1 end
for i = 1, init do
line = file:read()
end
local sum = init
while line do
-- logcat("get line".. sum, line)
if string.find(line, str) ~= nil then
-- logcat("result: " .. line)
return sum, line
-- return line
end
sum = sum + 1
line = file:read()
end
return -1
end
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化