Skip to content

Commit

Permalink
✨ 新增银行卡号生成器
Browse files Browse the repository at this point in the history
  • Loading branch information
Redns committed Jun 7, 2022
1 parent 647c0f3 commit c35a0ec
Show file tree
Hide file tree
Showing 15 changed files with 259 additions and 99 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -351,4 +351,5 @@ MigrationBackup/

.vs/
bin/
obj/
obj/

7 changes: 0 additions & 7 deletions CreditCard.cs

This file was deleted.

12 changes: 0 additions & 12 deletions IdentifyCard.cs

This file was deleted.

76 changes: 0 additions & 76 deletions MobileNumberGenerator.cs

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# FakeData
# FakeSharp
120 changes: 120 additions & 0 deletions lib/CreditCard.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
namespace FakeSharp
{
public class CreditCard
{
/// <summary>
/// Bin码类型
/// </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
}


/// <summary>
/// 生成银行卡号
/// </summary>
/// <param name="binType">Bin码类型</param>
/// <param name="length">银行卡号长度</param>
/// <param name="count">生成数量</param>
/// <returns></returns>
public static IEnumerable<string> Generate(BinType binType = BinType.Any, int length = 16, int count = 100)
{
var fakeCardNumber = 0L;
var fakeCardNumbers = new string[count];
var r = new Random(DateTime.Now.Second + DateTime.Now.Millisecond);

for (int i = 0; i < count; i++)
{
// 生成bin码
if (binType == BinType.Any) { fakeCardNumber = (int)GenerateRandomBinCode(r); }
else { fakeCardNumber = (int)binType; }

// 添加中间位
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}";
}

return fakeCardNumbers;
}


/// <summary>
/// 随机生成Bin码
/// </summary>
/// <param name="r"></param>
/// <returns></returns>
static BinType GenerateRandomBinCode(Random? r = null)
{
if (r == null) { r = new Random(DateTime.Now.Second + DateTime.Now.Millisecond); }
return r.Next(10) switch
{
0 => BinType.GongShang,
1 => BinType.ZhongGuo,
2 => BinType.JianShe,
3 => BinType.ZhaoShang,
4 => BinType.ZhongXin,
5 => BinType.GuangDa,
6 => BinType.MinSheng,
7 => BinType.JiaoTong,
8 => BinType.PingAn,
9 => BinType.NongYe,
_ => 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;
}
}
}
File renamed without changes.
8 changes: 7 additions & 1 deletion FakeData.sln → lib/FakeData.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.2.32526.322
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FakeSharp", "FakeSharp.csproj", "{8BE5F1C0-3A80-4978-83D5-441EB17F54A0}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FakeSharp", "FakeSharp.csproj", "{8BE5F1C0-3A80-4978-83D5-441EB17F54A0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FakeSharpTest", "..\test\FakeSharpTest.csproj", "{E097DBC2-CE50-4546-AB73-E053A8B4D4ED}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -15,6 +17,10 @@ Global
{8BE5F1C0-3A80-4978-83D5-441EB17F54A0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8BE5F1C0-3A80-4978-83D5-441EB17F54A0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8BE5F1C0-3A80-4978-83D5-441EB17F54A0}.Release|Any CPU.Build.0 = Release|Any CPU
{E097DBC2-CE50-4546-AB73-E053A8B4D4ED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E097DBC2-CE50-4546-AB73-E053A8B4D4ED}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E097DBC2-CE50-4546-AB73-E053A8B4D4ED}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E097DBC2-CE50-4546-AB73-E053A8B4D4ED}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
10 changes: 9 additions & 1 deletion FakeSharp.csproj → lib/FakeSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
<PackageTags>data-generation</PackageTags>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageReleaseNotes>✨ 新增手机号码生成器</PackageReleaseNotes>
<PackageReleaseNotes>✨ 新增银行卡号生成器</PackageReleaseNotes>
<Description>伪数据生成器,用于构造手机号、银行卡号、身份证号等数据</Description>
<Version>1.0.3</Version>
</PropertyGroup>

<ItemGroup>
Expand All @@ -28,6 +29,13 @@
<Content Include="logo.ico" />
</ItemGroup>

<ItemGroup>
<None Include="..\README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>

<ItemGroup>
<Resource Include="LICENSE.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand Down
9 changes: 9 additions & 0 deletions lib/IdentifyCard.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace FakeSharp
{
public class IdentifyCard
{



}
}
50 changes: 50 additions & 0 deletions lib/MobileNumber.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
namespace FakeSharp
{
/// <summary>
/// 手机号码生成器
/// </summary>
public class MobileNumber
{
static readonly int[] MAC_CHINA_MOBILE = { 134, 135, 136, 137, 138, 139, 150, 151, 152, 157, 158, 159, 182, 187, 188};
static readonly int[] MAC_CHINA_UNICOM = { 130, 131, 132, 155, 156, 182, 185, 186 };
static readonly int[] MAC_CHINA_TELECOM = { 133, 153, 180, 189 };
static readonly int[] MAC_CHINA_ALL = { 134, 135, 136, 137, 138, 139, 150, 151, 152, 157, 158, 159, 182, 187, 188, 130, 131, 132, 155, 156, 182, 185, 186, 133, 153, 180, 189 };


/// <summary>
/// 生成手机号码
/// </summary>
/// <param name="macConfig">移动接入码</param>
/// <param name="count">生成数量</param>
/// <returns></returns>
public static IEnumerable<string> Generate(MAC macConfig = MAC.Any, int count = 100)
{
var r = new Random(DateTime.Now.Second + DateTime.Now.Millisecond);
var fakeNumbers = new string[count];
for(int i = 0; i < count; i++)
{
int mac = macConfig switch
{
MAC.ChinaMobile => MAC_CHINA_MOBILE[r.Next(MAC_CHINA_MOBILE.Length)],
MAC.ChinaUnicom => MAC_CHINA_UNICOM[r.Next(MAC_CHINA_UNICOM.Length)],
MAC.ChinaTelecom => MAC_CHINA_TELECOM[r.Next(MAC_CHINA_TELECOM.Length)],
_ => MAC_CHINA_ALL[r.Next(MAC_CHINA_ALL.Length)],
};
fakeNumbers[i] = $"{mac}{r.Next(10000000, 89999999)}";
}
return fakeNumbers;
}


/// <summary>
/// 移动接入码(移动、联通、电信)
/// </summary>
public enum MAC
{
Any = 0, // 任意
ChinaMobile, // 中国移动
ChinaUnicom, // 中国联通
ChinaTelecom // 中国电信
}
}
}
File renamed without changes.
File renamed without changes
16 changes: 16 additions & 0 deletions test/FakeSharpTest.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<Reference Include="FakeSharp">
<HintPath>..\lib\bin\Release\net6.0\FakeSharp.dll</HintPath>
</Reference>
</ItemGroup>

</Project>
Loading

0 comments on commit c35a0ec

Please sign in to comment.