EmployeeProfileSensitiveHelper.cs
10.9 KB
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
using System;
using System.Globalization;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using NCC;
using NCC.Common.Extension;
using NCC.DataEncryption;
namespace NCC.Extend
{
/// <summary>
/// 员工档案敏感字段加解密、哈希与脱敏(身份证/银行卡)。
/// AES 密钥读取顺序:EmployeeProfile:SensitiveDataAesKey → JWTSettings:IssuerSigningKey(取前 32 字符)。
/// </summary>
internal static class EmployeeProfileSensitiveHelper
{
/// <summary>字典 certificateType-身份证 F_Id</summary>
internal const string IdCardCertificateTypeId = "a745d425adbb4321880817661cae8910";
internal const int DuplicateSubmitWindowMinutes = 30;
internal const int SameIpSubmitWindowMinutes = 5;
internal static readonly string[] PublicRequiredFields =
{
"realName", "mobilePhone", "gender", "idCardNo", "education",
"currentAddress", "idCardAddress", "idCardFrontUrl", "idCardBackUrl",
"bankName", "bankCardNo", "bankAccountName",
"urgentContacts", "urgentTelePhone"
};
/// <summary>文化程度合法取值白名单</summary>
internal static readonly string[] EducationLevels =
{
"博士", "硕士", "本科", "大专", "中专", "技校", "高中", "初中", "小学"
};
private static readonly string[] FullAddressDetailKeywords =
{
"区", "县", "镇", "乡", "村", "路", "街", "道", "号", "弄", "栋", "单元", "室", "层", "楼", "组", "委", "队"
};
internal static string NormalizeIdCard(string idCard)
{
return string.IsNullOrWhiteSpace(idCard) ? null : idCard.Trim().ToUpperInvariant();
}
internal static string NormalizeMobile(string mobile)
{
return string.IsNullOrWhiteSpace(mobile) ? null : mobile.Trim();
}
internal static string NormalizeBankCard(string bankCard)
{
return string.IsNullOrWhiteSpace(bankCard) ? null : Regex.Replace(bankCard.Trim(), @"\s+", string.Empty);
}
internal static string ComputeIdCardHash(string idCard)
{
var normalized = NormalizeIdCard(idCard);
if (string.IsNullOrEmpty(normalized))
{
return null;
}
using var sha = SHA256.Create();
var bytes = sha.ComputeHash(Encoding.UTF8.GetBytes(normalized));
return BitConverter.ToString(bytes).Replace("-", string.Empty).ToLowerInvariant();
}
internal static string EncryptSensitive(string plain)
{
if (string.IsNullOrWhiteSpace(plain))
{
return null;
}
return AESEncryption.Encrypt(plain, ResolveAesKey());
}
internal static string DecryptSensitive(string cipher)
{
if (string.IsNullOrWhiteSpace(cipher))
{
return null;
}
try
{
return AESEncryption.Decrypt(cipher, ResolveAesKey());
}
catch
{
return null;
}
}
internal static string ResolveAesKey()
{
var configured = App.Configuration["EmployeeProfile:SensitiveDataAesKey"];
if (!string.IsNullOrWhiteSpace(configured))
{
return configured.Trim();
}
var jwtKey = App.Configuration["JWTSettings:IssuerSigningKey"];
if (!string.IsNullOrWhiteSpace(jwtKey))
{
var key = jwtKey.Trim();
return key.Length >= 32 ? key.Substring(0, 32) : key.PadRight(32, '0');
}
throw new InvalidOperationException("未配置 EmployeeProfile:SensitiveDataAesKey 或 JWTSettings:IssuerSigningKey,无法加解密敏感字段");
}
internal static string MaskPhone(string phone)
{
if (string.IsNullOrWhiteSpace(phone) || phone.Length < 7)
{
return "无";
}
return phone.Substring(0, 3) + "****" + phone.Substring(phone.Length - 4);
}
internal static string MaskIdCard(string idCard)
{
if (string.IsNullOrWhiteSpace(idCard) || idCard.Length < 8)
{
return "无";
}
return idCard.Substring(0, 4) + "********" + idCard.Substring(idCard.Length - 4);
}
internal static string MaskBankCard(string bankCard)
{
if (string.IsNullOrWhiteSpace(bankCard) || bankCard.Length < 8)
{
return "无";
}
return "**** **** **** " + bankCard.Substring(bankCard.Length - 4);
}
/// <summary>审核比对:仅展示后 4 位,仍非完整明文。</summary>
internal static string MaskTail4(string value)
{
if (string.IsNullOrWhiteSpace(value) || value.Length < 4)
{
return "无";
}
return "****" + value.Substring(value.Length - 4);
}
internal static int? CalcAgeFromIdCard(string idCard, DateTime? birthdayFallback = null)
{
var birth = ParseBirthdayFromIdCard(idCard) ?? birthdayFallback?.Date;
if (!birth.HasValue)
{
return null;
}
var today = DateTime.Today;
var age = today.Year - birth.Value.Year;
if (birth.Value.Date > today.AddYears(-age))
{
age--;
}
return age < 0 ? (int?)null : age;
}
internal static DateTime? ParseBirthdayFromIdCard(string idCard)
{
var normalized = NormalizeIdCard(idCard);
if (string.IsNullOrEmpty(normalized))
{
return null;
}
if (normalized.Length == 18
&& DateTime.TryParseExact(normalized.Substring(6, 8), "yyyyMMdd", null, DateTimeStyles.None, out var dt18))
{
return dt18.Date;
}
if (normalized.Length == 15
&& DateTime.TryParseExact("19" + normalized.Substring(6, 6), "yyyyMMdd", null, DateTimeStyles.None, out var dt15))
{
return dt15.Date;
}
return null;
}
internal static int? ParseGenderFromIdCard(string idCard)
{
var normalized = NormalizeIdCard(idCard);
if (string.IsNullOrEmpty(normalized))
{
return null;
}
var index = normalized.Length == 18 ? 16 : normalized.Length == 15 ? 14 : -1;
if (index < 0 || !char.IsDigit(normalized[index]))
{
return null;
}
return (normalized[index] - '0') % 2 == 1 ? 1 : 2;
}
internal static string CalcTenureText(DateTime? entryDate)
{
if (!entryDate.HasValue)
{
return "无";
}
var today = DateTime.Today;
if (entryDate.Value.Date > today)
{
return "未入职";
}
var months = (today.Year - entryDate.Value.Year) * 12 + today.Month - entryDate.Value.Month;
if (today.Day < entryDate.Value.Day)
{
months--;
}
if (months < 0)
{
months = 0;
}
var years = months / 12;
var rem = months % 12;
if (years > 0 && rem > 0)
{
return $"{years}年{rem}个月";
}
if (years > 0)
{
return $"{years}年";
}
return $"{rem}个月";
}
internal static bool IsValidMobile(string mobile)
{
return !string.IsNullOrWhiteSpace(mobile) && Regex.IsMatch(mobile.Trim(), @"^1\d{10}$");
}
internal static bool IsValidBankCard(string bankCard)
{
if (string.IsNullOrWhiteSpace(bankCard))
{
return true;
}
var normalized = NormalizeBankCard(bankCard);
return normalized.Length >= 12 && normalized.Length <= 30 && normalized.All(char.IsDigit);
}
internal static bool IsEnumDefined<T>(int? value) where T : struct, Enum
{
return !value.HasValue || Enum.IsDefined(typeof(T), value.Value);
}
internal static bool IsValidEducation(string education)
{
if (string.IsNullOrWhiteSpace(education))
{
return false;
}
var trimmed = education.Trim();
return EducationLevels.Any(x => x == trimmed);
}
internal static bool IsValidFullAddress(string address)
{
if (string.IsNullOrWhiteSpace(address))
{
return false;
}
var trimmed = address.Trim();
if (trimmed.Length < 8)
{
return false;
}
return FullAddressDetailKeywords.Any(trimmed.Contains);
}
internal static bool IsValidHttpUrl(string url)
{
if (string.IsNullOrWhiteSpace(url))
{
return false;
}
return Uri.TryCreate(url.Trim(), UriKind.Absolute, out var uri)
&& (uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps);
}
/// <summary>入职日期 + 试用期月数自动推算转正日期;二者齐全时覆盖手工值。</summary>
internal static DateTime? CalcRegularizationDate(DateTime? entryDate, int? probationMonths)
{
if (!entryDate.HasValue || !probationMonths.HasValue)
{
return null;
}
return entryDate.Value.Date.AddMonths(probationMonths.Value);
}
internal static bool IsSamePhoneAfterNormalize(string phoneA, string phoneB)
{
var a = NormalizeMobile(phoneA);
var b = NormalizeMobile(phoneB);
return !string.IsNullOrEmpty(a) && a == b;
}
internal static string GenerateSubmissionNo()
{
var rnd = new Random(Guid.NewGuid().GetHashCode()).Next(1000, 9999);
return "EP" + DateTime.Now.ToString("yyyyMMddHHmmss") + rnd;
}
internal static (string ip, string ua) ResolveSubmitClient()
{
var ctx = App.HttpContext;
var ip = ctx?.Connection?.RemoteIpAddress?.ToString();
var ua = ctx?.Request?.Headers["User-Agent"].ToString();
if (ua != null && ua.Length > 500)
{
ua = ua.Substring(0, 500);
}
return (ip, ua);
}
}
}