// Sebastien Pouliot (sebastien@ximian.com)
//
// (C) 2002
-// Copyright (C) 2004 Novell (http://www.novell.com)
-//
-
-//
-// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// Great Power without form."
// -- The Verrah Rubicon of Verena, Book One
-using System;
using System.Globalization;
using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
using System.Text;
namespace System.Security.Cryptography {
-#if NET_1_0
- public class RNGCryptoServiceProvider : RandomNumberGenerator {
-#else
- public sealed class RNGCryptoServiceProvider : RandomNumberGenerator {
+#if !NET_2_1
+ [ComVisible (true)]
#endif
+ public sealed class RNGCryptoServiceProvider : RandomNumberGenerator {
+ private static object _lock;
private IntPtr _handle;
+ static RNGCryptoServiceProvider ()
+ {
+ if (RngOpen ())
+ _lock = new object ();
+ }
+
public RNGCryptoServiceProvider ()
{
_handle = RngInitialize (null);
Check ();
}
-
+#if !NET_2_1
public RNGCryptoServiceProvider (byte[] rgb)
{
_handle = RngInitialize (rgb);
_handle = RngInitialize (Encoding.UTF8.GetBytes (str));
Check ();
}
-
+#endif
private void Check ()
{
if (_handle == IntPtr.Zero) {
Locale.GetText ("Couldn't access random source."));
}
}
+
+ [MethodImplAttribute(MethodImplOptions.InternalCall)]
+ private static extern bool RngOpen ();
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private static extern IntPtr RngInitialize (byte[] seed);
if (data == null)
throw new ArgumentNullException ("data");
- _handle = RngGetBytes (_handle, data);
+ if (_lock == null) {
+ _handle = RngGetBytes (_handle, data);
+ } else {
+ // using a global handle for randomness
+ lock (_lock) {
+ _handle = RngGetBytes (_handle, data);
+ }
+ }
Check ();
}
_handle = IntPtr.Zero;
}
}
+
+#if NET_4_0 || MOONLIGHT
+ protected override void Dispose (bool disposing)
+ {
+ base.Dispose (disposing);
+ }
+#endif
}
}