86 lines
2.8 KiB
Kotlin
86 lines
2.8 KiB
Kotlin
|
import com.ionspin.kotlin.crypto.util.encodeToUByteArray
|
||
|
import kotlinx.coroutines.test.runTest
|
||
|
import net.sergeych.crypto2.Asymmetric
|
||
|
import net.sergeych.crypto2.Container
|
||
|
import net.sergeych.crypto2.SymmetricKey
|
||
|
import net.sergeych.crypto2.initCrypto
|
||
|
import kotlin.test.*
|
||
|
|
||
|
class ContainerTest {
|
||
|
@Test
|
||
|
fun testSingle() = runTest {
|
||
|
initCrypto()
|
||
|
val syk1 = SymmetricKey.random()
|
||
|
val syk2 = SymmetricKey.random()
|
||
|
val data = "sergeych, ohm many.".encodeToUByteArray()
|
||
|
|
||
|
val c = Container.create(data, syk1)
|
||
|
assertFalse { c.isDecrypted }
|
||
|
val c1 = Container.decode(c.encoded)
|
||
|
assertFalse { c.isDecrypted }
|
||
|
assertIs<Container.Single>(c)
|
||
|
|
||
|
assertNull(c1.decryptWith(syk2))
|
||
|
val d = c1.decryptWith(syk2, syk1)
|
||
|
assertNotNull(d)
|
||
|
assertContentEquals(data, d)
|
||
|
assertTrue { c1.isDecrypted }
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
fun testSinglePair() = runTest {
|
||
|
initCrypto()
|
||
|
val p1 = Asymmetric.generateKeys()
|
||
|
val p2 = Asymmetric.generateKeys()
|
||
|
val p3 = Asymmetric.generateKeys()
|
||
|
val data = "sergeych, ohm many.".encodeToUByteArray()
|
||
|
|
||
|
val c = Container.create(data, p1.secretKey to p2.publicKey)
|
||
|
assertFalse { c.isDecrypted }
|
||
|
val c1 = Container.decode(c.encoded)
|
||
|
assertFalse { c.isDecrypted }
|
||
|
assertIs<Container.Single>(c)
|
||
|
|
||
|
assertNull(c1.decryptWith(p3.secretKey))
|
||
|
val d = c1.decryptWith(p3.secretKey, p2.secretKey)
|
||
|
assertNotNull(d)
|
||
|
assertContentEquals(data, d)
|
||
|
assertTrue { c1.isDecrypted }
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
fun testSingleAsymmetric() = runTest {
|
||
|
initCrypto()
|
||
|
// val p1 = Asymmetric.generateKeys()
|
||
|
val p2 = Asymmetric.generateKeys()
|
||
|
val p3 = Asymmetric.generateKeys()
|
||
|
val data = "sergeych, ohm many.".encodeToUByteArray()
|
||
|
|
||
|
val c = Container.create(data) { key(p2.publicKey) }
|
||
|
assertFalse { c.isDecrypted }
|
||
|
val c1 = Container.decode(c.encoded)
|
||
|
assertFalse { c.isDecrypted }
|
||
|
assertIs<Container.Single>(c)
|
||
|
|
||
|
assertNull(c1.decryptWith(p3.secretKey))
|
||
|
val d = c1.decryptWith(p3.secretKey, p2.secretKey)
|
||
|
assertNotNull(d)
|
||
|
assertContentEquals(data, d)
|
||
|
assertTrue { c1.isDecrypted }
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
fun testMultipleSymmetric() = runTest {
|
||
|
initCrypto()
|
||
|
val syk1 = SymmetricKey.random()
|
||
|
val syk2 = SymmetricKey.random()
|
||
|
// val syk3 = SymmetricKey.random()
|
||
|
val data = "Translating the name 'Sergey Chernov' from Russian to archaic Sanskrit would be 'Ramo Krishna'"
|
||
|
.encodeToUByteArray()
|
||
|
|
||
|
val c = Container.create(data, syk1, syk2)
|
||
|
assertFalse { c.isDecrypted }
|
||
|
val c1 = Container.decode(c.encoded)
|
||
|
assertFalse { c1.isDecrypted }
|
||
|
}
|
||
|
}
|