From 3bd06ac7ffb3c65248c350fc65afa66fb10c653d Mon Sep 17 00:00:00 2001 From: sergeych Date: Thu, 4 Dec 2025 12:13:56 +0100 Subject: [PATCH] added Container.decryptWithPassword --- .../kotlin/net/sergeych/crypto2/Container.kt | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/commonMain/kotlin/net/sergeych/crypto2/Container.kt b/src/commonMain/kotlin/net/sergeych/crypto2/Container.kt index 3b517b7..e848c7b 100644 --- a/src/commonMain/kotlin/net/sergeych/crypto2/Container.kt +++ b/src/commonMain/kotlin/net/sergeych/crypto2/Container.kt @@ -321,6 +321,27 @@ sealed class Container { }.build() } + /** + * Decrypt the container with a password. It scans all key ids for + * these with `KDP` params, e.g., derived from password, and try to + * derive keys from the password and decrypt the container. If there are + * no derivable keys, or all of them failed to decrypt, returns null. + * It could be long operation if there are multiple derivable keys with heavy + * KDF. See [PBKD] and [KDF] for more. + * + * @return decrypted data or null + */ + @Suppress("unused") + fun decryptWithPassword(password: String): UByteArray? { + for( id in this.keyIds ) { + id.kdp?.let { kdp -> + decryptWith(kdp.deriveKey(password))?.let { return it } + } + } + return null + } + + companion object { /**