![]() |
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 |
|
Categories
/ (34) Jaime Blasco (feel free to get in touch) Friend's blogs:
Lecture...
Archives
2010-Aug Tags
| ![]() | ||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
![]() |




