1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-06 04:31:29 +00:00

Streamline Luhn checksum code

Removes various floating-point hazards. Fixes #1931, closes #1932
This commit is contained in:
nnposter
2020-03-01 03:50:42 +00:00
parent 80f4733c32
commit 44d7dbc517
2 changed files with 14 additions and 18 deletions

View File

@@ -42,6 +42,9 @@ o [Windows] Add support for the new loopback behavior in Npcap 0.9983. This
Adapter to be installed, which was a source of problems for some users.
[Daniel Miller]
o [NSE][GH#1931][GH#1932] Script http-grep was not correctly calculating Luhn
checksum [Colleen Li, nnposter]
o [NSE][GH#1838] Scripts dhcp-discover and broadcast-dhcp-discover now support
new argument "mac" to force a specific client MAC address [nnposter]

View File

@@ -145,26 +145,19 @@ local function ip(matched_ip)
return true
end
-- from rosettacode. A function to validate credit card numbers.
-- A function to validate credit card numbers.
local function luhn(matched_ccno)
matched_ccno = matched_ccno:gsub("-", "")
matched_ccno = matched_ccno:gsub(" ", "")
local n = string.reverse(matched_ccno)
local s1 = 0
for i=1, n:len(), 2 do
s1 = s1 + tonumber(n:sub(i,i))
local ccno = matched_ccno:gsub("%D", ""):reverse()
local sum = 0
for i = 1, #ccno do
local d = tonumber(ccno:sub(i,i))
if i % 2 == 0 then
local dd = 2 * d
d = dd // 10 + dd % 10
end
local s2 = 0
for i=2, n:len(), 2 do
local doubled = n:sub(i,i)*2
doubled = string.gsub(doubled,'(%d)(%d)',function(a,b)return a+b end)
s2 = s2+doubled
sum = sum + d
end
local total = s1 + s2
if total%10 == 0 then
return true
end
return false
return sum % 10 == 0
end
-- A function to validate ssn numbers.