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:
@@ -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.
|
Adapter to be installed, which was a source of problems for some users.
|
||||||
[Daniel Miller]
|
[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
|
o [NSE][GH#1838] Scripts dhcp-discover and broadcast-dhcp-discover now support
|
||||||
new argument "mac" to force a specific client MAC address [nnposter]
|
new argument "mac" to force a specific client MAC address [nnposter]
|
||||||
|
|
||||||
|
|||||||
@@ -145,26 +145,19 @@ local function ip(matched_ip)
|
|||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
-- from rosettacode. A function to validate credit card numbers.
|
-- A function to validate credit card numbers.
|
||||||
local function luhn(matched_ccno)
|
local function luhn(matched_ccno)
|
||||||
matched_ccno = matched_ccno:gsub("-", "")
|
local ccno = matched_ccno:gsub("%D", ""):reverse()
|
||||||
matched_ccno = matched_ccno:gsub(" ", "")
|
local sum = 0
|
||||||
local n = string.reverse(matched_ccno)
|
for i = 1, #ccno do
|
||||||
local s1 = 0
|
local d = tonumber(ccno:sub(i,i))
|
||||||
for i=1, n:len(), 2 do
|
if i % 2 == 0 then
|
||||||
s1 = s1 + tonumber(n:sub(i,i))
|
local dd = 2 * d
|
||||||
|
d = dd // 10 + dd % 10
|
||||||
end
|
end
|
||||||
local s2 = 0
|
sum = sum + d
|
||||||
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
|
|
||||||
end
|
end
|
||||||
local total = s1 + s2
|
return sum % 10 == 0
|
||||||
if total%10 == 0 then
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
return false
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- A function to validate ssn numbers.
|
-- A function to validate ssn numbers.
|
||||||
|
|||||||
Reference in New Issue
Block a user