Skip to content

Commit

Permalink
Simplify a bit FULL_AOT_RUNTIME build wrt use of CommonCrypto. Ref: a…
Browse files Browse the repository at this point in the history
…ssistly #9858
  • Loading branch information
spouliot authored and kumpera committed Dec 10, 2012
1 parent ae21cb1 commit 98df353
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions mcs/class/corlib/System/Guid.cs
Expand Up @@ -38,14 +38,17 @@
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
#if FULL_AOT_RUNTIME
using Crimson.CommonCrypto;
#endif

namespace System {

[Serializable]
[StructLayout (LayoutKind.Sequential)]
[ComVisible (true)]
public struct Guid : IFormattable, IComparable, IComparable<Guid>, IEquatable<Guid> {
#if MONOTOUCH
#if FULL_AOT_RUNTIME
static Guid () {
if (MonoTouchAOTHelper.FalseFlag) {
var comparer = new System.Collections.Generic.GenericComparer <Guid> ();
Expand Down Expand Up @@ -463,31 +466,27 @@ private static char ToHex (int b)
return (char)((b<0xA)?('0' + b):('a' + b - 0xA));
}

private static object _rngAccess = new object ();
#if !FULL_AOT_RUNTIME
private static object _rngAccess = new object ();
private static RandomNumberGenerator _rng;
private static RandomNumberGenerator _fastRng;
#else
private static object _fastRng;
#endif

// generated as per section 3.4 of the specification
public static Guid NewGuid ()
{
#if !FULL_AOT_RUNTIME
byte[] b = new byte [16];

#if !FULL_AOT_RUNTIME
// thread-safe access to the prng
lock (_rngAccess) {
if (_rng == null)
_rng = RandomNumberGenerator.Create ();
_rng.GetBytes (b);
}
#else
byte[] b = FastNewGuidArray ();
Cryptor.GetRandom (b);
#endif


Guid res = new Guid (b);
// Mask in Variant 1-0 in Bit[7..6]
res._d = (byte) ((res._d & 0x3fu) | 0x80u);
Expand All @@ -497,6 +496,7 @@ public static Guid NewGuid ()
return res;
}

#if !FULL_AOT_RUNTIME
// used in ModuleBuilder so mcs doesn't need to invoke
// CryptoConfig for simple assemblies.
internal static byte[] FastNewGuidArray ()
Expand All @@ -506,18 +506,12 @@ internal static byte[] FastNewGuidArray ()
// thread-safe access to the prng
lock (_rngAccess) {
// if known, use preferred RNG
#if FULL_AOT_RUNTIME
if (_fastRng == null)
_fastRng = new RNGCryptoServiceProvider ();
(_fastRng as RNGCryptoServiceProvider).GetBytes (guid);
#else
if (_rng != null)
_fastRng = _rng;
// else use hardcoded default RNG (bypassing CryptoConfig)
if (_fastRng == null)
_fastRng = new RNGCryptoServiceProvider ();
_fastRng.GetBytes (guid);
#endif
}

// Mask in Variant 1-0 in Bit[7..6]
Expand All @@ -527,7 +521,7 @@ internal static byte[] FastNewGuidArray ()

return guid;
}

#endif
public byte[] ToByteArray ()
{
byte[] res = new byte[16];
Expand Down

0 comments on commit 98df353

Please sign in to comment.