]> git.zerfleddert.de Git - proxmark3-svn/blame - client/lualibs/utils.lua
ADD: SHA1 hashes calculations in sha1.c and LUA
[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
40 if filename == nil then \r
41 return nil, 'Filename is empty'\r
42 end\r
43 if #filename == 0 then\r
44 return nil, 'Filename length is zero'\r
45 end\r
46\r
47 infile = io.open(filename, "rb")\r
48 if infile == nil then \r
49 return nil, string.format("Could not read file %s",filename)\r
50 end\r
51 local t = infile:read("*all")\r
52 len = string.len(t)\r
53 local _,hex = bin.unpack(("H%d"):format(len),t)\r
54 io.close(infile)\r
55 return hex\r
56 end,\r
57 \r
58 ------------ string split function\r
59 Split = function( inSplitPattern, outResults )\r
60 if not outResults then\r
61 outResults = {}\r
62 end\r
63 local start = 1\r
64 local splitStart, splitEnd = string.find( self, inSplitPattern, start )\r
65 while splitStart do\r
66 table.insert( outResults, string.sub( self, start, splitStart-1 ) )\r
67 start = splitEnd + 1\r
68 splitStart, splitEnd = string.find( self, inSplitPattern, start )\r
69 end\r
70 table.insert( outResults, string.sub( self, start ) )\r
71 return outResults\r
72 end,\r
73 \r
f91f0ebb 74 \r
04a6113f 75 ------------ CRC-16 ccitt checksums\r
f91f0ebb 76 -- Takes a hex string and calculates a crc16\r
77 Crc16 = function(s)\r
78 if s == nil then return nil end\r
79 if #s == 0 then return nil end\r
80 if type(s) == 'string' then\r
81 local utils = require('utils')\r
82 local asc = utils.ConvertHexToAscii(s)\r
83 local hash = core.crc16(asc)\r
84 return hash\r
85 end\r
86 return nil\r
87 end,\r
04a6113f 88 \r
89 ------------ CRC-64 ecma checksums\r
90 -- Takes a hex string and calculates a crc64 ecma\r
91 Crc64 = function(s)\r
92 if s == nil then return nil end\r
93 if #s == 0 then return nil end\r
94 if type(s) == 'string' then\r
95 local utils = require('utils')\r
96 local asc = utils.ConvertHexToAscii(s)\r
97 local hash = core.crc64(asc)\r
98 return hash\r
99 end\r
100 return nil\r
101 end,\r
ea75b30c 102\r
103 ------------ SHA1 hash\r
104 -- Takes a hex string and calculates a SHA1 hash\r
105 Sha1 = function(s)\r
106 if s == nil then return nil end\r
107 if #s == 0 then return nil end\r
108 if type(s) == 'string' then\r
109 local utils = require('utils')\r
110 --local asc = utils.ConvertHexToAscii(s)\r
111 local hash = core.sha1(s)\r
112 return hash\r
113 end\r
114 return nil\r
115 end, \r
04a6113f 116 \r
f91f0ebb 117 -- input parameter is a string\r
118 -- Swaps the endianess and returns a number, \r
119 -- IE: 'cd7a' -> '7acd' -> 0x7acd\r
120 SwapEndianness = function(s, len)\r
121 if s == nil then return nil end\r
122 if #s == 0 then return '' end\r
123 if type(s) ~= 'string' then return nil end\r
124 \r
125 local retval = 0\r
126 if len == 16 then\r
127 local t = s:sub(3,4)..s:sub(1,2)\r
128 retval = tonumber(t,16)\r
129 elseif len == 24 then\r
130 local t = s:sub(5,6)..s:sub(3,4)..s:sub(1,2)\r
131 retval = tonumber(t,16)\r
132 elseif len == 32 then\r
133 local t = s:sub(7,8)..s:sub(5,6)..s:sub(3,4)..s:sub(1,2)\r
134 retval = tonumber(t,16)\r
135 end\r
136 return retval\r
137 end,\r
138 \r
5149e37e 139 -- input parameter is a string\r
140 -- Swaps the endianess and returns a string, \r
141 -- IE: 'cd7a' -> '7acd' -> 0x7acd\r
142 SwapEndiannessStr = function(s, len)\r
143 if s == nil then return nil end\r
144 if #s == 0 then return '' end\r
145 if type(s) ~= 'string' then return nil end\r
146 \r
147 local retval\r
148 if len == 16 then\r
149 retval = s:sub(3,4)..s:sub(1,2)\r
150 elseif len == 24 then\r
151 retval = s:sub(5,6)..s:sub(3,4)..s:sub(1,2)\r
152 elseif len == 32 then\r
153 retval = s:sub(7,8)..s:sub(5,6)..s:sub(3,4)..s:sub(1,2)\r
154 end\r
155 return retval\r
156 end, \r
f91f0ebb 157 ------------ CONVERSIONS\r
158 \r
5b1311fb
MHS
159 --\r
160 -- Converts DECIMAL to HEX\r
f91f0ebb 161 ConvertDecToHex = function(IN)\r
5b1311fb
MHS
162 local B,K,OUT,I,D=16,"0123456789ABCDEF","",0\r
163 while IN>0 do\r
164 I=I+1\r
5149e37e 165 IN , D = math.floor(IN/B), math.modf(IN,B)+1\r
3ac59c7f 166 OUT = string.sub(K,D,D)..OUT\r
5b1311fb
MHS
167 end\r
168 return OUT\r
169 end,\r
170 ---\r
171 -- Convert Byte array to string of hex\r
f91f0ebb 172 ConvertBytesToHex = function(bytes)\r
cd5767d4 173 if #bytes == 0 then\r
174 return ''\r
175 end\r
176 local s={}\r
5b1311fb 177 for i = 1, #(bytes) do\r
9b989c45 178 s[i] = string.format("%02X",bytes[i]) \r
5b1311fb
MHS
179 end\r
180 return table.concat(s)\r
181 end, \r
cd5767d4 182 -- Convert byte array to string with ascii\r
f91f0ebb 183 ConvertBytesToAscii = function(bytes)\r
cd5767d4 184 if #bytes == 0 then\r
185 return ''\r
186 end\r
187 local s={}\r
188 for i = 1, #(bytes) do\r
189 s[i] = string.char(bytes[i]) \r
190 end\r
191 return table.concat(s) \r
192 end, \r
f91f0ebb 193 ConvertHexToBytes = function(s)\r
9b989c45 194 local t={}\r
cd5767d4 195 if s == nil then return t end\r
196 if #s == 0 then return t end\r
9b989c45 197 for k in s:gmatch"(%x%x)" do\r
198 table.insert(t,tonumber(k,16))\r
199 end\r
200 return t\r
201 end,\r
04a6113f 202 ConvertAsciiToBytes = function(s, reverse)\r
203 local t = {}\r
cd5767d4 204 if s == nil then return t end\r
205 if #s == 0 then return t end\r
206 \r
207 for k in s:gmatch"(.)" do\r
208 table.insert(t, string.byte(k))\r
209 end\r
04a6113f 210 \r
211 if not reverse then\r
212 return t\r
213 end\r
214 \r
215 local rev = {}\r
216 if reverse then\r
217 for i = #t, 1,-1 do\r
218 table.insert(rev, t[i] )\r
219 end\r
220 end\r
221 return rev\r
cd5767d4 222 end,\r
04a6113f 223 \r
47cbb2d4 224 ConvertHexToAscii = function(s)\r
225 local t={}\r
226 if s == nil then return t end\r
227 if #s == 0 then return t end\r
228 for k in s:gmatch"(%x%x)" do\r
229 table.insert(t, string.char(tonumber(k,16)))\r
230 end\r
231 return table.concat(t) \r
232 end,\r
9b989c45 233 \r
3ac59c7f 234 Chars2num = function(s)\r
235 return (s:byte(1)*16777216)+(s:byte(2)*65536)+(s:byte(3)*256)+(s:byte(4))\r
236 end,\r
237 \r
238 -- use length of string to determine 8,16,32,64 bits\r
239 bytes_to_int = function(str,endian,signed) \r
240 local t={str:byte(1,-1)}\r
241 if endian=="big" then --reverse bytes\r
242 local tt={}\r
243 for k=1,#t do\r
244 tt[#t-k+1]=t[k]\r
245 end\r
246 t=tt\r
247 end\r
248 local n=0\r
249 for k=1,#t do\r
250 n=n+t[k]*2^((k-1)*8)\r
251 end\r
252 if signed then\r
253 n = (n > 2^(#t*8-1) -1) and (n - 2^(#t*8)) or n -- if last bit set, negative.\r
254 end\r
255 return n\r
256 end,\r
257 \r
9b989c45 258 -- function convertStringToBytes(str)\r
259 -- local bytes = {}\r
260 -- local strLength = string.len(str)\r
261 -- for i=1,strLength do\r
262 -- table.insert(bytes, string.byte(str, i))\r
263 -- end\r
264\r
265 -- return bytes\r
266-- end\r
267\r
268-- function convertBytesToString(bytes)\r
269 -- local bytesLength = table.getn(bytes)\r
270 -- local str = ""\r
271 -- for i=1,bytesLength do\r
272 -- str = str .. string.char(bytes[i])\r
273 -- end\r
274\r
275 -- return str\r
276-- end\r
277\r
278-- function convertHexStringToBytes(str)\r
279 -- local bytes = {}\r
280 -- local strLength = string.len(str)\r
281 -- for k=2,strLength,2 do\r
282 -- local hexString = "0x" .. string.sub(str, (k - 1), k)\r
283 -- table.insert(bytes, hex.to_dec(hexString))\r
284 -- end\r
285\r
286 -- return bytes\r
287-- end\r
288\r
289-- function convertBytesToHexString(bytes)\r
290 -- local str = ""\r
291 -- local bytesLength = table.getn(bytes)\r
292 -- for i=1,bytesLength do\r
293 -- local hexString = string.sub(hex.to_hex(bytes[i]), 3)\r
294 -- if string.len(hexString) == 1 then\r
295 -- hexString = "0" .. hexString\r
296 -- end\r
297 -- str = str .. hexString\r
298 -- end\r
299\r
300 -- return str\r
301-- end\r
302\r
5b1311fb
MHS
303}\r
304return Utils
Impressum, Datenschutz