X-Git-Url: https://git.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/blobdiff_plain/b915fda392487a876ccc7b0c8b79a1b31ca5e398..d8ecc98a8eeebb814c28cd2eb4e21e53a59f6c4f:/client/lualibs/utils.lua

diff --git a/client/lualibs/utils.lua b/client/lualibs/utils.lua
index e84f70ad..592d0477 100644
--- a/client/lualibs/utils.lua
+++ b/client/lualibs/utils.lua
@@ -71,8 +71,8 @@ local Utils =
 		return outResults
 	end,
 	
-	------------ CRC-16 ccitt checksums
 	
+	------------ CRC-16 ccitt checksums
 	-- Takes a hex string and calculates a crc16
 	Crc16 = function(s)
 		if s == nil then return nil end
@@ -85,7 +85,48 @@ local Utils =
 		end
 		return nil
 	end,
+	
+	------------ CRC-64 ecma checksums
+	-- Takes a hex string and calculates a crc64 ecma
+	Crc64 = 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.crc64(asc)
+			return hash
+		end
+		return nil
+	end,
 
+	------------ 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
+		if  type(s) == 'string' then
+			local utils = require('utils')
+			--local asc = utils.ConvertHexToAscii(s)
+			local hash = core.sha1(s)
+			return hash
+		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,  
 	-- IE:  'cd7a' -> '7acd'  -> 0x7acd
@@ -108,6 +149,24 @@ local Utils =
 		return retval
 	end,
 	
+	-- input parameter is a string
+	-- Swaps the endianess and returns a string,  
+	-- IE:  'cd7a' -> '7acd'  -> 0x7acd
+	SwapEndiannessStr = function(s, len)
+		if s == nil then return nil end
+		if #s == 0 then return '' end
+		if  type(s) ~= 'string' then return nil end
+		
+		local retval
+		if len == 16 then
+			retval = s:sub(3,4)..s:sub(1,2)
+		elseif len == 24 then
+			retval = s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
+		elseif len == 32 then
+			retval = s:sub(7,8)..s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
+		end
+		return retval
+	end,	
 	------------ CONVERSIONS
 	
 	--
@@ -116,8 +175,8 @@ local Utils =
 		local B,K,OUT,I,D=16,"0123456789ABCDEF","",0
 		while IN>0 do
 			I=I+1
-			IN,D=math.floor(IN/B),math.mod(IN,B)+1
-			OUT=string.sub(K,D,D)..OUT
+			IN , D = math.floor(IN/B), math.modf(IN,B)+1
+			OUT = string.sub(K,D,D)..OUT
 		end
 		return OUT
 	end,
@@ -129,7 +188,7 @@ local Utils =
 		end
 		local s={}
 		for i = 1, #(bytes) do
-			s[i] =   string.format("%02X",bytes[i]) 
+			s[i] = string.format("%02X",bytes[i]) 
 		end
 		return table.concat(s)
 	end,	
@@ -153,16 +212,28 @@ local Utils =
 		end
 		return t
 	end,
-	ConvertAsciiToBytes = function(s)
-		local t={}
+	ConvertAsciiToBytes = function(s, reverse)
+		local t = {}
 		if s == nil then return t end
 		if #s == 0 then return t end
 		
 		for k in s:gmatch"(.)" do
 			table.insert(t, string.byte(k))
 		end
-		return t
+		
+		if not reverse then
+			return t
+		end
+	
+		local rev = {}
+		if reverse then
+			for i = #t, 1,-1 do
+				table.insert(rev, t[i] )
+			end
+		end
+		return rev
 	end,
+	
 	ConvertHexToAscii = function(s)
 		local t={}
 		if s == nil then return t end
@@ -173,6 +244,30 @@ local Utils =
 		return  table.concat(t)	
 	end,
 	
+	Chars2num = function(s)
+        return (s:byte(1)*16777216)+(s:byte(2)*65536)+(s:byte(3)*256)+(s:byte(4))
+	end,
+	
+	-- use length of string to determine 8,16,32,64 bits
+	bytes_to_int = function(str,endian,signed) 
+		local t={str:byte(1,-1)}
+		if endian=="big" then --reverse bytes
+			local tt={}
+			for k=1,#t do
+				tt[#t-k+1]=t[k]
+			end
+			t=tt
+		end
+		local n=0
+		for k=1,#t do
+			n=n+t[k]*2^((k-1)*8)
+		end
+		if signed then
+			n = (n > 2^(#t*8-1) -1) and (n - 2^(#t*8)) or n -- if last bit set, negative.
+		end
+		return n
+	end,
+	
 	-- function convertStringToBytes(str)
 	-- local bytes = {}
 	-- local strLength = string.len(str)
@@ -219,4 +314,4 @@ local Utils =
 -- end
 
 }
-return Utils
\ No newline at end of file
+return Utils