-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShortId.cs
194 lines (162 loc) · 6.17 KB
/
ShortId.cs
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
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Xml;
namespace SharpStack
{
public class ShortID
{
static HashSet<string> previousNumbers = new HashSet<string>();
static Random rx = new Random();
/// <summary>
/// Generates a unique number of length 12 to 32 based on timestamp be used as a unique id
/// </summary>
/// <param name="length"></param>
/// <returns></returns>
public static string LongUniqueNumber(int length = 12)
{
while (true)
{
// Get the current timestamp
long timestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
int l = length - 4;
//get right 10 characters from string
string strTimestamp = timestamp.ToString();
int startIndex = (strTimestamp.Length > l ? strTimestamp.Length - l : 0);
int endIndex = strTimestamp.Length - startIndex;
strTimestamp = strTimestamp.Substring(startIndex, endIndex);
if (previousNumbers.Count > 99999)
{
previousNumbers.Clear();
}
var prePattern = "00";
var postPattern = "";
if (strTimestamp.Length < (length - 2))
{
for (int i = 0; i < length - strTimestamp.Length - 2; i++)
{
postPattern += "0";
}
}
int postMax = Convert.ToInt32(postPattern.Replace("0", "9"));
string post = rx.Next(00, postMax).ToString(postPattern);
//if (post.Length > postPattern.Length)
//{
// post = post.Substring(post.Length - postPattern.Length, postPattern.Length);
//}
string pre = rx.Next(0, 99).ToString(prePattern);
string shortUniqueId = $"{pre}{strTimestamp}{post}";
if (previousNumbers.Contains(shortUniqueId))
{
//seed++;
continue;
}
else
{
previousNumbers.Add(shortUniqueId);
return shortUniqueId;
}
}
}
/// <summary>
/// Generates 8 Digit Unique Number based on Timestamp to be used as a unique id
/// </summary>
/// <returns></returns>
public static string ShortUniqueNumber()
{
if (previousNumbers.Count > 9999999)
{
previousNumbers.Clear();
}
while (true)
{
// Get the current timestamp
long timestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
//get right 10 characters from string
string strTimestamp = timestamp.ToString().Substring(timestamp.ToString().Length - 6, 6);
string post = rx.Next(00, 9).ToString("0");
string pre = rx.Next(0, 9).ToString("0");
string shortUniqueId = $"{pre}{strTimestamp}{post}";
if (previousNumbers.Contains(shortUniqueId))
{
//seed++;
continue;
}
else
{
previousNumbers.Add(shortUniqueId);
return shortUniqueId;
}
}
}
/// <summary>
/// Generate Unique Alphanumeric String between length 6 to 22 to be used as a unique id
/// </summary>
/// <param name="length">Between 6 to 22</param>
/// <returns></returns>
public static string UniqueID(int length = 22)
{
if (length < 6)
{
length = 6;
}
else if (length > 22)
{
length = 22;
}
if (previousNumbers.Count > 9999999)
{
previousNumbers.Clear();
}
while (true)
{
string uniqueId;
if (length < 15)
uniqueId = GenerateRandomString(length);
else
uniqueId = UniqueFromBase64(length);
if (previousNumbers.Contains(uniqueId))
{
continue;
}
else
{
previousNumbers.Add(uniqueId);
return uniqueId;
}
}
}
private static string UniqueFromBase64(int length)
{
var g = Guid.NewGuid();
var ba = g.ToByteArray();
var base64 = Convert.ToBase64String(ba);
var firsttwentytwo = base64.Substring(0, length);
var uniqueId = firsttwentytwo.Replace("/", "_").Replace("+", "-");
return uniqueId;
}
static string GetChecksum(long unixTimestamp)
{
const string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
// Calculate the remainder when dividing the timestamp by the number of letters
int remainder = (int)(unixTimestamp % letters.Length);
// Use the remainder as an index to get a character from the letters string
char checksum = letters[remainder];
return checksum.ToString();
}
static string GenerateRandomString(int length)
{
long timestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
// get year month day from timestamp
//string strTimestamp = timestamp.ToString().Substring(timestamp.ToString().Length - 10, 10);
string checksum = GetChecksum(timestamp);
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
char[] randomString = new char[length - 1];
for (int i = 0; i < length - 1; i++)
{
randomString[i] = chars[rx.Next(chars.Length)];
}
return checksum + (new string(randomString));
}
}
}