diff --git a/lib/Common/CheckMethod.cs b/lib/Common/CheckMethod.cs
new file mode 100644
index 0000000..a45613f
--- /dev/null
+++ b/lib/Common/CheckMethod.cs
@@ -0,0 +1,87 @@
+namespace FakeSharp.Common
+{
+ public class CheckMethod
+ {
+ ///
+ /// Luhn校验,用于银行卡号、IMEI、社会保险等场合
+ ///
+ /// 待校验的数字
+ ///
+ public static int Luhn(long n)
+ {
+ var sum = 0L;
+ var isDoubleBit = true;
+
+ while (n > 0)
+ {
+ if (isDoubleBit) { sum += SelfSum((n % 10) * 2); }
+ else { sum += n % 10; }
+
+ isDoubleBit = !isDoubleBit;
+ n /= 10;
+ }
+
+ var res = sum % 10;
+ if (res != 0) { res = 10 - res; }
+
+ return (int)res;
+ }
+
+
+ ///
+ /// 身份证号校验
+ ///
+ /// 身份证号码(不含校验码)
+ ///
+ public static char IDCardCheck(string number)
+ {
+ if(number.Length != 17)
+ {
+ return 'E';
+ }
+ else
+ {
+ var sum = (number[0] - 48) * 7 + (number[1] - 48) * 9 + (number[2] - 48) * 10 +
+ (number[3] - 48) * 5 + (number[4] - 48) * 8 + (number[5] - 48) * 4 +
+ (number[6] - 48) * 2 + (number[7] - 48) * 1 + (number[8] - 48) * 6 +
+ (number[9] - 48) * 3 + (number[10] - 48) * 7 + (number[11] - 48) * 9 +
+ (number[12] - 48) * 10 + (number[13] - 48) * 5 + (number[14] - 48) * 8 +
+ (number[15] - 48) * 4 + (number[16] - 48) * 2;
+ return (sum % 11) switch
+ {
+ 0 => '1',
+ 1 => '0',
+ 2 => 'X',
+ 3 => '9',
+ 4 => '8',
+ 5 => '7',
+ 6 => '6',
+ 7 => '5',
+ 8 => '4',
+ 9 => '3',
+ 10 => '2',
+ _ => 'E'
+ };
+ }
+ }
+
+
+ ///
+ /// 求整数中每位数字之和
+ ///
+ ///
+ ///
+ static long SelfSum(long n)
+ {
+ var sum = n % 10;
+ var rest = n;
+
+ while ((rest /= 10) > 0)
+ {
+ sum += rest % 10;
+ }
+
+ return sum;
+ }
+ }
+}
diff --git a/lib/CreditCard.cs b/lib/CreditCard.cs
index 712a242..c9d8b3a 100644
--- a/lib/CreditCard.cs
+++ b/lib/CreditCard.cs
@@ -1,4 +1,6 @@
-namespace FakeSharp
+using FakeSharp.Common;
+
+namespace FakeSharp
{
public class CreditCard
{
@@ -7,17 +9,17 @@ public class CreditCard
///
public enum BinType
{
- Any = 0,
- GongShang = 623062,
- ZhongGuo = 621343,
- JianShe = 622676,
- ZhaoShang = 410062,
- ZhongXin = 433680,
- GuangDa = 622663,
- MinSheng = 622622,
- JiaoTong = 621335,
- PingAn = 622989,
- NongYe = 622848
+ Any = 0, // 任意
+ GongShang = 623062, // 工商银行
+ ZhongGuo = 621343, // 中国银行
+ JianShe = 622676, // 建设银行
+ ZhaoShang = 410062, // 招商银行
+ ZhongXin = 433680, // 中信银行
+ GuangDa = 622663, // 光大银行
+ MinSheng = 622622, // 民生银行
+ JiaoTong = 621335, // 交通银行
+ PingAn = 622989, // 平安银行
+ NongYe = 622848 // 农业银行
}
@@ -43,31 +45,8 @@ public static IEnumerable Generate(BinType binType = BinType.Any, int le
// 添加中间位
fakeCardNumber += (long)(fakeCardNumber * Math.Pow(10, length - 7)) + r.Next((int)Math.Pow(10, length - 8), (int)(0.9 * Math.Pow(10, length - 7)));
- // 计算校验位
- var sum = 0L;
- var isDoubleBit = true;
-
- var fakeCardNumberCopy = fakeCardNumber;
- while (fakeCardNumberCopy > 0)
- {
- if (isDoubleBit)
- {
- sum += SelfSum((fakeCardNumberCopy % 10) * 2);
- }
- else
- {
- sum += fakeCardNumberCopy % 10;
- }
-
- isDoubleBit = !isDoubleBit;
- fakeCardNumberCopy /= 10;
- }
-
- var res = sum % 10;
- if (res != 0) { res = 10 - res; }
-
// 构造银行卡号
- fakeCardNumbers[i] = $"{fakeCardNumber}{res}";
+ fakeCardNumbers[i] = $"{fakeCardNumber}{CheckMethod.Luhn(fakeCardNumber)}";
}
return fakeCardNumbers;
@@ -97,24 +76,5 @@ static BinType GenerateRandomBinCode(Random? r = null)
_ => BinType.Any
};
}
-
-
- ///
- /// 求整数中每位数字之和
- ///
- ///
- ///
- static long SelfSum(long n)
- {
- var sum = n % 10;
- var rest = n;
-
- while ((rest /= 10) > 0)
- {
- sum += rest % 10;
- }
-
- return sum;
- }
}
}
\ No newline at end of file
diff --git a/lib/FakeSharp.csproj b/lib/FakeSharp.csproj
index 3de4d9e..edc5150 100644
--- a/lib/FakeSharp.csproj
+++ b/lib/FakeSharp.csproj
@@ -16,9 +16,9 @@
True
data-generation
MIT
- ✨ 新增银行卡号生成器
+ ✨ 新增身份证号生成器
伪数据生成器,用于构造手机号、银行卡号、身份证号等数据
- 1.0.3
+ 1.0.4
diff --git a/lib/IdentifyCard.cs b/lib/IdentifyCard.cs
index 6e61311..74855db 100644
--- a/lib/IdentifyCard.cs
+++ b/lib/IdentifyCard.cs
@@ -1,9 +1,67 @@
-namespace FakeSharp
+using FakeSharp.Common;
+
+namespace FakeSharp
{
public class IdentifyCard
{
-
+ static readonly string[] PROVINCE_CODE = { "11", "12", "13", "14", "15", "21", "22", "23", "31", "32", "33", "34", "35", "36",
+ "37", "42", "42", "43", "44", "45", "46", "50", "51", "52", "53", "54", "61", "62",
+ "63", "64", "65", "71", "81", "82" };
+ static readonly string[] PREFECTURE_CODE = { "01", "02", "03", "04", "05", "06", "07", "08", "09", "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", "90" };
+
+ ///
+ /// 生成身份证号
+ ///
+ /// 起始日期
+ /// 终止日期
+ /// 性别
+ /// 生成数量
+ ///
+ public static IEnumerable Generate(DateTime? dateStart = null, DateTime? dateEnd = null, Gender gender = Gender.Any, int count = 100)
+ {
+ var IDCardNumber = "";
+ var fakeIDCardNumbers = new string[count];
+ var random = new Random(DateTime.Now.Second * 1000 + DateTime.Now.Millisecond);
+
+ // 检查输入参数
+ if(dateStart == null) { dateStart = new DateTime(1949, 10, 1); }
+ if(dateEnd == null) { dateEnd = DateTime.Now; }
+ if(dateStart > dateEnd) { dateStart = dateEnd; }
+
+ for(int i = 0; i < count; i++)
+ {
+ IDCardNumber = $"{PROVINCE_CODE[random.Next(PROVINCE_CODE.Length)]}{PREFECTURE_CODE[random.Next(PREFECTURE_CODE.Length)]}{random.Next(100):D2}{random.Next(dateStart.Value.Year, dateEnd.Value.Year + 1)}{random.Next(13):D2}{random.Next(29):D2}{random.Next(100):D2}";
+
+ // 添加性别位
+ _ = gender switch
+ {
+ Gender.Any => IDCardNumber += $"{random.Next(10)}",
+ Gender.Male => IDCardNumber += $"{random.Next(5) * 2 + 1}",
+ Gender.Female => IDCardNumber += $"{random.Next(5) * 2}",
+ _ => ""
+ };
+
+ // 添加校验位
+ fakeIDCardNumbers[i] = $"{IDCardNumber}{CheckMethod.IDCardCheck(IDCardNumber)}";
+ }
+
+ return fakeIDCardNumbers;
+ }
+ ///
+ /// 性别
+ ///
+ public enum Gender
+ {
+ Any = 0, // 任意
+ Male = 1, // 男性
+ Female = 2 // 女性
+ }
}
}
diff --git a/test/Program.cs b/test/Program.cs
index d2e9796..81a9b95 100644
--- a/test/Program.cs
+++ b/test/Program.cs
@@ -11,6 +11,9 @@ static void Main(string[] args)
// 随机生成10个银行卡号,限制Bin码为建设银行
GenerateCreditCardNumbers();
+
+ // 随机生成10个身份证号
+ GenerateIDCardNumbers();
}
///
@@ -33,7 +36,7 @@ static void GenerateMobileNumber()
///
static void GenerateCreditCardNumbers()
{
- var fakeCreditCardNumbers = CreditCard.Generate(CreditCard.BinType.JianShe, count: 10).ToArray();
+ var fakeCreditCardNumbers = CreditCard.Generate(CreditCard.BinType.Any, count: 10).ToArray();
Console.WriteLine("########## 银行卡号 ##########");
for (int i = 0; i < fakeCreditCardNumbers.Length; i++)
@@ -41,5 +44,20 @@ static void GenerateCreditCardNumbers()
Console.WriteLine($"[{i}]{fakeCreditCardNumbers[i]}");
}
}
+
+
+ ///
+ /// 随机生成身份证号
+ ///
+ static void GenerateIDCardNumbers()
+ {
+ var fakeIDCardNumbers = IdentifyCard.Generate(count: 10).ToArray();
+
+ Console.WriteLine("########## 身份证号 ##########");
+ for (int i = 0; i < fakeIDCardNumbers.Length; i++)
+ {
+ Console.WriteLine($"[{i}]{fakeIDCardNumbers[i]}");
+ }
+ }
}
}
\ No newline at end of file