summary refs log tree commit diff
path: root/crypto/src
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-06-30 10:33:35 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-06-30 10:33:35 +0700
commit1d31e90574c9dc8060670773a99c8a9ea5b2827c (patch)
tree50c520a5e0bd14dab5e5ad85cdc5dd5e8928ff56 /crypto/src
parentCleanup Stream disposing in Bcpg (diff)
downloadBouncyCastle.NET-ed25519-1d31e90574c9dc8060670773a99c8a9ea5b2827c.tar.xz
Some PORTABLE cleanup
Diffstat (limited to 'crypto/src')
-rw-r--r--crypto/src/asn1/DerGeneralizedTime.cs7
-rw-r--r--crypto/src/asn1/DerUTCTime.cs4
-rw-r--r--crypto/src/asn1/x509/Time.cs7
-rw-r--r--crypto/src/crypto/generators/OpenBsdBCrypt.cs6
-rw-r--r--crypto/src/crypto/prng/CryptoApiEntropySourceProvider.cs5
-rw-r--r--crypto/src/crypto/prng/CryptoApiRandomGenerator.cs4
-rw-r--r--crypto/src/openpgp/WrappedGeneratorStream.cs42
-rw-r--r--crypto/src/util/Strings.cs4
-rw-r--r--crypto/src/util/io/FilterStream.cs35
9 files changed, 46 insertions, 68 deletions
diff --git a/crypto/src/asn1/DerGeneralizedTime.cs b/crypto/src/asn1/DerGeneralizedTime.cs
index f5ef3f372..16774cb02 100644
--- a/crypto/src/asn1/DerGeneralizedTime.cs
+++ b/crypto/src/asn1/DerGeneralizedTime.cs
@@ -98,14 +98,9 @@ namespace Org.BouncyCastle.Asn1
         /**
          * base constructor from a local time object
          */
-        public DerGeneralizedTime(
-            DateTime time)
+        public DerGeneralizedTime(DateTime time)
         {
-#if PORTABLE
             this.time = time.ToUniversalTime().ToString(@"yyyyMMddHHmmss\Z");
-#else
-            this.time = time.ToString(@"yyyyMMddHHmmss\Z");
-#endif
         }
 
         internal DerGeneralizedTime(
diff --git a/crypto/src/asn1/DerUTCTime.cs b/crypto/src/asn1/DerUTCTime.cs
index 3b4b247bd..7f7756d49 100644
--- a/crypto/src/asn1/DerUTCTime.cs
+++ b/crypto/src/asn1/DerUTCTime.cs
@@ -101,11 +101,7 @@ namespace Org.BouncyCastle.Asn1
          */
         public DerUtcTime(DateTime time)
         {
-#if PORTABLE
             this.time = time.ToUniversalTime().ToString("yyMMddHHmmss", CultureInfo.InvariantCulture) + "Z";
-#else
-            this.time = time.ToString("yyMMddHHmmss", CultureInfo.InvariantCulture) + "Z";
-#endif
         }
 
 		internal DerUtcTime(byte[] contents)
diff --git a/crypto/src/asn1/x509/Time.cs b/crypto/src/asn1/x509/Time.cs
index fa3936d63..efdf63850 100644
--- a/crypto/src/asn1/x509/Time.cs
+++ b/crypto/src/asn1/x509/Time.cs
@@ -33,14 +33,9 @@ namespace Org.BouncyCastle.Asn1.X509
          * and 2049 a UTCTime object is Generated, otherwise a GeneralizedTime
          * is used.
          */
-        public Time(
-            DateTime date)
+        public Time(DateTime date)
         {
-#if PORTABLE
             string d = date.ToUniversalTime().ToString("yyyyMMddHHmmss", CultureInfo.InvariantCulture) + "Z";
-#else
-            string d = date.ToString("yyyyMMddHHmmss", CultureInfo.InvariantCulture) + "Z";
-#endif
 
             int year = int.Parse(d.Substring(0, 4));
 
diff --git a/crypto/src/crypto/generators/OpenBsdBCrypt.cs b/crypto/src/crypto/generators/OpenBsdBCrypt.cs
index 40211a51d..cda9d871f 100644
--- a/crypto/src/crypto/generators/OpenBsdBCrypt.cs
+++ b/crypto/src/crypto/generators/OpenBsdBCrypt.cs
@@ -167,18 +167,14 @@ namespace Org.BouncyCastle.Crypto.Generators
             if (!AllowedVersions.Contains(version))
                 throw new ArgumentException("Bcrypt version '" + version + "' is not supported by this implementation", "bcryptString");
 
-            int cost = 0;
+            int cost;
             try
             {
                 cost = int.Parse(bcryptString.Substring(4, 2));
             }
             catch (Exception nfe)
             {
-#if PORTABLE
-                throw new ArgumentException("Invalid cost factor: " + bcryptString.Substring(4, 2), "bcryptString");
-#else
                 throw new ArgumentException("Invalid cost factor: " + bcryptString.Substring(4, 2), "bcryptString", nfe);
-#endif
             }
             if (cost < 4 || cost > 31)
                 throw new ArgumentException("Invalid cost factor: " + cost + ", 4 < cost < 31 expected.");
diff --git a/crypto/src/crypto/prng/CryptoApiEntropySourceProvider.cs b/crypto/src/crypto/prng/CryptoApiEntropySourceProvider.cs
index a0925f1ba..635af2bd9 100644
--- a/crypto/src/crypto/prng/CryptoApiEntropySourceProvider.cs
+++ b/crypto/src/crypto/prng/CryptoApiEntropySourceProvider.cs
@@ -1,5 +1,4 @@
-#if !PORTABLE
-using System;
+using System;
 using System.Security.Cryptography;
 
 namespace Org.BouncyCastle.Crypto.Prng
@@ -66,5 +65,3 @@ namespace Org.BouncyCastle.Crypto.Prng
         }
     }
 }
