Jaime Blasco Blog


Lua: LUHN checksum algorithm Lua implementation
Sat, 12 Sep 2009

I have wrote a LUA function that implements the LUHN checksum algorithm (requires bitlib), this algorithm checks that a sequence of digits is a valid credit card number. Here is the code:

local bit = require("bit")
local band, bor, bxor = bit.band, bit.bor, bit.bxor

function checksum(card)
	num = 0
	nDigits = card:len()
	odd = band(nDigits, 1)
	
	for count = 0,nDigits-1 do
		digit = tonumber(string.sub(card, count+1,count+1))
		if (bxor(band(count, 1),odd)) == 0 then
			digit = digit * 2
		end
		
		if digit > 9 then
			digit = digit - 9	
		end
		
		num = num + digit
		
	end
	return ((num % 10) == 0)
	
end

posted at: 19:51 | path: /Lua | permanent link to this entry | 0 comments |



Name:


E-mail:


URL:


Comment:


Categories

/ (34)
    Attacks/ (2)
    Exploits/ (1)
    General/ (3)
    Lua/ (1)
    Malware/ (3)
    Nessus/ (6)
        cisco/ (1)
        plugins/ (3)
    Ossim/ (9)
    Scada Security/ (2)
    Security Visualization/ (6)
        Malware/ (2)
    Vulnerability Management/ (1)



Jaime Blasco
(feel free to get in touch)
  • Mail
  • Linkedin
  • Twitter
  • Linkedin
  • Forums

Friend's blogs:
  • /blog/dk
  • /blog/juanma
  • /blog/santiago
  • /blog/pablo/




RSS




Lecture...





< September 2009 >
MoTuWeThFrSaSu
  1 2 3 4 5 6
7 8 910111213
14151617181920
21222324252627
282930    




Archives

2010-Aug
2010-Jul
2010-Mar
2010-Jan
2009-Dec
2009-Oct
2009-Sep
2009-Jul
2009-Jun
2009-Apr
2009-Mar
2009-Feb
2009-Jan
2008-Oct
2008-Aug




Tags




Made with PyBlosxom