-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathGuidEx.cs
executable file
·222 lines (196 loc) · 6.43 KB
/
GuidEx.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
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
//==========================================================================================
//
// OpenNETCF.GuidEx
// Copyright (C) 2003-2005, OpenNETCF.org
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the OpenNETCF.org Shared Source License.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the OpenNETCF.org Shared Source License
// for more details.
//
// You should have received a copy of the OpenNETCF.org Shared Source License
// along with this library; if not, email [email protected] to request a copy.
//
// If you wish to contact the OpenNETCF Advisory Board to discuss licensing, please
// email [email protected].
//
// For general enquiries, email [email protected] or visit our website at:
// http://www.opennetcf.org
//
// !!! A HUGE thank-you goes out to Casey Chesnut for supplying parts of this code !!!
// !!! You can contact Casey at http://www.brains-n-brawn.com
//
//==========================================================================================
#if(COMPACT_FRAMEWORK)
using System;
using System.Runtime.InteropServices;
//using OpenNETCF.Security.Cryptography;
// New for v1.3 - "The Guid to end all Guids" - Peter Foot
namespace OpenNETCF
{
/// <summary>
/// Helper class for generating a globally unique identifier (GUID).
/// <para><b>Revised in v1.3</b></para>
/// </summary>
/// <seealso cref="System.Guid"/>
public sealed class GuidEx
{
private GuidEx(){}
#region New Guid
/// <summary>
/// Initializes a new instance of the <see cref="System.Guid"/> class.
/// </summary>
/// <returns>A new <see cref="System.Guid"/> object.</returns>
/// <remarks>On CE.NET 4.1 and higher this method uses the CoCreateGuid API call.
/// On CE 3.0 based Pocket PCs it uses the OpenNETCF.Security.Cryptography classes to generate a random Guid.</remarks>
public static System.Guid NewGuid()
{
//cocreateguid supported on CE.NET 4.1 and above (4.0 not supported by .NETCF)
if(System.Environment.OSVersion.Version.Major > 3)
{
return NewOleGuid();
}
else
{
//check if target has crypto API support
// if(OpenNETCF.Security.Cryptography.NativeMethods.Context.IsCryptoApi)
// {
// return NewCryptoGuid();
// }
// else
{
//if not use random generator
return NewRandomGuid();
}
}
}
#endregion
#region Constants
// constants that are used in the class
private class Const
{
// guid variant types
public enum GuidVariant
{
ReservedNCS = 0x00,
Standard = 0x02,
ReservedMicrosoft = 0x06,
ReservedFuture = 0x07
}
// guid version types
public enum GuidVersion
{
TimeBased = 0x01,
Reserved = 0x02,
NameBased = 0x03,
Random = 0x04
}
// multiplex variant info
public const int VariantByte = 8;
public const int VariantByteMask = 0x3f;
public const int VariantByteShift = 6;
// multiplex version info
public const int VersionByte = 7;
public const int VersionByteMask = 0x0f;
public const int VersionByteShift = 4;
}
#endregion
#region Crypto
// /// <summary>
// /// Create a new Random Guid using Crypto APIs
// /// </summary>
// /// <returns></returns>
// public static System.Guid NewCryptoGuid()
// {
// //create guid manually
// byte[] guidbytes = new byte[16];
//
// //use crypto apis to generate random bytes
// OpenNETCF.Security.Cryptography.RNGCryptoServiceProvider rng = new OpenNETCF.Security.Cryptography.RNGCryptoServiceProvider();
// rng.GetBytes(guidbytes);
//
// //set version etc
// MakeValidRandomGuid(guidbytes);
//
// // create the new System.Guid object
// return new System.Guid(guidbytes);
// }
#endregion
#region Random
/// <summary>
/// Create a new Random Guid (For platforms without Crypto support).
/// </summary>
/// <returns></returns>
public static System.Guid NewRandomGuid()
{
byte[] guidbytes = new byte[16];
(new Random()).NextBytes(guidbytes);
//set version etc
MakeValidRandomGuid(guidbytes);
// create the new System.Guid object
return new System.Guid(guidbytes);
}
#endregion
#region Helper Methods
private static void MakeValidRandomGuid(byte[] guidbytes)
{
// set the variant
guidbytes[Const.VariantByte] &= Const.VariantByteMask;
guidbytes[Const.VariantByte] |=
((int)Const.GuidVariant.Standard << Const.VariantByteShift);
// set the version
guidbytes[Const.VersionByte] &= Const.VersionByteMask;
guidbytes[Const.VersionByte] |=
((int)Const.GuidVersion.Random << Const.VersionByteShift);
}
#endregion
#region Ticks
/// <summary>
/// Create a new <see cref="Guid"/> only using TickCount and bit shifting.
/// </summary>
public static System.Guid NewGuidTicks()
{
// Create a unique GUID
long fileTime = DateTime.Now.ToUniversalTime().ToFileTime();
UInt32 high32 = (UInt32)(fileTime >> 32)+0x146BF4;
int tick = System.Environment.TickCount;
byte[] guidBytes = new byte[8];
// load the byte array with random bits
Random rand = new Random((int)fileTime);
rand.NextBytes(guidBytes);
// use tick info in the middle of the array
guidBytes[2] = (byte)(tick >> 24);
guidBytes[3] = (byte)(tick >> 16);
guidBytes[4] = (byte)(tick >> 8);
guidBytes[5] = (byte)tick;
// Construct a Guid with our data
System.Guid guid = new System.Guid((int)fileTime, (short)high32, (short)((high32 | 0x10000000) >> 16), guidBytes);
return guid;
}
#endregion
#region Ole
/// <summary>
/// Create a new <see cref="Guid"/> using COM APIs
/// </summary>
/// <returns></returns>
/// <remarks>Requires Windows CE.NET 4.1 or higher.</remarks>
public static System.Guid NewOleGuid()
{
System.Guid val = System.Guid.Empty;
int hresult = 0;
hresult = CoCreateGuid(ref val);
if(hresult != 0)
{
throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error(), "Error creating new Guid");
}
return val;
}
[DllImport("ole32.dll", SetLastError=true)]
private static extern int CoCreateGuid(ref System.Guid pguid);
#endregion
}
}
#endif