From 4c6a6360cf83659d1f5c3a7c5710ac54426e9235 Mon Sep 17 00:00:00 2001 From: oSumAtrIX Date: Thu, 14 Mar 2024 12:39:01 +0100 Subject: [PATCH] feat: Simplify signing utility API --- api/revanced-library.api | 1 + .../kotlin/app/revanced/library/ApkUtils.kt | 37 ++++++++++++++++--- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/api/revanced-library.api b/api/revanced-library.api index 8c40200..54f5e86 100644 --- a/api/revanced-library.api +++ b/api/revanced-library.api @@ -39,6 +39,7 @@ public final class app/revanced/library/ApkUtils { public final fun sign (Ljava/io/File;Lapp/revanced/library/ApkUtils$SigningOptions;)V public final fun sign (Ljava/io/File;Ljava/io/File;Lapp/revanced/library/ApkUtils$SigningOptions;)V public final fun sign (Ljava/io/File;Ljava/io/File;Ljava/lang/String;Lapp/revanced/library/ApkSigner$PrivateKeyCertificatePair;)V + public final fun signApk (Ljava/io/File;Ljava/io/File;Ljava/lang/String;Lapp/revanced/library/ApkUtils$KeyStoreDetails;)V } public final class app/revanced/library/ApkUtils$KeyStoreDetails { diff --git a/src/main/kotlin/app/revanced/library/ApkUtils.kt b/src/main/kotlin/app/revanced/library/ApkUtils.kt index d098779..b56c62c 100644 --- a/src/main/kotlin/app/revanced/library/ApkUtils.kt +++ b/src/main/kotlin/app/revanced/library/ApkUtils.kt @@ -103,6 +103,7 @@ object ApkUtils { * * @return The newly created private key and certificate pair. */ + @Deprecated("This method will be removed in the future.") fun newPrivateKeyCertificatePair( privateKeyCertificatePairDetails: PrivateKeyCertificatePairDetails, keyStoreDetails: KeyStoreDetails, @@ -131,9 +132,10 @@ object ApkUtils { * * @return The private key and certificate pair. */ + @Deprecated("This method will be removed in the future.") fun readPrivateKeyCertificatePairFromKeyStore( keyStoreDetails: KeyStoreDetails, - ) = ApkSigner.readKeyCertificatePair( + ) = ApkSigner.readPrivateKeyCertificatePair( ApkSigner.readKeyStore( keyStoreDetails.keyStore.inputStream(), keyStoreDetails.keyStorePassword, @@ -144,20 +146,26 @@ object ApkUtils { /** * Signs [inputApkFile] with the given options and saves the signed apk to [outputApkFile]. + * If [KeyStoreDetails.keyStore] does not exist, + * a new private key and certificate pair will be created and saved to the keystore. * * @param inputApkFile The apk file to sign. * @param outputApkFile The file to save the signed apk to. * @param signer The name of the signer. - * @param privateKeyCertificatePair The private key and certificate pair to use for signing. + * @param keyStoreDetails The details for the keystore. */ - fun sign( + fun signApk( inputApkFile: File, outputApkFile: File, signer: String, - privateKeyCertificatePair: ApkSigner.PrivateKeyCertificatePair, + keyStoreDetails: KeyStoreDetails, ) = ApkSigner.newApkSigner( signer, - privateKeyCertificatePair, + if (keyStoreDetails.keyStore.exists()) { + readPrivateKeyCertificatePairFromKeyStore(keyStoreDetails) + } else { + newPrivateKeyCertificatePair(PrivateKeyCertificatePairDetails(), keyStoreDetails) + }, ).signApk(inputApkFile, outputApkFile) @Deprecated("This method will be removed in the future.") @@ -182,6 +190,25 @@ object ApkUtils { } } + /** + * Signs [inputApkFile] with the given options and saves the signed apk to [outputApkFile]. + * + * @param inputApkFile The apk file to sign. + * @param outputApkFile The file to save the signed apk to. + * @param signer The name of the signer. + * @param privateKeyCertificatePair The private key and certificate pair to use for signing. + */ + @Deprecated("This method will be removed in the future.") + fun sign( + inputApkFile: File, + outputApkFile: File, + signer: String, + privateKeyCertificatePair: ApkSigner.PrivateKeyCertificatePair, + ) = ApkSigner.newApkSigner( + signer, + privateKeyCertificatePair, + ).signApk(inputApkFile, outputApkFile) + /** * Signs the apk file with the given options. *