GenerateId.cs
3.13 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
using System;
using System.Security.Cryptography;
namespace Infrastructure
{
public class GenerateId
{
public static string GetGuidHash()
{
return Guid.NewGuid().ToString().GetHashCode().ToString("x");
}
/// <summary>
/// 生成一个长整型,可以转成19字节长的字符串
/// </summary>
/// <returns>System.Int64.</returns>
public static long GenerateLong()
{
byte[] buffer = Guid.NewGuid().ToByteArray();
return BitConverter.ToInt64(buffer, 0);
}
/// <summary>
/// 生成16个字节长度的数据与英文组合串
/// </summary>
public static string GenerateStr()
{
long i = 1;
foreach (byte b in Guid.NewGuid().ToByteArray())
{
i *= ((int)b + 1);
}
return String.Format("{0:x}", i - DateTime.Now.Ticks);
}
/// <summary>
/// 创建11位的英文与数字组合
/// </summary>
/// <returns>System.String.</returns>
public static string ShortStr()
{
return Convert(GenerateLong());
}
/// <summary>
/// 唯一订单号生成
/// </summary>
/// <returns></returns>
public static string GenerateOrderNumber()
{
string strDateTimeNumber = DateTime.Now.ToString("yyyyMMddHHmmssffff");
string strRandomResult = NextRandom(1000, 1).ToString("0000");
return strDateTimeNumber + strRandomResult;
}
#region private
/// <summary>
/// 参考:msdn上的RNGCryptoServiceProvider例子
/// </summary>
/// <param name="numSeeds"></param>
/// <param name="length"></param>
/// <returns></returns>
private static int NextRandom(int numSeeds, int length)
{
// Create a byte array to hold the random value.
byte[] randomNumber = new byte[length];
// Create a new instance of the RNGCryptoServiceProvider.
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
// Fill the array with a random value.
rng.GetBytes(randomNumber);
// Convert the byte to an uint value to make the modulus operation easier.
uint randomResult = 0x0;
for (int i = 0; i < length; i++)
{
randomResult |= ((uint) randomNumber[i] << ((length - 1 - i)*8));
}
return (int) (randomResult%numSeeds) + 1;
}
static string Seq = "s9LFkgy5RovixI1aOf8UhdY3r4DMplQZJXPqebE0WSjBn7wVzmN2Gc6THCAKut";
/// <summary>
/// 10进制转换为62进制
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
private static string Convert(long id)
{
if (id < 62)
{
return Seq[(int) id].ToString();
}
int y = (int) (id%62);
long x = (long) (id/62);
return Convert(x) + Seq[y];
}
#endregion
}
}