summary refs log tree commit diff
path: root/crypto/src/x509
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2023-04-20 18:40:56 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2023-04-20 18:40:56 +0700
commit758b05345c9d2f0b412f75435cb31231a7f70311 (patch)
tree5600970e299bc813a44ee48d2e3c8ba8eca9572d /crypto/src/x509
parentBigInteger construction from little-endian (diff)
downloadBouncyCastle.NET-ed25519-758b05345c9d2f0b412f75435cb31231a7f70311.tar.xz
Refactoring: reduced allocations
Diffstat (limited to 'crypto/src/x509')
-rw-r--r--crypto/src/x509/SubjectPublicKeyInfoFactory.cs20
1 files changed, 19 insertions, 1 deletions
diff --git a/crypto/src/x509/SubjectPublicKeyInfoFactory.cs b/crypto/src/x509/SubjectPublicKeyInfoFactory.cs
index fc0492fe4..36facc3aa 100644
--- a/crypto/src/x509/SubjectPublicKeyInfoFactory.cs
+++ b/crypto/src/x509/SubjectPublicKeyInfoFactory.cs
@@ -174,7 +174,17 @@ namespace Org.BouncyCastle.X509
                         x962 = new X962Parameters(_key.PublicKeyParamSet);
                     }
 
-                    byte[] pubKey = _key.Q.GetEncoded(false);
+                    var q = _key.Q;
+
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+                    int encodedLength = q.GetEncodedLength(false);
+                    Span<byte> pubKey = encodedLength <= 512
+                        ? stackalloc byte[encodedLength]
+                        : new byte[encodedLength];
+                    q.EncodeTo(false, pubKey);
+#else
+                    byte[] pubKey = q.GetEncoded(false);
+#endif
 
                     AlgorithmIdentifier algID = new AlgorithmIdentifier(
                         X9ObjectIdentifiers.IdECPublicKey, x962.ToAsn1Object());
@@ -212,14 +222,22 @@ namespace Org.BouncyCastle.X509
             {
                 X448PublicKeyParameters key = (X448PublicKeyParameters)publicKey;
 
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+                return new SubjectPublicKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_X448), key.DataSpan);
+#else
                 return new SubjectPublicKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_X448), key.GetEncoded());
+#endif
             }
 
             if (publicKey is X25519PublicKeyParameters)
             {
                 X25519PublicKeyParameters key = (X25519PublicKeyParameters)publicKey;
 
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+                return new SubjectPublicKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_X25519), key.DataSpan);
+#else
                 return new SubjectPublicKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_X25519), key.GetEncoded());
+#endif
             }
 
             if (publicKey is Ed448PublicKeyParameters)