]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - 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
index c5baa406955d067012d525e9b57c8bf9cc525a42..698017d923d28de50a5baa044d7eb51510b1699a 100644 (file)
@@ -37,9 +37,7 @@ local Utils =
        ------------ FILE READING\r
        ReadDumpFile = function (filename)\r
        \r
-               if filename == nil then \r
-                       return nil, 'Filename is empty'\r
-               end\r
+               filename = filename or 'dumpdata.bin'\r
                if #filename == 0 then\r
                        return nil, 'Filename length is zero'\r
                end\r
@@ -71,8 +69,20 @@ local Utils =
                return outResults\r
        end,\r
        \r
-       ------------ CRC-16 ccitt checksums\r
+       ----ISO14443-B CRC\r
+       Crc14b = function(s)\r
+               if s == nil then return nil end\r
+               if #s == 0 then return nil end\r
+               if  type(s) == 'string' then\r
+                       local utils = require('utils')\r
+                       local ascii = utils.ConvertHexToAscii(s)\r
+                       local hashed = core.iso14443b_crc(ascii)\r
+                       return utils.ConvertAsciiToHex(hashed)\r
+               end\r
+               return nil              \r
+       end,\r
        \r
+       ------------ CRC-16 ccitt checksums\r
        -- Takes a hex string and calculates a crc16\r
        Crc16 = function(s)\r
                if s == nil then return nil end\r
@@ -85,7 +95,48 @@ local Utils =
                end\r
                return nil\r
        end,\r
+       \r
+       ------------ CRC-64 ecma checksums\r
+       -- Takes a hex string and calculates a crc64 ecma\r
+       Crc64 = function(s)\r
+               if s == nil then return nil end\r
+               if #s == 0 then return nil end\r
+               if  type(s) == 'string' then\r
+                       local utils = require('utils')\r
+                       local asc = utils.ConvertHexToAscii(s)\r
+                       local hash = core.crc64(asc)\r
+                       return hash\r
+               end\r
+               return nil\r
+       end,\r
+\r
+       ------------ SHA1 hash\r
+       -- Takes a string and calculates a SHA1 hash\r
+       Sha1 = function(s)\r
+               if s == nil then return nil end\r
+               if #s == 0 then return nil end\r
+               if  type(s) == 'string' then\r
+                       local utils = require('utils')\r
+                       --local asc = utils.ConvertHexToAscii(s)\r
+                       local hash = core.sha1(s)\r
+                       return hash\r
+               end\r
+               return nil\r
+       end,    \r
+       -- Takes a hex string and calculates a SHA1 hash\r
+       Sha1Hex = function(s)\r
+               if s == nil then return nil end\r
+               if #s == 0 then return nil end\r
+               if  type(s) == 'string' then\r
+                       local utils = require('utils')\r
+                       local asc = utils.ConvertHexToAscii(s)\r
+                       local hash = core.sha1(asc)\r
+                       return hash\r
+               end\r
+               return nil\r
+       end,    \r
 \r
+       \r
        -- input parameter is a string\r
        -- Swaps the endianess and returns a number,  \r
        -- IE:  'cd7a' -> '7acd'  -> 0x7acd\r
@@ -135,27 +186,25 @@ local Utils =
                while IN>0 do\r
                        I=I+1\r
                        IN , D = math.floor(IN/B), math.modf(IN,B)+1\r
-                       OUT=string.sub(K,D,D)..OUT\r
+                       OUT = string.sub(K,D,D)..OUT\r
                end\r
                return OUT\r
        end,\r
        ---\r
        -- Convert Byte array to string of hex\r
        ConvertBytesToHex = function(bytes)\r
-               if #bytes == 0 then\r
-                       return ''\r
-               end\r
+               if bytes == nil then return '' end\r
+               if #bytes == 0 then return '' end\r
                local s={}\r
-               for i = 1, #(bytes) do\r
-                       s[i] =   string.format("%02X",bytes[i]) \r
+               for i = 1, #bytes do\r
+                       s[i] = string.format("%02X",bytes[i]) \r
                end\r
                return table.concat(s)\r
        end,    \r
        -- Convert byte array to string with ascii\r
     ConvertBytesToAscii = function(bytes)\r
-               if #bytes == 0 then\r
-                       return ''\r
-               end\r
+               if bytes == nil then return '' end\r
+               if #bytes == 0 then return '' end\r
                local s={}\r
                for i = 1, #(bytes) do\r
                        s[i] = string.char(bytes[i]) \r
@@ -171,24 +220,70 @@ local Utils =
                end\r
                return t\r
        end,\r
-       ConvertAsciiToBytes = function(s)\r
-               local t={}\r
+       ConvertAsciiToBytes = function(s, reverse)\r
+               local t = {}\r
                if s == nil then return t end\r
                if #s == 0 then return t end\r
                \r
                for k in s:gmatch"(.)" do\r
                        table.insert(t, string.byte(k))\r
                end\r
-               return t\r
+               \r
+               if not reverse then\r
+                       return t\r
+               end\r
+       \r
+               local rev = {}\r
+               if reverse then\r
+                       for i = #t, 1,-1 do\r
+                               table.insert(rev, t[i] )\r
+                       end\r
+               end\r
+               return rev\r
        end,\r
+       \r
        ConvertHexToAscii = function(s)\r
+               if s == nil then return '' end\r
+               if #s == 0 then return '' end\r
                local t={}\r
-               if s == nil then return t end\r
-               if #s == 0 then return t end\r
                for k in s:gmatch"(%x%x)" do\r
                        table.insert(t, string.char(tonumber(k,16)))\r
                end\r
-               return  table.concat(t) \r
+               return table.concat(t)  \r
+       end,\r
+       \r
+       ConvertAsciiToHex = function(s)         \r
+               if s == nil then return '' end\r
+               if #s == 0 then return '' end\r
+               local t={}\r
+               for k in s:gmatch"(.)" do\r
+                       table.insert(t, string.format("%02X", string.byte(k)))\r
+               end\r
+               return table.concat(t)\r
+       end,\r
+       \r
+       Chars2num = function(s)\r
+        return (s:byte(1)*16777216)+(s:byte(2)*65536)+(s:byte(3)*256)+(s:byte(4))\r
+       end,\r
+       \r
+       -- use length of string to determine 8,16,32,64 bits\r
+       bytes_to_int = function(str,endian,signed) \r
+               local t={str:byte(1,-1)}\r
+               if endian=="big" then --reverse bytes\r
+                       local tt={}\r
+                       for k=1,#t do\r
+                               tt[#t-k+1]=t[k]\r
+                       end\r
+                       t=tt\r
+               end\r
+               local n=0\r
+               for k=1,#t do\r
+                       n=n+t[k]*2^((k-1)*8)\r
+               end\r
+               if signed then\r
+                       n = (n > 2^(#t*8-1) -1) and (n - 2^(#t*8)) or n -- if last bit set, negative.\r
+               end\r
+               return n\r
        end,\r
        \r
        -- function convertStringToBytes(str)\r
Impressum, Datenschutz