diff --git a/.idea/misc.xml b/.idea/misc.xml index 7deb2c1..e0c5638 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/src/commonMain/kotlin/net/sergeych/crypto2/UniversalRing.kt b/src/commonMain/kotlin/net/sergeych/crypto2/UniversalRing.kt index ab96888..d9d883c 100644 --- a/src/commonMain/kotlin/net/sergeych/crypto2/UniversalRing.kt +++ b/src/commonMain/kotlin/net/sergeych/crypto2/UniversalRing.kt @@ -229,6 +229,8 @@ class UniversalRing( companion object { val EMPTY = UniversalRing(keyWithTags = emptyMap()) + fun new(): UniversalRing = UniversalRing(keyWithTags = emptyMap()) + /** * Join a collection of keyrings together (same as reducing with `+`). Correctly * works if there is no keyring (returns [EMPTY]), or only one keyring (returns diff --git a/src/commonMain/kotlin/net/sergeych/crypto2/kdf.kt b/src/commonMain/kotlin/net/sergeych/crypto2/kdf.kt index 2a3b490..e99dc12 100644 --- a/src/commonMain/kotlin/net/sergeych/crypto2/kdf.kt +++ b/src/commonMain/kotlin/net/sergeych/crypto2/kdf.kt @@ -47,6 +47,12 @@ sealed class KDF { fun kdfForSize(numberOfKeys: Int,salt: UByteArray = Argon.randomSalt()): KDF = creteDefault(SymmetricKey.keyLength * numberOfKeys, this, salt) + fun kdfForSizeInBytes(sizeInBytes: Int, complexity: Complexity, domain: String): KDF { + val salt = Hash.Blake2b.deriveSalt(domain, Argon.saltSize) + return creteDefault(sizeInBytes, complexity, salt) + } + + /** * Derive multiple keys from the password. Derivation params will be included in the key ids, see * [SymmetricKey.id] as [KeyId.kdp]. diff --git a/src/commonTest/kotlin/KDFTest.kt b/src/commonTest/kotlin/KDFTest.kt index ece84fe..a19385c 100644 --- a/src/commonTest/kotlin/KDFTest.kt +++ b/src/commonTest/kotlin/KDFTest.kt @@ -9,6 +9,7 @@ */ import kotlinx.coroutines.test.runTest +import net.sergeych.crypto2.Hash import net.sergeych.crypto2.KDF import net.sergeych.crypto2.initCrypto import kotlin.test.Test @@ -16,6 +17,7 @@ import kotlin.test.assertContentEquals import kotlin.test.assertEquals import kotlin.test.assertFalse import kotlin.test.assertFailsWith +import kotlin.test.assertIs class KDFTest { @Test @@ -57,6 +59,24 @@ class KDFTest { assertEquals(3, kk.size) } + @Test + fun complexityKdfForSizeInBytesTest() = runTest { + initCrypto() + val size = KDF.Argon.minKeySize + 17 + val domain = "kdf-size-in-bytes-test" + val expectedSalt = Hash.Blake2b.deriveSalt(domain, KDF.Argon.saltSize) + + val kdf = KDF.Complexity.Sensitive.kdfForSizeInBytes(size, KDF.Complexity.FixedLow, domain) + assertIs(kdf) + + assertEquals(KDF.Argon.create(KDF.Complexity.FixedLow, expectedSalt, size), kdf) + assertEquals(size, kdf.keySize) + assertContentEquals(expectedSalt, kdf.salt) + assertFalse { + kdf.salt contentEquals Hash.Blake2b.deriveSalt("$domain-other", KDF.Argon.saltSize) + } + } + @Test fun deriveFromBytesTest() = runTest { initCrypto()