Skip to content

Commit

Permalink
✨ 新增身份证号生成器
Browse files Browse the repository at this point in the history
  • Loading branch information
Redns committed Jun 8, 2022
1 parent c35a0ec commit 253a120
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 60 deletions.
87 changes: 87 additions & 0 deletions lib/Common/CheckMethod.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
namespace FakeSharp.Common
{
public class CheckMethod
{
/// <summary>
/// Luhn校验,用于银行卡号、IMEI、社会保险等场合
/// </summary>
/// <param name="n">待校验的数字</param>
/// <returns></returns>
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;
}


/// <summary>
/// 身份证号校验
/// </summary>
/// <param name="n">身份证号码(不含校验码)</param>
/// <returns></returns>
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'
};
}
}


/// <summary>
/// 求整数中每位数字之和
/// </summary>
/// <param name="n"></param>
/// <returns></returns>
static long SelfSum(long n)
{
var sum = n % 10;
var rest = n;

while ((rest /= 10) > 0)
{
sum += rest % 10;
}

return sum;
}
}
}
70 changes: 15 additions & 55 deletions lib/CreditCard.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace FakeSharp
using FakeSharp.Common;

namespace FakeSharp
{
public class CreditCard
{
Expand All @@ -7,17 +9,17 @@ public class CreditCard
/// </summary>
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 // 农业银行
}


Expand All @@ -43,31 +45,8 @@ public static IEnumerable<string> 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;
Expand Down Expand Up @@ -97,24 +76,5 @@ static BinType GenerateRandomBinCode(Random? r = null)
_ => BinType.Any
};
}


/// <summary>
/// 求整数中每位数字之和
/// </summary>
/// <param name="n"></param>
/// <returns></returns>
static long SelfSum(long n)
{
var sum = n % 10;
var rest = n;

while ((rest /= 10) > 0)
{
sum += rest % 10;
}

return sum;
}
}
}
4 changes: 2 additions & 2 deletions lib/FakeSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
<PackageTags>data-generation</PackageTags>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageReleaseNotes>✨ 新增银行卡号生成器</PackageReleaseNotes>
<PackageReleaseNotes>✨ 新增身份证号生成器</PackageReleaseNotes>
<Description>伪数据生成器,用于构造手机号、银行卡号、身份证号等数据</Description>
<Version>1.0.3</Version>
<Version>1.0.4</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
62 changes: 60 additions & 2 deletions lib/IdentifyCard.cs
Original file line number Diff line number Diff line change
@@ -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" };

/// <summary>
/// 生成身份证号
/// </summary>
/// <param name="dateStart">起始日期</param>
/// <param name="dateEnd">终止日期</param>
/// <param name="gender">性别</param>
/// <param name="count">生成数量</param>
/// <returns></returns>
public static IEnumerable<string> 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;
}


/// <summary>
/// 性别
/// </summary>
public enum Gender
{
Any = 0, // 任意
Male = 1, // 男性
Female = 2 // 女性
}
}
}
20 changes: 19 additions & 1 deletion test/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ static void Main(string[] args)

// 随机生成10个银行卡号,限制Bin码为建设银行
GenerateCreditCardNumbers();

// 随机生成10个身份证号
GenerateIDCardNumbers();
}

/// <summary>
Expand All @@ -33,13 +36,28 @@ static void GenerateMobileNumber()
/// </summary>
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++)
{
Console.WriteLine($"[{i}]{fakeCreditCardNumbers[i]}");
}
}


/// <summary>
/// 随机生成身份证号
/// </summary>
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]}");
}
}
}
}

0 comments on commit 253a120

Please sign in to comment.