-
Notifications
You must be signed in to change notification settings - Fork 152
/
CreditCardNumberGenerator.cs
284 lines (232 loc) · 11.6 KB
/
CreditCardNumberGenerator.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
using System;
using System.Collections.Generic;
using System.Linq;
namespace CreditCardNumberGenerator
{
public static class RandomCreditCardNumberGenerator
{
/*
Copy Kev Hunter's changes August 16, 2009 from from https://kevhunter.wordpress.com/2009/08/16/creating-fake-credit-card-numbers/
Include comments from Zoltan siaynoq(http://en.gravatar.com/siaynoq) April 20, 2011
Included in GitHub by MNF 31 May 2016.
MNF 31 May 2016 Added PrefixAndLength struct and methods to generate random numbers of different types in the same call to GetCreditCardNumbers
This is a port of the port of of the Javascript credit card number generator now in C#
* by Kev Hunter https://kevhunter.wordpress.com
* See the license below. Obviously, this is not a Javascript credit card number
generator. However, The following class is a port of a Javascript credit card
number generator.
@author robweber
Javascript credit card number generator Copyright (C) 2006 Graham King
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
www.darkcoding.net
*/
/*
Example of use:
var random = new Random();
var cardsList = random.GetCreditCardNumbers(RandomCreditCardNumberGenerator.BuildPrefixAndLengthArrayForVisaMasterCardAmex(), 10);
*/
public static string[] AMEX_PREFIX_LIST = new[] {"34", "37"};
public static string[] DINERS_PREFIX_LIST = new[]
{
"300",
"301", "302", "303", "36", "38"
};
public static string[] DISCOVER_PREFIX_LIST = new[] {"6011"};
public static string[] ENROUTE_PREFIX_LIST = new[]
{
"2014",
"2149"
};
public static String[] JCB_PREFIX_LIST = new[]
{
"35"
};
public static string[] MASTERCARD_PREFIX_LIST = new[]
{
"51",
"52", "53", "54", "55",
"2221",
"2222",
"2223",
"2224",
"2225",
"2226",
"2227",
"2228",
"2229",
"223",
"224",
"225",
"226",
"227",
"228",
"229",
"23",
"24",
"25",
"26",
"270",
"271",
"2720"
};
public static string[] VISA_PREFIX_LIST = new[]
{
"4539",
"4556", "4916", "4532", "4929", "40240071", "4485", "4716", "4"
};
public static string[] VOYAGER_PREFIX_LIST = new[] {"8699"};
public struct PrefixAndLength
{
public PrefixAndLength(string prefix, int length)
{
Prefix = prefix;
Length = length;
}
public string Prefix { get; set; }
public int Length { get; set; }
}
public static IEnumerable<PrefixAndLength> BuildPrefixAndLengthList(string[] prefixList, int length)
{
var list=from p in prefixList select new PrefixAndLength(p, length);
return list;
}
/// <summary>
/// This is an example how BuildPrefixAndLengthList can be used
/// </summary>
/// <returns></returns>
public static PrefixAndLength[] BuildPrefixAndLengthArrayForVisaMasterCardAmex()
{
var list=BuildPrefixAndLengthList(VISA_PREFIX_LIST, 16)
.Union(BuildPrefixAndLengthList(MASTERCARD_PREFIX_LIST, 16))
.Union(BuildPrefixAndLengthList(AMEX_PREFIX_LIST, 15))
;
return list.ToArray();
}
/// <summary>
/// Better to use extension overload with [this Random random] paramenter. See http://stackoverflow.com/questions/2706500/how-do-i-generate-a-random-int-number-in-c
/// </summary>
/// <param name="prefixAndLengthList"></param>
/// <param name="howMany"></param>
/// <returns></returns>
public static IEnumerable<string> GetCreditCardNumbers(PrefixAndLength[] prefixAndLengthList,
int howMany)
{
Random rndGen = new Random();
return GetCreditCardNumbers(rndGen, prefixAndLengthList, howMany);
}
public static IEnumerable<string> GetCreditCardNumbers(this Random random, PrefixAndLength[] prefixAndLengthList, int howMany)
{
var result = new Stack<string>();
for (int i = 0; i < howMany; i++)
{
int randomPrefix = random.Next(0, prefixAndLengthList.Length - 1);
var prefixAndLength = prefixAndLengthList[randomPrefix];
result.Push(CreateFakeCreditCardNumber(random, prefixAndLength.Prefix, prefixAndLength.Length));
}
return result;
}
/*
'prefix' is the start of the CC number as a string, any number
private of digits 'length' is the length of the CC number to generate.
* Typically 13 or 16
*/
private static string CreateFakeCreditCardNumber(this Random random, string prefix, int length)
{
string ccnumber = prefix;
while (ccnumber.Length < (length - 1))
{
double rnd = (random.NextDouble()*1.0f - 0f);
ccnumber += Math.Floor(rnd*10);
}
// reverse number and convert to int
var reversedCCnumberstring = ccnumber.ToCharArray().Reverse();
var reversedCCnumberList = reversedCCnumberstring.Select(c => Convert.ToInt32(c.ToString()));
// calculate sum //Luhn Algorithm http://en.wikipedia.org/wiki/Luhn_algorithm
int sum = 0;
int pos = 0;
int[] reversedCCnumber = reversedCCnumberList.ToArray();
while (pos < length - 1)
{
int odd = reversedCCnumber[pos]*2;
if (odd > 9)
odd -= 9;
sum += odd;
if (pos != (length - 2))
sum += reversedCCnumber[pos + 1];
pos += 2;
}
// calculate check digit
int checkdigit =
Convert.ToInt32((Math.Floor((decimal) sum/10) + 1)*10 - sum)%10;
ccnumber += checkdigit;
return ccnumber;
}
public static IEnumerable<string> GetCreditCardNumbers(string[] prefixList, int length,
int howMany)
{
var result = new Stack<string>();
var random = new Random();
for (int i = 0; i < howMany; i++)
{
int randomPrefix = random.Next(0, prefixList.Length - 1);
if(randomPrefix>1) //Why??, is it a bug ? it never will select last element
{
randomPrefix--;
}
string ccnumber = prefixList[randomPrefix];
result.Push(CreateFakeCreditCardNumber(random, ccnumber, length));
}
return result;
}
public static IEnumerable<string> GenerateMasterCardNumbers(int howMany)
{
return GetCreditCardNumbers(MASTERCARD_PREFIX_LIST, 16, howMany);
}
public static string GenerateMasterCardNumber()
{
return GetCreditCardNumbers(MASTERCARD_PREFIX_LIST, 16, 1).First();
}
public static bool IsValidCreditCardNumber(string creditCardNumber)
{
try
{
var reversedNumber = creditCardNumber.ToCharArray().Reverse();
int mod10Count = 0;
for (int i = 0; i < reversedNumber.Count(); i++)
{
int augend = Convert.ToInt32(reversedNumber.ElementAt(i).ToString());
if (((i + 1)%2) == 0)
{
string productstring = (augend*2).ToString();
augend = 0;
for (int j = 0; j < productstring.Length; j++)
{
augend += Convert.ToInt32(productstring.ElementAt(j).ToString());
}
}
mod10Count += augend;
}
if ((mod10Count%10) == 0)
{
return true;
}
}
catch
{
return false;
}
return false;
}
}
}