add legic crc16 to lua

This commit is contained in:
iceman1001 committed 2021-10-23 23:53:59 +02:00
1 parent 982e669a77
commit 644da79a50
4 files changed
+56 -15

No files matched your search

+22 -6
View File
@@ -122,7 +122,7 @@ local Utils =
return nil
end,
------------ CRC-8 Legic checksums
------------ CRC-8 Legic checksum
-- Takes a hex string and calculates a crc8
Crc8Legic = function(s)
if s == nil then return nil end
@@ -130,13 +130,29 @@ local Utils =
if type(s) == 'string' then
local utils = require('utils')
local asc = utils.ConvertHexToAscii(s)
local hash = core.crc8legic(asc)
return hash
return core.crc8legic(asc)
end
return nil
end,
------------ CRC-16 Legic checksum
-- Takes data as hex string, uid hex and calculates a crc16 legic
Crc16Legic = function(s, uid)
if s == nil then return nil end
if #s == 0 then return nil end
if uid == nil then return nil end
if #uid == 0 then return nil end
if type(s) == 'string' then
local utils = require('utils')
local asc = utils.ConvertHexToAscii(s)
local uidstr = utils.ConvertHexToAscii(uid)
return core.crc16legic(asc, uidstr)
end
return nil
end,
------------ CRC-16 ccitt checksums
------------ CRC-16 ccitt checksum
-- Takes a hex string and calculates a crc16
Crc16 = function(s)
if s == nil then return nil end
@@ -151,7 +167,7 @@ local Utils =
end,
------------ CRC-64 ecma checksums
------------ CRC-64 ecma checksum
-- Takes a hex string and calculates a crc64 ecma hash
Crc64 = function(s)
if s == nil then return nil end
@@ -164,7 +180,7 @@ local Utils =
end
return nil
end,
------------ CRC-64 ecma 182 checksums
------------ CRC-64 ecma 182 checksum
-- Takes a hex string and calculates a crc64 ecma182 hash
Crc64_ecma182 = function(s)
if s == nil then return nil end
+20 -3
View File
@@ -683,13 +683,29 @@ static int l_aes128encrypt_ecb(lua_State *L) {
static int l_crc8legic(lua_State *L) {
size_t size;
const char *p_str = luaL_checklstring(L, 1, &size);
uint16_t retval = CRC8Legic((uint8_t *) p_str, size);
const char *p_hexstr = luaL_checklstring(L, 1, &size);
uint16_t retval = CRC8Legic((uint8_t *)p_hexstr, size);
lua_pushunsigned(L, retval);
return 1;
}
static int l_crc16legic(lua_State *L) {
size_t hexsize, uidsize;
// data as hex string
const char *p_hexstr = luaL_checklstring(L, 1, &hexsize);
// calc uid crc based on uid hex
const char *p_uid = luaL_checklstring(L, 2, &uidsize);
uint16_t uidcrc = CRC8Legic((uint8_t *)p_uid, uidsize);
init_table(CRC_LEGIC_16);
uint16_t retval = crc16_legic((uint8_t *)p_hexstr, hexsize, uidcrc);
lua_pushunsigned(L, retval);
return 1;
}
static int l_crc16(lua_State *L) {
size_t size;
const char *p_str = luaL_checklstring(L, 1, &size);
@@ -1359,6 +1375,7 @@ int set_pm3_libraries(lua_State *L) {
{"aes128_encrypt", l_aes128encrypt_cbc},
{"aes128_encrypt_ecb", l_aes128encrypt_ecb},
{"crc8legic", l_crc8legic},
{"crc16legic", l_crc16legic},
{"crc16", l_crc16},
{"crc64", l_crc64},
{"crc64_ecma182", l_crc64_ecma182},