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