diff options
Diffstat (limited to 'crypto/src/asn1')
-rw-r--r-- | crypto/src/asn1/Asn1RelativeOid.cs | 21 | ||||
-rw-r--r-- | crypto/src/asn1/DerObjectIdentifier.cs | 20 | ||||
-rw-r--r-- | crypto/src/asn1/x9/X9ECParametersHolder.cs | 39 |
3 files changed, 15 insertions, 65 deletions
diff --git a/crypto/src/asn1/Asn1RelativeOid.cs b/crypto/src/asn1/Asn1RelativeOid.cs index b7df4b75a..1d2ecb3df 100644 --- a/crypto/src/asn1/Asn1RelativeOid.cs +++ b/crypto/src/asn1/Asn1RelativeOid.cs @@ -137,8 +137,11 @@ namespace Org.BouncyCastle.Asn1 return new PrimitiveDerEncoding(tagClass, tagNo, GetContents()); } - private void DoOutput(MemoryStream bOut) + private byte[] GetContents() => Objects.EnsureSingletonInitialized(ref contents, identifier, CreateContents); + + private static byte[] CreateContents(string identifier) { + MemoryStream bOut = new MemoryStream(); OidTokenizer tok = new OidTokenizer(identifier); while (tok.HasMoreTokens) { @@ -152,21 +155,7 @@ namespace Org.BouncyCastle.Asn1 WriteField(bOut, new BigInteger(token)); } } - } - - private byte[] GetContents() - { - lock (this) - { - if (contents == null) - { - MemoryStream bOut = new MemoryStream(); - DoOutput(bOut); - contents = bOut.ToArray(); - } - - return contents; - } + return bOut.ToArray(); } internal static Asn1RelativeOid CreatePrimitive(byte[] contents, bool clone) diff --git a/crypto/src/asn1/DerObjectIdentifier.cs b/crypto/src/asn1/DerObjectIdentifier.cs index 91e427d0b..12e8ea0a8 100644 --- a/crypto/src/asn1/DerObjectIdentifier.cs +++ b/crypto/src/asn1/DerObjectIdentifier.cs @@ -168,8 +168,11 @@ namespace Org.BouncyCastle.Asn1 return new PrimitiveDerEncoding(tagClass, tagNo, GetContents()); } - private void DoOutput(MemoryStream bOut) + private byte[] GetContents() => Objects.EnsureSingletonInitialized(ref contents, identifier, CreateContents); + + private static byte[] CreateContents(string identifier) { + MemoryStream bOut = new MemoryStream(); OidTokenizer tok = new OidTokenizer(identifier); string token = tok.NextToken(); @@ -197,21 +200,8 @@ namespace Org.BouncyCastle.Asn1 Asn1RelativeOid.WriteField(bOut, new BigInteger(token)); } } - } - - private byte[] GetContents() - { - lock (this) - { - if (contents == null) - { - MemoryStream bOut = new MemoryStream(); - DoOutput(bOut); - contents = bOut.ToArray(); - } - return contents; - } + return bOut.ToArray(); } internal static DerObjectIdentifier CreatePrimitive(byte[] contents, bool clone) diff --git a/crypto/src/asn1/x9/X9ECParametersHolder.cs b/crypto/src/asn1/x9/X9ECParametersHolder.cs index ea72cc6ac..535dad9f7 100644 --- a/crypto/src/asn1/x9/X9ECParametersHolder.cs +++ b/crypto/src/asn1/x9/X9ECParametersHolder.cs @@ -1,4 +1,5 @@ using Org.BouncyCastle.Math.EC; +using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Asn1.X9 { @@ -7,42 +8,12 @@ namespace Org.BouncyCastle.Asn1.X9 private ECCurve m_curve; private X9ECParameters m_parameters; - public ECCurve Curve - { - get - { - lock (this) - { - if (m_curve == null) - { - m_curve = CreateCurve(); - } + public ECCurve Curve => Objects.EnsureSingletonInitialized(ref m_curve, this, self => self.CreateCurve()); - return m_curve; - } - } - } + public X9ECParameters Parameters => + Objects.EnsureSingletonInitialized(ref m_parameters, this, self => self.CreateParameters()); - public X9ECParameters Parameters - { - get - { - lock (this) - { - if (m_parameters == null) - { - m_parameters = CreateParameters(); - } - - return m_parameters; - } - } - } - - protected virtual ECCurve CreateCurve() - { - return CreateParameters().Curve; - } + protected virtual ECCurve CreateCurve() => Parameters.Curve; protected abstract X9ECParameters CreateParameters(); } |