]> git.zerfleddert.de Git - proxmark3-svn/blame - client/lualibs/utils.lua
Update README.md
[proxmark3-svn] / client / lualibs / utils.lua
CommitLineData
5b1311fb
MHS
1--[[\r
2 This may be moved to a separate library at some point (Holiman)\r
3--]]\r
4local Utils = \r
5{\r
6 -- Asks the user for Yes or No\r
7 confirm = function(message, ...)\r
8 local answer\r
9 message = message .. " [y/n] ?"\r
10 repeat\r
11 io.write(message)\r
12 io.flush()\r
13 answer=io.read()\r
14 if answer == 'Y' or answer == "y" then\r
15 return true\r
16 elseif answer == 'N' or answer == 'n' then \r
17 return false\r
18 end\r
19 until false\r
20 end,\r
21 ---\r
22 -- Asks the user for input\r
23 input = function (message , default)\r
24 local answer\r
25 if default ~= nil then\r
26 message = message .. " (default: ".. default.. " )"\r
27 end\r
28 message = message .." \n > "\r
29 io.write(message)\r
30 io.flush()\r
31 answer=io.read()\r
32 if answer == '' then answer = default end\r
33\r
34 return answer\r
35 end,\r
f91f0ebb 36 \r
37 ------------ FILE READING\r
38 ReadDumpFile = function (filename)\r
39 \r
553e868f 40 filename = filename or 'dumpdata.bin'\r
f91f0ebb 41 if #filename == 0 then\r
42 return nil, 'Filename length is zero'\r
43 end\r
44\r
45 infile = io.open(filename, "rb")\r
46 if infile == nil then \r
47 return nil, string.format("Could not read file %s",filename)\r
48 end\r
49 local t = infile:read("*all")\r
50 len = string.len(t)\r
51 local _,hex = bin.unpack(("H%d"):format(len),t)\r
52 io.close(infile)\r
53 return hex\r
54 end,\r
55 \r
56 ------------ string split function\r
57 Split = function( inSplitPattern, outResults )\r
58 if not outResults then\r
59 outResults = {}\r
60 end\r
61 local start = 1\r
62 local splitStart, splitEnd = string.find( self, inSplitPattern, start )\r
63 while splitStart do\r
64 table.insert( outResults, string.sub( self, start, splitStart-1 ) )\r
65 start = splitEnd + 1\r
66 splitStart, splitEnd = string.find( self, inSplitPattern, start )\r
67 end\r
68 table.insert( outResults, string.sub( self, start ) )\r
69 return outResults\r
70 end,\r
71 \r
5de79e20 72 ----ISO14443-B CRC\r
73 Crc14b = function(s)\r
74 if s == nil then return nil end\r
75 if #s == 0 then return nil end\r
76 if type(s) == 'string' then\r
77 local utils = require('utils')\r
f6af1cf0 78 return utils.ConvertAsciiToHex(\r
79 core.iso14443b_crc(s)\r
80 )\r
5de79e20 81 end\r
82 return nil \r
83 end,\r
f91f0ebb 84 \r
b9411eba 85 ------------ CRC-8 Legic checksums\r
86 -- Takes a hex string and calculates a crc8\r
a75d63f1 87 Crc8Legic = function(s)\r
88 if s == nil then return nil end\r
89 if #s == 0 then return nil end\r
90 if type(s) == 'string' then\r
91 local utils = require('utils')\r
92 local asc = utils.ConvertHexToAscii(s)\r
93 local hash = core.crc8legic(asc)\r
94 return hash\r
95 end\r
96 return nil\r
97 end,\r
98 \r
04a6113f 99 ------------ CRC-16 ccitt checksums\r
f91f0ebb 100 -- Takes a hex string and calculates a crc16\r
101 Crc16 = function(s)\r
102 if s == nil then return nil end\r
103 if #s == 0 then return nil end\r
104 if type(s) == 'string' then\r
105 local utils = require('utils')\r
106 local asc = utils.ConvertHexToAscii(s)\r
107 local hash = core.crc16(asc)\r
108 return hash\r
109 end\r
110 return nil\r
111 end,\r
04a6113f 112 \r
a75d63f1 113 \r
04a6113f 114 ------------ CRC-64 ecma checksums\r
115 -- Takes a hex string and calculates a crc64 ecma\r
116 Crc64 = function(s)\r
117 if s == nil then return nil end\r
118 if #s == 0 then return nil end\r
119 if type(s) == 'string' then\r
120 local utils = require('utils')\r
121 local asc = utils.ConvertHexToAscii(s)\r
122 local hash = core.crc64(asc)\r
123 return hash\r
124 end\r
125 return nil\r
126 end,\r
ea75b30c 127\r
128 ------------ SHA1 hash\r
b18948fd 129 -- Takes a string and calculates a SHA1 hash\r
ea75b30c 130 Sha1 = function(s)\r
131 if s == nil then return nil end\r
132 if #s == 0 then return nil end\r
133 if type(s) == 'string' then\r
134 local utils = require('utils')\r
135 --local asc = utils.ConvertHexToAscii(s)\r
136 local hash = core.sha1(s)\r
137 return hash\r
138 end\r
139 return nil\r
140 end, \r
b18948fd 141 -- Takes a hex string and calculates a SHA1 hash\r
142 Sha1Hex = function(s)\r
143 if s == nil then return nil end\r
144 if #s == 0 then return nil end\r
145 if type(s) == 'string' then\r
146 local utils = require('utils')\r
147 local asc = utils.ConvertHexToAscii(s)\r
148 local hash = core.sha1(asc)\r
149 return hash\r
150 end\r
151 return nil\r
152 end, \r
153\r
04a6113f 154 \r
f91f0ebb 155 -- input parameter is a string\r
156 -- Swaps the endianess and returns a number, \r
157 -- IE: 'cd7a' -> '7acd' -> 0x7acd\r
158 SwapEndianness = function(s, len)\r
159 if s == nil then return nil end\r
160 if #s == 0 then return '' end\r
161 if type(s) ~= 'string' then return nil end\r
162 \r
163 local retval = 0\r
164 if len == 16 then\r
165 local t = s:sub(3,4)..s:sub(1,2)\r
166 retval = tonumber(t,16)\r
167 elseif len == 24 then\r
168 local t = s:sub(5,6)..s:sub(3,4)..s:sub(1,2)\r
169 retval = tonumber(t,16)\r
170 elseif len == 32 then\r
171 local t = s:sub(7,8)..s:sub(5,6)..s:sub(3,4)..s:sub(1,2)\r
172 retval = tonumber(t,16)\r
173 end\r
174 return retval\r
175 end,\r
176 \r
5149e37e 177 -- input parameter is a string\r
178 -- Swaps the endianess and returns a string, \r
179 -- IE: 'cd7a' -> '7acd' -> 0x7acd\r
180 SwapEndiannessStr = function(s, len)\r
181 if s == nil then return nil end\r
182 if #s == 0 then return '' end\r
183 if type(s) ~= 'string' then return nil end\r
184 \r
185 local retval\r
186 if len == 16 then\r
187 retval = s:sub(3,4)..s:sub(1,2)\r
188 elseif len == 24 then\r
189 retval = s:sub(5,6)..s:sub(3,4)..s:sub(1,2)\r
190 elseif len == 32 then\r
191 retval = s:sub(7,8)..s:sub(5,6)..s:sub(3,4)..s:sub(1,2)\r
192 end\r
193 return retval\r
194 end, \r
f91f0ebb 195 ------------ CONVERSIONS\r
196 \r
5b1311fb
MHS
197 --\r
198 -- Converts DECIMAL to HEX\r
f91f0ebb 199 ConvertDecToHex = function(IN)\r
5b1311fb
MHS
200 local B,K,OUT,I,D=16,"0123456789ABCDEF","",0\r
201 while IN>0 do\r
202 I=I+1\r
5149e37e 203 IN , D = math.floor(IN/B), math.modf(IN,B)+1\r
3ac59c7f 204 OUT = string.sub(K,D,D)..OUT\r
5b1311fb
MHS
205 end\r
206 return OUT\r
207 end,\r
208 ---\r
209 -- Convert Byte array to string of hex\r
f91f0ebb 210 ConvertBytesToHex = function(bytes)\r
5de79e20 211 if bytes == nil then return '' end\r
212 if #bytes == 0 then return '' end\r
cd5767d4 213 local s={}\r
5de79e20 214 for i = 1, #bytes do\r
9b989c45 215 s[i] = string.format("%02X",bytes[i]) \r
5b1311fb
MHS
216 end\r
217 return table.concat(s)\r
218 end, \r
cd5767d4 219 -- Convert byte array to string with ascii\r
f91f0ebb 220 ConvertBytesToAscii = function(bytes)\r
5de79e20 221 if bytes == nil then return '' end\r
222 if #bytes == 0 then return '' end\r
cd5767d4 223 local s={}\r
224 for i = 1, #(bytes) do\r
225 s[i] = string.char(bytes[i]) \r
226 end\r
227 return table.concat(s) \r
228 end, \r
f91f0ebb 229 ConvertHexToBytes = function(s)\r
9b989c45 230 local t={}\r
cd5767d4 231 if s == nil then return t end\r
232 if #s == 0 then return t end\r
9b989c45 233 for k in s:gmatch"(%x%x)" do\r
234 table.insert(t,tonumber(k,16))\r
235 end\r
236 return t\r
237 end,\r
04a6113f 238 ConvertAsciiToBytes = function(s, reverse)\r
239 local t = {}\r
cd5767d4 240 if s == nil then return t end\r
241 if #s == 0 then return t end\r
242 \r
243 for k in s:gmatch"(.)" do\r
244 table.insert(t, string.byte(k))\r
245 end\r
04a6113f 246 \r
247 if not reverse then\r
248 return t\r
249 end\r
250 \r
251 local rev = {}\r
252 if reverse then\r
253 for i = #t, 1,-1 do\r
254 table.insert(rev, t[i] )\r
255 end\r
256 end\r
257 return rev\r
cd5767d4 258 end,\r
04a6113f 259 \r
47cbb2d4 260 ConvertHexToAscii = function(s)\r
5de79e20 261 if s == nil then return '' end\r
262 if #s == 0 then return '' end\r
47cbb2d4 263 local t={}\r
47cbb2d4 264 for k in s:gmatch"(%x%x)" do\r
265 table.insert(t, string.char(tonumber(k,16)))\r
266 end\r
5de79e20 267 return table.concat(t) \r
268 end,\r
269 \r
270 ConvertAsciiToHex = function(s) \r
271 if s == nil then return '' end\r
272 if #s == 0 then return '' end\r
273 local t={}\r
274 for k in s:gmatch"(.)" do\r
275 table.insert(t, string.format("%02X", string.byte(k)))\r
276 end\r
277 return table.concat(t)\r
47cbb2d4 278 end,\r
9b989c45 279 \r
3ac59c7f 280 Chars2num = function(s)\r
281 return (s:byte(1)*16777216)+(s:byte(2)*65536)+(s:byte(3)*256)+(s:byte(4))\r
282 end,\r
283 \r
284 -- use length of string to determine 8,16,32,64 bits\r
285 bytes_to_int = function(str,endian,signed) \r
286 local t={str:byte(1,-1)}\r
287 if endian=="big" then --reverse bytes\r
288 local tt={}\r
289 for k=1,#t do\r
290 tt[#t-k+1]=t[k]\r
291 end\r
292 t=tt\r
293 end\r
294 local n=0\r
295 for k=1,#t do\r
296 n=n+t[k]*2^((k-1)*8)\r
297 end\r
298 if signed then\r
299 n = (n > 2^(#t*8-1) -1) and (n - 2^(#t*8)) or n -- if last bit set, negative.\r
300 end\r
301 return n\r
302 end,\r
303 \r
b9411eba 304 -- a simple implementation of a sleep command. Thanks to Mosci\r
305 -- takes number of seconds to sleep\r
306 Sleep = function(n)\r
307 local clock = os.clock\r
308 local t0 = clock()\r
309 while clock() - t0 <= n do end\r
310 return nil \r
311 end,\r
312 \r
9b989c45 313 -- function convertStringToBytes(str)\r
314 -- local bytes = {}\r
315 -- local strLength = string.len(str)\r
316 -- for i=1,strLength do\r
317 -- table.insert(bytes, string.byte(str, i))\r
318 -- end\r
319\r
320 -- return bytes\r
321-- end\r
322\r
323-- function convertBytesToString(bytes)\r
324 -- local bytesLength = table.getn(bytes)\r
325 -- local str = ""\r
326 -- for i=1,bytesLength do\r
327 -- str = str .. string.char(bytes[i])\r
328 -- end\r
329\r
330 -- return str\r
331-- end\r
332\r
333-- function convertHexStringToBytes(str)\r
334 -- local bytes = {}\r
335 -- local strLength = string.len(str)\r
336 -- for k=2,strLength,2 do\r
337 -- local hexString = "0x" .. string.sub(str, (k - 1), k)\r
338 -- table.insert(bytes, hex.to_dec(hexString))\r
339 -- end\r
340\r
341 -- return bytes\r
342-- end\r
343\r
344-- function convertBytesToHexString(bytes)\r
345 -- local str = ""\r
346 -- local bytesLength = table.getn(bytes)\r
347 -- for i=1,bytesLength do\r
348 -- local hexString = string.sub(hex.to_hex(bytes[i]), 3)\r
349 -- if string.len(hexString) == 1 then\r
350 -- hexString = "0" .. hexString\r
351 -- end\r
352 -- str = str .. hexString\r
353 -- end\r
354\r
355 -- return str\r
356-- end\r
357\r
5b1311fb
MHS
358}\r
359return Utils
Impressum, Datenschutz