2024-06-19 16:46:32 +07:00
|
|
|
import com.ionspin.kotlin.crypto.util.encodeToUByteArray
|
|
|
|
import kotlinx.coroutines.test.runTest
|
2024-06-20 09:27:10 +07:00
|
|
|
import kotlinx.serialization.encodeToString
|
|
|
|
import kotlinx.serialization.json.Json
|
2024-06-19 20:17:39 +07:00
|
|
|
import net.sergeych.crypto2.*
|
2024-06-19 16:46:32 +07:00
|
|
|
import kotlin.test.*
|
|
|
|
|
|
|
|
class ContainerTest {
|
|
|
|
@Test
|
|
|
|
fun testSingle() = runTest {
|
|
|
|
initCrypto()
|
2024-06-22 18:18:14 +07:00
|
|
|
val syk1 = SymmetricKey.new()
|
|
|
|
val syk2 = SymmetricKey.new()
|
2024-06-19 16:46:32 +07:00
|
|
|
val data = "sergeych, ohm many.".encodeToUByteArray()
|
|
|
|
|
2024-06-19 20:17:39 +07:00
|
|
|
val c = Container.createWith(data, syk1)
|
2024-06-20 09:27:10 +07:00
|
|
|
assertTrue { c.isDecrypted }
|
2024-06-19 16:46:32 +07:00
|
|
|
val c1 = Container.decode(c.encoded)
|
2024-06-20 09:27:10 +07:00
|
|
|
assertFalse { c1.isDecrypted }
|
2024-06-19 16:46:32 +07:00
|
|
|
assertIs<Container.Single>(c)
|
|
|
|
|
|
|
|
assertNull(c1.decryptWith(syk2))
|
|
|
|
val d = c1.decryptWith(syk2, syk1)
|
|
|
|
assertNotNull(d)
|
|
|
|
assertContentEquals(data, d)
|
|
|
|
assertTrue { c1.isDecrypted }
|
2024-06-20 10:25:15 +07:00
|
|
|
|
|
|
|
val data2 = "To push unpushinable".encodeToUByteArray()
|
|
|
|
val c2 = Container.decode(c.updateData(data2).encoded)
|
|
|
|
assertFalse { c2.isDecrypted }
|
|
|
|
assertContentEquals(data2, c2.decryptWith(syk1))
|
2024-06-19 16:46:32 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testSinglePair() = runTest {
|
|
|
|
initCrypto()
|
|
|
|
val p1 = Asymmetric.generateKeys()
|
|
|
|
val p2 = Asymmetric.generateKeys()
|
|
|
|
val p3 = Asymmetric.generateKeys()
|
|
|
|
val data = "sergeych, ohm many.".encodeToUByteArray()
|
|
|
|
|
2024-06-19 20:17:39 +07:00
|
|
|
val c = Container.createWith(data, p1.secretKey to p2.publicKey)
|
2024-06-20 09:27:10 +07:00
|
|
|
assertTrue { c.isDecrypted }
|
2024-06-19 16:46:32 +07:00
|
|
|
val c1 = Container.decode(c.encoded)
|
2024-06-20 09:27:10 +07:00
|
|
|
assertFalse { c1.isDecrypted }
|
2024-06-19 16:46:32 +07:00
|
|
|
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 }
|
2024-08-28 09:24:20 +02:00
|
|
|
assertEquals(p2.publicKey.id, c1.decryptedWithKeyId)
|
|
|
|
assertEquals(p1.publicKey, c1.authorisedByKey)
|
2024-06-20 10:25:15 +07:00
|
|
|
|
|
|
|
val data2 = "To push unpushinable".encodeToUByteArray()
|
|
|
|
val c2 = Container.decode(c.updateData(data2).encoded)
|
|
|
|
assertFalse { c2.isDecrypted }
|
2024-08-28 09:24:20 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
@Test
|
|
|
|
fun testMultiplePair() = runTest {
|
|
|
|
initCrypto()
|
|
|
|
val p1 = Asymmetric.generateKeys()
|
|
|
|
val p2 = Asymmetric.generateKeys()
|
|
|
|
val p3 = Asymmetric.generateKeys()
|
|
|
|
val p4 = Asymmetric.generateKeys()
|
|
|
|
val data = "sergeych, ohm many.".encodeToUByteArray()
|
|
|
|
|
|
|
|
val c = Container.createWith(data, p1.secretKey to p2.publicKey, p1.secretKey to p4.publicKey)
|
|
|
|
assertTrue { c.isDecrypted }
|
|
|
|
val c1 = Container.decode(c.encoded)
|
|
|
|
assertFalse { c1.isDecrypted }
|
|
|
|
|
|
|
|
assertNull(c1.decryptWith(p3.secretKey, p1.secretKey))
|
|
|
|
val d = c1.decryptWith(p3.secretKey, p4.secretKey)
|
|
|
|
assertNotNull(d)
|
|
|
|
assertContentEquals(data, d)
|
|
|
|
assertTrue { c1.isDecrypted }
|
|
|
|
assertEquals(p4.publicKey.id, c1.decryptedWithKeyId)
|
|
|
|
assertEquals(p1.publicKey, c1.authorisedByKey)
|
|
|
|
|
|
|
|
val data2 = "To push unpushinable".encodeToUByteArray()
|
|
|
|
val c2 = Container.decode(c.updateData(data2).encoded)
|
|
|
|
assertFalse { c2.isDecrypted }
|
|
|
|
|
2024-06-19 16:46:32 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
@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) }
|
2024-06-20 09:27:10 +07:00
|
|
|
assertTrue { c.isDecrypted }
|
2024-06-19 16:46:32 +07:00
|
|
|
val c1 = Container.decode(c.encoded)
|
2024-06-20 09:27:10 +07:00
|
|
|
println(Json.encodeToString(c1))
|
|
|
|
assertFalse { c1.isDecrypted }
|
2024-06-19 16:46:32 +07:00
|
|
|
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 }
|
2024-06-20 10:25:15 +07:00
|
|
|
|
|
|
|
val data2 = "To push unpushinable".encodeToUByteArray()
|
|
|
|
val c2 = Container.decode(c.updateData(data2).encoded)
|
|
|
|
assertFalse { c2.isDecrypted }
|
|
|
|
assertContentEquals(data2, c2.decryptWith(p2.secretKey))
|
2024-06-19 16:46:32 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testMultipleSymmetric() = runTest {
|
|
|
|
initCrypto()
|
2024-06-22 18:18:14 +07:00
|
|
|
val syk1 = SymmetricKey.new()
|
|
|
|
val syk2 = SymmetricKey.new()
|
|
|
|
val syk3 = SymmetricKey.new()
|
2024-06-19 20:17:39 +07:00
|
|
|
val p1 = Asymmetric.generateKeys()
|
|
|
|
val p2 = Asymmetric.generateKeys()
|
|
|
|
val p3 = Asymmetric.generateKeys()
|
|
|
|
val p4 = Asymmetric.generateKeys()
|
2024-06-19 16:46:32 +07:00
|
|
|
val data = "Translating the name 'Sergey Chernov' from Russian to archaic Sanskrit would be 'Ramo Krishna'"
|
|
|
|
.encodeToUByteArray()
|
|
|
|
|
2024-06-19 20:17:39 +07:00
|
|
|
val c = Container.createWith(data, syk1, syk2) {
|
|
|
|
key(p1.secretKey to p3.publicKey)
|
|
|
|
key(p4.publicKey)
|
|
|
|
}
|
2024-06-20 10:25:15 +07:00
|
|
|
assertTrue { c.isDecrypted }
|
2024-06-19 20:17:39 +07:00
|
|
|
var c1 = Container.decode(c.encoded)
|
|
|
|
assertFalse { c1.isDecrypted }
|
|
|
|
|
|
|
|
assertNull(c1.decryptWith(syk3))
|
|
|
|
assertFalse { c1.isDecrypted }
|
|
|
|
|
|
|
|
assertContentEquals(data, c1.decryptWith(syk3, syk1))
|
|
|
|
assertTrue { c1.isDecrypted }
|
|
|
|
|
|
|
|
c1 = Container.decode(c.encoded)
|
|
|
|
assertFalse { c1.isDecrypted }
|
|
|
|
assertNull(c1.decryptWith(p2.secretKey, p1.secretKey))
|
|
|
|
assertContentEquals(data, c1.decryptWith(syk3, p3.secretKey))
|
|
|
|
|
|
|
|
c1 = Container.decode(c.encoded)
|
2024-06-19 16:46:32 +07:00
|
|
|
assertFalse { c1.isDecrypted }
|
2024-06-19 20:17:39 +07:00
|
|
|
assertContentEquals(data, c1.decryptWith(syk3, p4.secretKey))
|
2024-06-20 10:25:15 +07:00
|
|
|
|
|
|
|
val data2 = "To push unpushinable".encodeToUByteArray()
|
|
|
|
assertTrue { c.isDecrypted }
|
|
|
|
val c2 = Container.decode(c.updateData(data2).encoded)
|
|
|
|
assertFalse { c2.isDecrypted }
|
|
|
|
assertContentEquals(data2, c2.decryptWith(syk2))
|
|
|
|
|
2024-06-19 20:17:39 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testSingleGrowSymmetric() = runTest {
|
|
|
|
initCrypto()
|
2024-06-22 18:18:14 +07:00
|
|
|
val syk1 = SymmetricKey.new()
|
|
|
|
val syk2 = SymmetricKey.new()
|
|
|
|
val syk3 = SymmetricKey.new()
|
2024-06-19 20:17:39 +07:00
|
|
|
val p1 = Asymmetric.generateKeys()
|
|
|
|
val p3 = Asymmetric.generateKeys()
|
|
|
|
val p4 = Asymmetric.generateKeys()
|
|
|
|
val data = "Translating the name 'Sergey Chernov' from Russian to archaic Sanskrit would be 'Ramo Krishna'"
|
|
|
|
.encodeToUByteArray()
|
|
|
|
|
|
|
|
var c = Container.createWith(data, syk1)
|
2024-06-20 10:25:15 +07:00
|
|
|
assertTrue { c.isDecrypted }
|
2024-06-19 20:17:39 +07:00
|
|
|
|
|
|
|
fun expectOpen(k: DecryptingKey) {
|
|
|
|
val c1 = Container.decode(c.encoded)
|
|
|
|
assertContentEquals(data, c1.decryptWith(k))
|
|
|
|
}
|
|
|
|
|
|
|
|
fun expectNotOpen(k: DecryptingKey) {
|
|
|
|
val c1 = Container.decode(c.encoded)
|
|
|
|
assertNull(c1.decryptWith(k))
|
|
|
|
}
|
|
|
|
|
|
|
|
expectOpen(syk1)
|
|
|
|
expectNotOpen(syk2)
|
|
|
|
expectNotOpen(p3.secretKey)
|
|
|
|
|
|
|
|
c.decryptWith(syk1)
|
|
|
|
assertTrue { c.isDecrypted }
|
|
|
|
assertNotNull(c.decryptedData)
|
|
|
|
c += syk2
|
|
|
|
expectOpen(syk1)
|
|
|
|
expectOpen(syk2)
|
|
|
|
expectNotOpen(syk3)
|
|
|
|
expectNotOpen(p3.secretKey)
|
|
|
|
|
|
|
|
c.decryptWith(syk1)
|
|
|
|
c += p3.publicKey
|
|
|
|
expectOpen(syk1)
|
|
|
|
expectOpen(syk2)
|
|
|
|
expectOpen(p3.secretKey)
|
|
|
|
expectNotOpen(syk3)
|
|
|
|
expectNotOpen(p4.secretKey)
|
|
|
|
|
|
|
|
c.decryptWith(syk1)
|
|
|
|
c += p1.secretKey to p4.publicKey
|
|
|
|
expectOpen(syk1)
|
|
|
|
expectOpen(syk2)
|
|
|
|
expectOpen(p3.secretKey)
|
|
|
|
expectNotOpen(syk3)
|
|
|
|
expectOpen(p4.secretKey)
|
2024-06-20 10:25:15 +07:00
|
|
|
|
|
|
|
val data2 = "To push unpushinable".encodeToUByteArray()
|
|
|
|
assertTrue { c.isDecrypted }
|
|
|
|
val c2 = Container.decode(c.updateData(data2).encoded)
|
|
|
|
assertFalse { c2.isDecrypted }
|
|
|
|
assertContentEquals(data2, c2.decryptWith(p4.secretKey))
|
|
|
|
|
2024-06-19 20:17:39 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testSingleGrowAsymmetric() = runTest {
|
|
|
|
initCrypto()
|
2024-06-22 18:18:14 +07:00
|
|
|
val syk1 = SymmetricKey.new()
|
|
|
|
val syk2 = SymmetricKey.new()
|
2024-06-19 20:17:39 +07:00
|
|
|
val p1 = Asymmetric.generateKeys()
|
|
|
|
val p3 = Asymmetric.generateKeys()
|
|
|
|
val data = "Translating the name 'Sergey Chernov' from Russian to archaic Sanskrit would be 'Ramo Krishna'"
|
|
|
|
.encodeToUByteArray()
|
|
|
|
|
|
|
|
var c = Container.createWith(data, p1.secretKey to p3.publicKey)
|
|
|
|
|
|
|
|
fun expectOpen(k: DecryptingKey) {
|
|
|
|
val c1 = Container.decode(c.encoded)
|
|
|
|
assertContentEquals(data, c1.decryptWith(k))
|
|
|
|
}
|
|
|
|
|
|
|
|
fun expectNotOpen(k: DecryptingKey) {
|
|
|
|
val c1 = Container.decode(c.encoded)
|
|
|
|
assertNull(c1.decryptWith(k))
|
|
|
|
}
|
|
|
|
|
|
|
|
expectNotOpen(syk1)
|
|
|
|
expectOpen(p3.secretKey)
|
|
|
|
|
|
|
|
c.decryptWith(p3.secretKey)
|
|
|
|
|
|
|
|
c += syk1
|
|
|
|
expectOpen(syk1)
|
|
|
|
expectNotOpen(syk2)
|
|
|
|
expectOpen(p3.secretKey)
|
2024-06-19 16:46:32 +07:00
|
|
|
}
|
2024-06-20 10:25:15 +07:00
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testMixedOps1() = runTest {
|
|
|
|
initCrypto()
|
2024-06-22 18:18:14 +07:00
|
|
|
val syk1 = SymmetricKey.new()
|
|
|
|
val syk2 = SymmetricKey.new()
|
|
|
|
val syk3 = SymmetricKey.new()
|
2024-06-20 10:25:15 +07:00
|
|
|
val p1 = Asymmetric.generateKeys()
|
|
|
|
val p2 = Asymmetric.generateKeys()
|
|
|
|
val p3 = Asymmetric.generateKeys()
|
|
|
|
val p4 = Asymmetric.generateKeys()
|
|
|
|
val data = "Translating the name 'Sergey Chernov' from Russian to archaic Sanskrit would be 'Ramo Krishna'"
|
|
|
|
.encodeToUByteArray()
|
|
|
|
|
|
|
|
var c = Container.createWith(data, p1.secretKey to p3.publicKey)
|
|
|
|
|
|
|
|
fun expectOpen(testData: UByteArray,vararg keys: DecryptingKey) {
|
|
|
|
val c1 = Container.decode(c.encoded)
|
|
|
|
for( k in keys)
|
|
|
|
assertContentEquals(testData, c1.decryptWith(k))
|
|
|
|
}
|
|
|
|
|
|
|
|
fun expectNotOpen(vararg keys: DecryptingKey) {
|
|
|
|
val c1 = Container.decode(c.encoded)
|
|
|
|
for(k in keys) assertNull(c1.decryptWith(k))
|
|
|
|
}
|
|
|
|
|
|
|
|
expectNotOpen(syk1)
|
|
|
|
expectOpen(data, p3.secretKey)
|
|
|
|
|
|
|
|
// c = Container.decode(c.encoded)
|
|
|
|
|
|
|
|
val data2 = "Cocktails have a delicious, complex taste".encodeToUByteArray()
|
|
|
|
c = (c + syk3 + (p1.secretKey to p4.publicKey)).updateData(data2)
|
|
|
|
expectNotOpen(syk2, syk1, p1.secretKey, p2.secretKey )
|
|
|
|
expectOpen(data2, syk3, p3.secretKey, p4.secretKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testMixedOps2() = runTest {
|
|
|
|
initCrypto()
|
2024-06-22 18:18:14 +07:00
|
|
|
val syk1 = SymmetricKey.new()
|
|
|
|
val syk2 = SymmetricKey.new()
|
|
|
|
val syk3 = SymmetricKey.new()
|
2024-06-20 10:25:15 +07:00
|
|
|
val p1 = Asymmetric.generateKeys()
|
|
|
|
val p2 = Asymmetric.generateKeys()
|
|
|
|
val p3 = Asymmetric.generateKeys()
|
|
|
|
val p4 = Asymmetric.generateKeys()
|
2024-06-20 18:37:16 +07:00
|
|
|
val p5 = Asymmetric.generateKeys()
|
2024-06-20 10:25:15 +07:00
|
|
|
val data = "Translating the name 'Sergey Chernov' from Russian to archaic Sanskrit would be 'Ramo Krishna'"
|
|
|
|
.encodeToUByteArray()
|
|
|
|
|
|
|
|
var c = Container.createWith(data, p1.secretKey to p3.publicKey)
|
|
|
|
|
|
|
|
fun expectOpen(testData: UByteArray,vararg keys: DecryptingKey) {
|
|
|
|
val c1 = Container.decode(c.encoded)
|
|
|
|
for( k in keys)
|
|
|
|
assertContentEquals(testData, c1.decryptWith(k))
|
|
|
|
}
|
|
|
|
|
|
|
|
fun expectNotOpen(vararg keys: DecryptingKey) {
|
|
|
|
val c1 = Container.decode(c.encoded)
|
|
|
|
for(k in keys) assertNull(c1.decryptWith(k))
|
|
|
|
}
|
|
|
|
|
|
|
|
expectNotOpen(syk1)
|
|
|
|
expectOpen(data, p3.secretKey)
|
|
|
|
|
|
|
|
c = Container.decode(c.encoded)
|
|
|
|
c.decryptWith(p3.secretKey)
|
|
|
|
|
|
|
|
val data2 = "Cocktails have a delicious, complex taste".encodeToUByteArray()
|
2024-06-20 18:37:16 +07:00
|
|
|
c = (c + syk3 + p5.publicKey + (p1.secretKey to p4.publicKey)).updateData(data2)
|
2024-06-20 10:25:15 +07:00
|
|
|
expectNotOpen(syk2, syk1, p1.secretKey, p2.secretKey )
|
|
|
|
expectOpen(data2, syk3, p3.secretKey, p4.secretKey)
|
|
|
|
}
|
|
|
|
|
2024-06-19 16:46:32 +07:00
|
|
|
}
|