chg: adaptation.. made shortcut for Hexlify for ConvertAsciiToHex

This commit is contained in:
iceman1001 2018-07-27 09:36:47 +02:00
parent 6d4d58659f
commit 2919a35665

View file

@ -10,7 +10,7 @@ local Utils =
repeat repeat
io.write(message) io.write(message)
io.flush() io.flush()
answer=io.read() answer = io.read()
if answer == 'Y' or answer == "y" then if answer == 'Y' or answer == "y" then
return true return true
elseif answer == 'N' or answer == 'n' then elseif answer == 'N' or answer == 'n' then
@ -28,7 +28,7 @@ local Utils =
message = message .." \n > " message = message .." \n > "
io.write(message) io.write(message)
io.flush() io.flush()
answer=io.read() answer = io.read()
if answer == '' then answer = default end if answer == '' then answer = default end
return answer return answer
@ -47,9 +47,9 @@ local Utils =
return nil, string.format("Could not read file %s",filename) return nil, string.format("Could not read file %s",filename)
end end
local t = infile:read("*all") local t = infile:read("*all")
io.close(infile)
len = string.len(t) len = string.len(t)
local _,hex = bin.unpack(("H%d"):format(len),t) local _,hex = bin.unpack(("H%d"):format(len),t)
io.close(infile)
return hex return hex
end, end,
@ -172,10 +172,7 @@ local Utils =
if s == nil then return nil end if s == nil then return nil end
if #s == 0 then return nil end if #s == 0 then return nil end
if type(s) == 'string' then if type(s) == 'string' then
local utils = require('utils') return core.sha1(s)
--local asc = utils.ConvertHexToAscii(s)
local hash = core.sha1(s)
return hash
end end
return nil return nil
end, end,
@ -238,11 +235,11 @@ local Utils =
-- --
-- Converts DECIMAL to HEX -- Converts DECIMAL to HEX
ConvertDecToHex = function(IN) ConvertDecToHex = function(IN)
local B,K,OUT,I,D=16,"0123456789ABCDEF","",0 local B,K,OUT,I,D = 16, "0123456789ABCDEF", "", 0
while IN>0 do while IN > 0 do
I=I+1 I = I+1
IN , D = math.floor(IN/B), math.modf(IN,B)+1 IN, D = math.floor(IN/B), math.modf(IN, B) + 1
OUT = string.sub(K,D,D)..OUT OUT = string.sub(K, D, D)..OUT
end end
return OUT return OUT
end, end,
@ -326,23 +323,28 @@ local Utils =
return table.concat(t) return table.concat(t)
end, end,
hexlify = function(s)
local u = require('utils')
return u.ConvertAsciiToHex(s)
end,
Chars2num = function(s) Chars2num = function(s)
return (s:byte(1)*16777216)+(s:byte(2)*65536)+(s:byte(3)*256)+(s:byte(4)) return (s:byte(1)*16777216)+(s:byte(2)*65536)+(s:byte(3)*256)+(s:byte(4))
end, end,
-- use length of string to determine 8,16,32,64 bits -- use length of string to determine 8,16,32,64 bits
bytes_to_int = function(str,endian,signed) bytes_to_int = function(str,endian,signed)
local t={str:byte(1,-1)} local t = {str:byte(1, -1)}
if endian=="big" then --reverse bytes if endian == "big" then --reverse bytes
local tt={} local tt = {}
for k=1,#t do for k = 1, #t do
tt[#t-k+1]=t[k] tt[#t-k+1] = t[k]
end end
t=tt t = tt
end end
local n=0 local n = 0
for k=1,#t do for k = 1, #t do
n=n+t[k]*2^((k-1)*8) n = n + t[k] * 2^((k-1) * 8)
end end
if signed then if signed then
n = (n > 2^(#t*8-1) -1) and (n - 2^(#t*8)) or n -- if last bit set, negative. n = (n > 2^(#t*8-1) -1) and (n - 2^(#t*8)) or n -- if last bit set, negative.