-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
183 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 // 女性 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters