markus / ConvertService / ServiceBase / Markus.Service.Extensions / Exntensions / guid.cs @ 218e5002
이력 | 보기 | 이력해설 | 다운로드 (1.65 KB)
1 | 53c9637d | taeseongkim | using System; |
---|---|---|---|
2 | using System.Collections.Generic; |
||
3 | using System.Linq; |
||
4 | using System.Security.Cryptography; |
||
5 | using System.Text; |
||
6 | using System.Threading.Tasks; |
||
7 | |||
8 | namespace Markus.Service.Extensions |
||
9 | { |
||
10 | 218e5002 | semi | /// <summary> |
11 | /// var id = GuidExtension.shortGuid(); |
||
12 | /// </summary> |
||
13 | 53c9637d | taeseongkim | public static class GuidExtension |
14 | { |
||
15 | public static Guid CreateUniqueGuid(this Guid guid) |
||
16 | { |
||
17 | 218e5002 | semi | |
18 | 53c9637d | taeseongkim | return GetUniqueGuid(); |
19 | } |
||
20 | |||
21 | private static Guid GetUniqueGuid() |
||
22 | { |
||
23 | char[] chars = "ABCDEF1234567890".ToCharArray(); |
||
24 | byte[] data = new byte[1]; |
||
25 | System.Security.Cryptography.RNGCryptoServiceProvider crypto = new System.Security.Cryptography.RNGCryptoServiceProvider(); |
||
26 | crypto.GetNonZeroBytes(data); |
||
27 | data = new byte[32]; |
||
28 | crypto.GetNonZeroBytes(data); |
||
29 | StringBuilder result = new StringBuilder(32); |
||
30 | |||
31 | foreach (byte b in data) |
||
32 | { |
||
33 | result.Append(chars[b % (chars.Length - 1)]); |
||
34 | } |
||
35 | |||
36 | return Guid.ParseExact(result.ToString(0, 8) + "-" + result.ToString(8, 4) + "-" + result.ToString(11, 4) + "-" |
||
37 | + result.ToString(16, 4) + "-" + result.ToString(20, 12), "D"); |
||
38 | } |
||
39 | |||
40 | public static string shortGuid() |
||
41 | { |
||
42 | byte[] numArray = new byte[16]; |
||
43 | using (RandomNumberGenerator randomNumberGenerator = RandomNumberGenerator.Create()) |
||
44 | randomNumberGenerator.GetBytes(numArray); |
||
45 | return Convert.ToBase64String(new Guid(numArray).ToByteArray()).Substring(0, 10).Replace("/", "").Replace("+", "") + DateTime.UtcNow.Ticks.ToString("x"); |
||
46 | } |
||
47 | } |
||
48 | } |