-
-#endif
diff --git a/crypto/src/crypto/prng/CryptoApiRandomGenerator.cs b/crypto/src/crypto/prng/CryptoApiRandomGenerator.cs
index d324a935f..7803ddd3d 100644
--- a/crypto/src/crypto/prng/CryptoApiRandomGenerator.cs
+++ b/crypto/src/crypto/prng/CryptoApiRandomGenerator.cs
@@ -1,5 +1,3 @@
-#if !PORTABLE
-
 using System;
 using System.Security.Cryptography;
 
@@ -62,5 +60,3 @@ namespace Org.BouncyCastle.Crypto.Prng
         #endregion
     }
 }
-
-#endif
diff --git a/crypto/src/openpgp/WrappedGeneratorStream.cs b/crypto/src/openpgp/WrappedGeneratorStream.cs
index 5f4a4b045..df31d71f0 100644
--- a/crypto/src/openpgp/WrappedGeneratorStream.cs
+++ b/crypto/src/openpgp/WrappedGeneratorStream.cs
@@ -1,37 +1,41 @@
+using System;
 using System.IO;
 
 using Org.BouncyCastle.Utilities.IO;
 
 namespace Org.BouncyCastle.Bcpg.OpenPgp
 {
-	public class WrappedGeneratorStream
+	internal sealed class WrappedGeneratorStream
 		: FilterStream
 	{
-		private readonly IStreamGenerator gen;
+		private IStreamGenerator m_generator;
 
-		public WrappedGeneratorStream(
-			IStreamGenerator	gen,
-			Stream				str)
-			: base(str)
+		internal WrappedGeneratorStream(IStreamGenerator generator, Stream s)
+			: base(s)
 		{
-			this.gen = gen;
+			if (generator == null)
+				throw new ArgumentNullException(nameof(generator));
+
+			m_generator = generator;
 		}
 
-#if PORTABLE
         protected override void Dispose(bool disposing)
         {
-            if (disposing)
+			if (m_generator != null)
+			{
+				if (disposing)
+				{
+					m_generator.Close();
+				}
+
+				m_generator = null;
+			}
+
+			// Don't dispose the wrapped Stream
+			if (!disposing)
             {
-                gen.Close();
-                return;
-            }
-            base.Dispose(disposing);
-        }
-#else
-		public override void Close()
-		{
-			gen.Close();
+				base.Dispose(disposing);
+			}
 		}
-#endif
 	}
 }
diff --git a/crypto/src/util/Strings.cs b/crypto/src/util/Strings.cs
index 1d0c94611..baee573be 100644
--- a/crypto/src/util/Strings.cs
+++ b/crypto/src/util/Strings.cs
@@ -48,7 +48,7 @@ namespace Org.BouncyCastle.Utilities
 
         public static string FromAsciiByteArray(byte[] bytes)
         {
-            return Encoding.ASCII.GetString(bytes, 0, bytes.Length);
+            return Encoding.ASCII.GetString(bytes);
         }
 
         public static byte[] ToAsciiByteArray(char[] cs)
@@ -63,7 +63,7 @@ namespace Org.BouncyCastle.Utilities
 
         public static string FromUtf8ByteArray(byte[] bytes)
         {
-            return Encoding.UTF8.GetString(bytes, 0, bytes.Length);
+            return Encoding.UTF8.GetString(bytes);
         }
 
         public static byte[] ToUtf8ByteArray(char[] cs)
diff --git a/crypto/src/util/io/FilterStream.cs b/crypto/src/util/io/FilterStream.cs
index a92dee3e5..0db82ec88 100644
--- a/crypto/src/util/io/FilterStream.cs
+++ b/crypto/src/util/io/FilterStream.cs
@@ -1,13 +1,29 @@
+using System;
 using System.IO;
 
 namespace Org.BouncyCastle.Utilities.IO
 {
-    public class FilterStream : Stream
+    public class FilterStream
+        : Stream
     {
+        protected readonly Stream s;
+
         public FilterStream(Stream s)
         {
+            if (s == null)
+                throw new ArgumentNullException(nameof(s));
+
             this.s = s;
         }
+        protected override void Dispose(bool disposing)
+        {
+            if (disposing)
+            {
+                s.Dispose();
+            }
+
+            base.Dispose(disposing);
+        }
         public override bool CanRead
         {
             get { return s.CanRead; }
@@ -29,22 +45,6 @@ namespace Org.BouncyCastle.Utilities.IO
             get { return s.Position; }
             set { s.Position = value; }
         }
-#if PORTABLE
-        protected override void Dispose(bool disposing)
-        {
-            if (disposing)
-            {
-                Platform.Dispose(s);
-            }
-            base.Dispose(disposing);
-        }
-#else
-        public override void Close()
-        {
-            Platform.Dispose(s);
-            base.Close();
-        }
-#endif
         public override void Flush()
         {
             s.Flush();
@@ -73,6 +73,5 @@ namespace Org.BouncyCastle.Utilities.IO
         {
             s.WriteByte(value);
         }
-        protected readonly Stream s;
     }
 }