X-Git-Url: https://git.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/blobdiff_plain/ea75b30c81839eb6eb27f083572f03e317ba63d2..1d0ccbe04b6d04cc4e05aeb9bbcb7b7fa0cfdbd1:/client/lualibs/utils.lua?ds=sidebyside diff --git a/client/lualibs/utils.lua b/client/lualibs/utils.lua index 41fb0856..698017d9 100644 --- a/client/lualibs/utils.lua +++ b/client/lualibs/utils.lua @@ -37,9 +37,7 @@ local Utils = ------------ FILE READING ReadDumpFile = function (filename) - if filename == nil then - return nil, 'Filename is empty' - end + filename = filename or 'dumpdata.bin' if #filename == 0 then return nil, 'Filename length is zero' end @@ -71,6 +69,18 @@ local Utils = return outResults end, + ----ISO14443-B CRC + Crc14b = function(s) + if s == nil then return nil end + if #s == 0 then return nil end + if type(s) == 'string' then + local utils = require('utils') + local ascii = utils.ConvertHexToAscii(s) + local hashed = core.iso14443b_crc(ascii) + return utils.ConvertAsciiToHex(hashed) + end + return nil + end, ------------ CRC-16 ccitt checksums -- Takes a hex string and calculates a crc16 @@ -101,7 +111,7 @@ local Utils = end, ------------ SHA1 hash - -- Takes a hex string and calculates a SHA1 hash + -- Takes a string and calculates a SHA1 hash Sha1 = function(s) if s == nil then return nil end if #s == 0 then return nil end @@ -113,6 +123,19 @@ local Utils = end return nil end, + -- Takes a hex string and calculates a SHA1 hash + Sha1Hex = function(s) + if s == nil then return nil end + if #s == 0 then return nil end + if type(s) == 'string' then + local utils = require('utils') + local asc = utils.ConvertHexToAscii(s) + local hash = core.sha1(asc) + return hash + end + return nil + end, + -- input parameter is a string -- Swaps the endianess and returns a number, @@ -170,20 +193,18 @@ local Utils = --- -- Convert Byte array to string of hex ConvertBytesToHex = function(bytes) - if #bytes == 0 then - return '' - end + if bytes == nil then return '' end + if #bytes == 0 then return '' end local s={} - for i = 1, #(bytes) do + for i = 1, #bytes do s[i] = string.format("%02X",bytes[i]) end return table.concat(s) end, -- Convert byte array to string with ascii ConvertBytesToAscii = function(bytes) - if #bytes == 0 then - return '' - end + if bytes == nil then return '' end + if #bytes == 0 then return '' end local s={} for i = 1, #(bytes) do s[i] = string.char(bytes[i]) @@ -222,13 +243,23 @@ local Utils = end, ConvertHexToAscii = function(s) + if s == nil then return '' end + if #s == 0 then return '' end local t={} - if s == nil then return t end - if #s == 0 then return t end for k in s:gmatch"(%x%x)" do table.insert(t, string.char(tonumber(k,16))) end - return table.concat(t) + return table.concat(t) + end, + + ConvertAsciiToHex = function(s) + if s == nil then return '' end + if #s == 0 then return '' end + local t={} + for k in s:gmatch"(.)" do + table.insert(t, string.format("%02X", string.byte(k))) + end + return table.concat(t) end, Chars2num = function(s)