]> git.zerfleddert.de Git - proxmark3-svn/blame_incremental - client/lualibs/utils.lua
FIX: tnp3sim, now can insert keys if the dumpfile is blank. Like the ,,,lander dumps...
[proxmark3-svn] / client / lualibs / utils.lua
... / ...
CommitLineData
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
36 \r
37 ------------ FILE READING\r
38 ReadDumpFile = function (filename)\r
39 \r
40 filename = filename or 'dumpdata.bin'\r
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
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
78 local ascii = utils.ConvertHexToAscii(s)\r
79 local hashed = core.iso14443b_crc(ascii)\r
80 return utils.ConvertAsciiToHex(hashed)\r
81 end\r
82 return nil \r
83 end,\r
84 \r
85 ------------ CRC-16 ccitt checksums\r
86 -- Takes a hex string and calculates a crc16\r
87 Crc16 = 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.crc16(asc)\r
94 return hash\r
95 end\r
96 return nil\r
97 end,\r
98 \r
99 ------------ CRC-64 ecma checksums\r
100 -- Takes a hex string and calculates a crc64 ecma\r
101 Crc64 = 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.crc64(asc)\r
108 return hash\r
109 end\r
110 return nil\r
111 end,\r
112\r
113 ------------ SHA1 hash\r
114 -- Takes a string and calculates a SHA1 hash\r
115 Sha1 = function(s)\r
116 if s == nil then return nil end\r
117 if #s == 0 then return nil end\r
118 if type(s) == 'string' then\r
119 local utils = require('utils')\r
120 --local asc = utils.ConvertHexToAscii(s)\r
121 local hash = core.sha1(s)\r
122 return hash\r
123 end\r
124 return nil\r
125 end, \r
126 -- Takes a hex string and calculates a SHA1 hash\r
127 Sha1Hex = function(s)\r
128 if s == nil then return nil end\r
129 if #s == 0 then return nil end\r
130 if type(s) == 'string' then\r
131 local utils = require('utils')\r
132 local asc = utils.ConvertHexToAscii(s)\r
133 local hash = core.sha1(asc)\r
134 return hash\r
135 end\r
136 return nil\r
137 end, \r
138\r
139 \r
140 -- input parameter is a string\r
141 -- Swaps the endianess and returns a number, \r
142 -- IE: 'cd7a' -> '7acd' -> 0x7acd\r
143 SwapEndianness = function(s, len)\r
144 if s == nil then return nil end\r
145 if #s == 0 then return '' end\r
146 if type(s) ~= 'string' then return nil end\r
147 \r
148 local retval = 0\r
149 if len == 16 then\r
150 local t = s:sub(3,4)..s:sub(1,2)\r
151 retval = tonumber(t,16)\r
152 elseif len == 24 then\r
153 local t = s:sub(5,6)..s:sub(3,4)..s:sub(1,2)\r
154 retval = tonumber(t,16)\r
155 elseif len == 32 then\r
156 local t = s:sub(7,8)..s:sub(5,6)..s:sub(3,4)..s:sub(1,2)\r
157 retval = tonumber(t,16)\r
158 end\r
159 return retval\r
160 end,\r
161 \r
162 -- input parameter is a string\r
163 -- Swaps the endianess and returns a string, \r
164 -- IE: 'cd7a' -> '7acd' -> 0x7acd\r
165 SwapEndiannessStr = function(s, len)\r
166 if s == nil then return nil end\r
167 if #s == 0 then return '' end\r
168 if type(s) ~= 'string' then return nil end\r
169 \r
170 local retval\r
171 if len == 16 then\r
172 retval = s:sub(3,4)..s:sub(1,2)\r
173 elseif len == 24 then\r
174 retval = s:sub(5,6)..s:sub(3,4)..s:sub(1,2)\r
175 elseif len == 32 then\r
176 retval = s:sub(7,8)..s:sub(5,6)..s:sub(3,4)..s:sub(1,2)\r
177 end\r
178 return retval\r
179 end, \r
180 ------------ CONVERSIONS\r
181 \r
182 --\r
183 -- Converts DECIMAL to HEX\r
184 ConvertDecToHex = function(IN)\r
185 local B,K,OUT,I,D=16,"0123456789ABCDEF","",0\r
186 while IN>0 do\r
187 I=I+1\r
188 IN , D = math.floor(IN/B), math.modf(IN,B)+1\r
189 OUT = string.sub(K,D,D)..OUT\r
190 end\r
191 return OUT\r
192 end,\r
193 ---\r
194 -- Convert Byte array to string of hex\r
195 ConvertBytesToHex = function(bytes)\r
196 if bytes == nil then return '' end\r
197 if #bytes == 0 then return '' end\r
198 local s={}\r
199 for i = 1, #bytes do\r
200 s[i] = string.format("%02X",bytes[i]) \r
201 end\r
202 return table.concat(s)\r
203 end, \r
204 -- Convert byte array to string with ascii\r
205 ConvertBytesToAscii = function(bytes)\r
206 if bytes == nil then return '' end\r
207 if #bytes == 0 then return '' end\r
208 local s={}\r
209 for i = 1, #(bytes) do\r
210 s[i] = string.char(bytes[i]) \r
211 end\r
212 return table.concat(s) \r
213 end, \r
214 ConvertHexToBytes = function(s)\r
215 local t={}\r
216 if s == nil then return t end\r
217 if #s == 0 then return t end\r
218 for k in s:gmatch"(%x%x)" do\r
219 table.insert(t,tonumber(k,16))\r
220 end\r
221 return t\r
222 end,\r
223 ConvertAsciiToBytes = function(s, reverse)\r
224 local t = {}\r
225 if s == nil then return t end\r
226 if #s == 0 then return t end\r
227 \r
228 for k in s:gmatch"(.)" do\r
229 table.insert(t, string.byte(k))\r
230 end\r
231 \r
232 if not reverse then\r
233 return t\r
234 end\r
235 \r
236 local rev = {}\r
237 if reverse then\r
238 for i = #t, 1,-1 do\r
239 table.insert(rev, t[i] )\r
240 end\r
241 end\r
242 return rev\r
243 end,\r
244 \r
245 ConvertHexToAscii = function(s)\r
246 if s == nil then return '' end\r
247 if #s == 0 then return '' end\r
248 local t={}\r
249 for k in s:gmatch"(%x%x)" do\r
250 table.insert(t, string.char(tonumber(k,16)))\r
251 end\r
252 return table.concat(t) \r
253 end,\r
254 \r
255 ConvertAsciiToHex = function(s) \r
256 if s == nil then return '' end\r
257 if #s == 0 then return '' end\r
258 local t={}\r
259 for k in s:gmatch"(.)" do\r
260 table.insert(t, string.format("%02X", string.byte(k)))\r
261 end\r
262 return table.concat(t)\r
263 end,\r
264 \r
265 Chars2num = function(s)\r
266 return (s:byte(1)*16777216)+(s:byte(2)*65536)+(s:byte(3)*256)+(s:byte(4))\r
267 end,\r
268 \r
269 -- use length of string to determine 8,16,32,64 bits\r
270 bytes_to_int = function(str,endian,signed) \r
271 local t={str:byte(1,-1)}\r
272 if endian=="big" then --reverse bytes\r
273 local tt={}\r
274 for k=1,#t do\r
275 tt[#t-k+1]=t[k]\r
276 end\r
277 t=tt\r
278 end\r
279 local n=0\r
280 for k=1,#t do\r
281 n=n+t[k]*2^((k-1)*8)\r
282 end\r
283 if signed then\r
284 n = (n > 2^(#t*8-1) -1) and (n - 2^(#t*8)) or n -- if last bit set, negative.\r
285 end\r
286 return n\r
287 end,\r
288 \r
289 -- function convertStringToBytes(str)\r
290 -- local bytes = {}\r
291 -- local strLength = string.len(str)\r
292 -- for i=1,strLength do\r
293 -- table.insert(bytes, string.byte(str, i))\r
294 -- end\r
295\r
296 -- return bytes\r
297-- end\r
298\r
299-- function convertBytesToString(bytes)\r
300 -- local bytesLength = table.getn(bytes)\r
301 -- local str = ""\r
302 -- for i=1,bytesLength do\r
303 -- str = str .. string.char(bytes[i])\r
304 -- end\r
305\r
306 -- return str\r
307-- end\r
308\r
309-- function convertHexStringToBytes(str)\r
310 -- local bytes = {}\r
311 -- local strLength = string.len(str)\r
312 -- for k=2,strLength,2 do\r
313 -- local hexString = "0x" .. string.sub(str, (k - 1), k)\r
314 -- table.insert(bytes, hex.to_dec(hexString))\r
315 -- end\r
316\r
317 -- return bytes\r
318-- end\r
319\r
320-- function convertBytesToHexString(bytes)\r
321 -- local str = ""\r
322 -- local bytesLength = table.getn(bytes)\r
323 -- for i=1,bytesLength do\r
324 -- local hexString = string.sub(hex.to_hex(bytes[i]), 3)\r
325 -- if string.len(hexString) == 1 then\r
326 -- hexString = "0" .. hexString\r
327 -- end\r
328 -- str = str .. hexString\r
329 -- end\r
330\r
331 -- return str\r
332-- end\r
333\r
334}\r
335return Utils
Impressum, Datenschutz