-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathluhnValidation.cs
83 lines (69 loc) · 2.42 KB
/
luhnValidation.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using Xunit;
namespace luhnValidation
{
public class luhnValidation
{
[Theory]
[InlineData("4111 1111 1111 1111", true)]
[InlineData("4ab1asd1as1 1asd111 11gfd11 1111", true)]
[InlineData("123456489", false)]
[InlineData("17893729974", true)]
[InlineData("5610591081018250", false)]
[InlineData("abcdefghijk", false)]
public void TestIsValidLuhn(string cardNumber, bool expected)
{
bool result = isValidLuhn(cardNumber);
Assert.Equal(expected, result);
}
static public bool isValidLuhn(string? cardNumber) {
return isValidLuhn(cardNumber, null);
}
static public bool isValidLuhn(string? cardNumber,ILogger? logger)
{
// do not accept empty input
if (string.IsNullOrWhiteSpace(cardNumber))
{
return false;
}
//trim spaces on both ends
cardNumber = cardNumber.Trim();
//luhn method
//code based on the examples on wikipedia.
int sum = 0;
bool isDoublingValue = true;
int checkDigit = 0;
for (int i = cardNumber.Length - 1; i >= 0; i--)
{
//return false if non-digit character is found
if (!char.IsDigit(cardNumber[i]))
{
logger?.LogWarning("inappropriate input found: " + cardNumber[i]);
continue;
}
int n = int.Parse(cardNumber[i].ToString());
//the last digit is used for validation.
if (i == cardNumber.Length - 1)
{
checkDigit = n;
continue;
}
if (isDoublingValue)
{
n *= 2;
//if greater than 10 use the sum of two digits
//since value range is 0~18
//simplify this by -9
if (n > 9)
{
n -= 9;
}
}
sum += n;
isDoublingValue = !isDoublingValue;
}
//compare given check digit and the check digit by calculation
//pass if results match
return (checkDigit == (10 - (sum % 10)));
}
}
}