From 62ddc8f95d0de54a1237ee30a07b3d180b61a63c Mon Sep 17 00:00:00 2001 From: cach30verfl0w Date: Fri, 14 Jun 2024 18:15:40 +0200 Subject: [PATCH] Add 'tryParse' method to KeyReaderHelper --- .../advcrypto/linux/utils/KeyReaderHelper.kt | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/kmp-advcrypto/src/linuxX64Main/kotlin/io/karma/advcrypto/linux/utils/KeyReaderHelper.kt b/kmp-advcrypto/src/linuxX64Main/kotlin/io/karma/advcrypto/linux/utils/KeyReaderHelper.kt index 224d13b..e6f7434 100644 --- a/kmp-advcrypto/src/linuxX64Main/kotlin/io/karma/advcrypto/linux/utils/KeyReaderHelper.kt +++ b/kmp-advcrypto/src/linuxX64Main/kotlin/io/karma/advcrypto/linux/utils/KeyReaderHelper.kt @@ -158,4 +158,28 @@ object KeyReaderHelper { return null } + /** + * This method tries to parse the data in the specified pointer in a key. This method supports + * PEM and DER. If no supported format worked, this method simply returns null. + * + * @author Cedric Hammes + * @since 14/06/2024 + */ + fun tryParse(pointer: CPointer, size: ULong, purposes: UByte): Key? { + // Try to parse as PEM and return if successful + val pemKey = tryParseAsPEM(pointer, size, purposes) + if (pemKey != null) { + return pemKey + } + + // Try to parse as DER and return if successful + val derKey = tryParseAsDER(pointer, size, purposes) + if (derKey != null) { + return derKey + } + + // If no format works, return null + return null + } + } \ No newline at end of file