proxmark3/client/luascripts/data_hex_crc.lua

97 lines
2.6 KiB
Lua
Raw Normal View History

local getopt = require('getopt')
local utils = require('utils')
2020-04-05 16:05:14 +08:00
local ansicolors = require('ansicolors')
2019-05-08 05:37:15 +08:00
copyright = ''
author = 'Iceman'
2020-04-05 16:05:14 +08:00
version = 'v1.0.2'
2019-05-08 05:37:15 +08:00
desc = [[
2019-03-09 17:34:43 +08:00
This script calculates many checksums (CRC) over the provided hex input.
2019-05-08 05:37:15 +08:00
]]
example = [[
script run data_hex_crc -b 010203040506070809
script run data_hex_crc -b 010203040506070809 -w 16
]]
2019-05-08 05:37:15 +08:00
usage = [[
script run data_hex_crc [-b <hex bytes] [-w <width>]
2020-04-05 16:05:14 +08:00
]]
arguments = [[
2019-05-08 05:37:15 +08:00
-b data in hex
-w bitwidth of the CRC family of algorithm. <optional> defaults to all known CRC presets.
]]
2019-03-09 17:34:43 +08:00
---
-- A debug printout-function
2019-05-08 05:37:15 +08:00
local function dbg(args)
if not DEBUG then return end
if type(args) == 'table' then
local i = 1
while args[i] do
dbg(args[i])
i = i+1
end
else
print('###', args)
2019-03-09 17:34:43 +08:00
end
end
---
-- This is only meant to be used when errors occur
2019-05-08 05:37:15 +08:00
local function oops(err)
print('ERROR:', err)
core.clearCommandBuffer()
return nil, err
end
2019-03-09 17:34:43 +08:00
---
-- Usage help
2019-05-08 05:37:15 +08:00
local function help()
print(copyright)
print(author)
print(version)
2019-03-09 17:34:43 +08:00
print(desc)
2020-04-05 16:05:14 +08:00
print(ansicolors.cyan..'Usage'..ansicolors.reset)
2019-05-08 05:37:15 +08:00
print(usage)
2020-04-05 16:05:14 +08:00
print(ansicolors.cyan..'Arguments'..ansicolors.reset)
print(arguments)
print(ansicolors.cyan..'Example usage'..ansicolors.reset)
print(example)
end
2019-03-09 17:34:43 +08:00
---
-- The main entry point
function main(args)
2019-03-09 17:34:43 +08:00
local data
local width = 0
-- Read the parameters
for o, a in getopt.getopt(args, 'hb:w:') do
2019-05-08 05:37:15 +08:00
if o == 'h' then return help() end
if o == 'b' then data = a end
if o == 'w' then width = a end
2019-03-09 17:34:43 +08:00
end
data = data or '01020304'
width = width or 0
2019-03-09 17:34:43 +08:00
print( string.rep('-',60) )
print('Bit width of CRC | '..width)
print('Bytes | '..data)
print('')
print( ('%-20s| %-16s| %s'):format('Model','CRC', 'CRC reverse','bigEnd', 'bigEnd','little','little'))
print( string.rep('-',60) )
local lists, err = core.reveng_models(width)
if lists == nil then return oops(err) end
2019-03-09 17:34:43 +08:00
for _,i in pairs(lists) do
if string.len(i) > 1 then
local a1 = core.reveng_runmodel(i, data, false, '0')
local a2 = core.reveng_runmodel(i, data, true, '0')
local a3 = core.reveng_runmodel(i, data, false, 'b')
local a4 = core.reveng_runmodel(i, data, false, 'B')
local a5 = core.reveng_runmodel(i, data, false, 'l')
local a6 = core.reveng_runmodel(i, data, false, 'L')
print( ('%-20s| %-16s| %-16s| %-16s| %-16s| %-16s| %-16s'):format(i, a1:upper(), a2:upper(),a3:upper(),a4:upper(),a5:upper(),a6:upper() ) )
end
end
end
2019-03-12 07:12:26 +08:00
main(args)