From fd1e12d9e0e12b7cf49165248a86bb24d4e87d65 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Fri, 16 Oct 2015 12:01:53 +0700 Subject: Avoid Interlocked.Increment(Int64) on .NET CF - https://github.com/bcgit/bc-csharp/issues/20 --- crypto/src/security/SecureRandom.cs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'crypto/src/security/SecureRandom.cs') diff --git a/crypto/src/security/SecureRandom.cs b/crypto/src/security/SecureRandom.cs index f46427a5c..137a471c1 100644 --- a/crypto/src/security/SecureRandom.cs +++ b/crypto/src/security/SecureRandom.cs @@ -13,12 +13,16 @@ namespace Org.BouncyCastle.Security { private static long counter = Times.NanoTime(); +#if NETCF_1_0 + private static object counterLock = new object(); private static long NextCounterValue() { - return Interlocked.Increment(ref counter); + lock (counterLock) + { + return ++counter; + } } -#if NETCF_1_0 private static readonly SecureRandom[] master = { null }; private static SecureRandom Master { @@ -43,6 +47,11 @@ namespace Org.BouncyCastle.Security } } #else + private static long NextCounterValue() + { + return Interlocked.Increment(ref counter); + } + private static readonly SecureRandom master = new SecureRandom(new CryptoApiRandomGenerator()); private static SecureRandom Master { -- cgit 1.5.1