From 1d31e90574c9dc8060670773a99c8a9ea5b2827c Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Thu, 30 Jun 2022 10:33:35 +0700 Subject: Some PORTABLE cleanup --- crypto/src/asn1/DerGeneralizedTime.cs | 7 +--- crypto/src/asn1/DerUTCTime.cs | 4 --- crypto/src/asn1/x509/Time.cs | 7 +--- crypto/src/crypto/generators/OpenBsdBCrypt.cs | 6 +--- .../crypto/prng/CryptoApiEntropySourceProvider.cs | 5 +-- crypto/src/crypto/prng/CryptoApiRandomGenerator.cs | 4 --- crypto/src/openpgp/WrappedGeneratorStream.cs | 42 ++++++++++++---------- crypto/src/util/Strings.cs | 4 +-- crypto/src/util/io/FilterStream.cs | 35 +++++++++--------- crypto/test/src/util/test/SimpleTest.cs | 8 ----- crypto/test/src/util/test/UncloseableStream.cs | 20 +++-------- 11 files changed, 51 insertions(+), 91 deletions(-) (limited to 'crypto') 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; } } diff --git a/crypto/test/src/util/test/SimpleTest.cs b/crypto/test/src/util/test/SimpleTest.cs index 91c493ef7..81c38b54f 100644 --- a/crypto/test/src/util/test/SimpleTest.cs +++ b/crypto/test/src/util/test/SimpleTest.cs @@ -187,20 +187,12 @@ namespace Org.BouncyCastle.Utilities.Test public static DateTime MakeUtcDateTime(int year, int month, int day, int hour, int minute, int second) { -#if PORTABLE return new DateTime(year, month, day, hour, minute, second, DateTimeKind.Utc); -#else - return new DateTime(year, month, day, hour, minute, second); -#endif } public static DateTime MakeUtcDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond) { -#if PORTABLE return new DateTime(year, month, day, hour, minute, second, millisecond, DateTimeKind.Utc); -#else - return new DateTime(year, month, day, hour, minute, second, millisecond); -#endif } } } diff --git a/crypto/test/src/util/test/UncloseableStream.cs b/crypto/test/src/util/test/UncloseableStream.cs index 0a7a16e66..2ec13f249 100644 --- a/crypto/test/src/util/test/UncloseableStream.cs +++ b/crypto/test/src/util/test/UncloseableStream.cs @@ -6,34 +6,24 @@ using Org.BouncyCastle.Utilities.IO; namespace Org.BouncyCastle.Utilities.Test { /// - /// This is a testing utility class to check the property that a Stream is never - /// closed in some particular context - typically when wrapped by another Stream that - /// should not be forwarding its Stream.Close() calls. Not needed in production code. + /// This is a testing utility class to check the property that a is never disposed in some + /// particular context,typically when wrapped by another that should not be forwarding its + /// calls. Not needed in production code. /// public class UncloseableStream : FilterStream { - public UncloseableStream( - Stream s) + public UncloseableStream(Stream s) : base(s) { } -#if PORTABLE protected override void Dispose(bool disposing) { if (disposing) - { - throw new Exception("UncloseableStream was disposed"); - } + throw new InvalidOperationException("UncloseableStream was disposed"); base.Dispose(disposing); } -#else - public override void Close() - { - throw new Exception("Close() called on UncloseableStream"); - } -#endif } } -- cgit 1.4.1