From 35d6cf0af1e8305c84fb2fccafd97fc91db9f099 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Tue, 11 Apr 2023 18:13:27 +0700 Subject: Add Memory/Span accessors to avoid some copies --- crypto/src/security/PrivateKeyFactory.cs | 7 +++++++ crypto/src/security/PublicKeyFactory.cs | 11 +++++++++++ 2 files changed, 18 insertions(+) (limited to 'crypto/src/security') diff --git a/crypto/src/security/PrivateKeyFactory.cs b/crypto/src/security/PrivateKeyFactory.cs index 5d70423f4..38e571cd6 100644 --- a/crypto/src/security/PrivateKeyFactory.cs +++ b/crypto/src/security/PrivateKeyFactory.cs @@ -350,10 +350,17 @@ namespace Org.BouncyCastle.Security } } +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + private static ReadOnlySpan GetRawKey(PrivateKeyInfo keyInfo) + { + return Asn1OctetString.GetInstance(keyInfo.ParsePrivateKey()).GetOctetsSpan(); + } +#else private static byte[] GetRawKey(PrivateKeyInfo keyInfo) { return Asn1OctetString.GetInstance(keyInfo.ParsePrivateKey()).GetOctets(); } +#endif public static AsymmetricKeyParameter DecryptKey( char[] passPhrase, diff --git a/crypto/src/security/PublicKeyFactory.cs b/crypto/src/security/PublicKeyFactory.cs index 775884e94..03cabbb13 100644 --- a/crypto/src/security/PublicKeyFactory.cs +++ b/crypto/src/security/PublicKeyFactory.cs @@ -280,6 +280,16 @@ namespace Org.BouncyCastle.Security } } +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + private static ReadOnlySpan GetRawKey(SubjectPublicKeyInfo keyInfo) + { + /* + * TODO[RFC 8422] + * - Require keyInfo.Algorithm.Parameters == null? + */ + return keyInfo.PublicKeyData.GetOctetsSpan(); + } +#else private static byte[] GetRawKey(SubjectPublicKeyInfo keyInfo) { /* @@ -288,6 +298,7 @@ namespace Org.BouncyCastle.Security */ return keyInfo.PublicKeyData.GetOctets(); } +#endif private static bool IsPkcsDHParam(Asn1Sequence seq) { -- cgit 1.5.1