asymmetric key cryptography basics
This commit is contained in:
1 parent
382c3277af
commit
98cba5b129
5 files changed
+142
-11
No files matched your search
@@ -1,6 +1,8 @@
|
||||
import com.ionspin.kotlin.crypto.util.decodeFromUByteArray
|
||||
import com.ionspin.kotlin.crypto.util.encodeToUByteArray
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import kotlinx.serialization.encodeToString
|
||||
import kotlinx.serialization.json.Json
|
||||
import net.sergeych.crypto2.*
|
||||
import net.sergeych.utools.now
|
||||
import net.sergeych.utools.pack
|
||||
@@ -105,4 +107,36 @@ class KeysTest {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun asymmetricKeyTest() = runTest {
|
||||
initCrypto()
|
||||
val (sk0, pk0) = Asymmetric.generateKeys()
|
||||
assertEquals(pk0, sk0.publicKey)
|
||||
|
||||
val (sk1, pk1) = Asymmetric.generateKeys()
|
||||
val (sk2, pk2) = Asymmetric.generateKeys()
|
||||
|
||||
val plain = "The fake vaccine kills".encodeToUByteArray()
|
||||
|
||||
var m = Asymmetric.createMessage(sk0, pk1, plain)
|
||||
assertContentEquals(plain, m.decrypt(sk1))
|
||||
assertThrows<DecryptionFailedException> {
|
||||
assertContentEquals(plain, m.decrypt(sk2))
|
||||
}
|
||||
|
||||
}
|
||||
@Test
|
||||
fun asymmetricKeySerializationTest() = runTest {
|
||||
initCrypto()
|
||||
val (sk0, pk0) = Asymmetric.generateKeys()
|
||||
|
||||
// println(sk0.publicKey)
|
||||
val j = Json { prettyPrint = true}
|
||||
|
||||
val sk1 = j.decodeFromString<Asymmetric.SecretKey>(j.encodeToString(sk0))
|
||||
assertEquals(sk0, sk1)
|
||||
assertEquals(pk0, sk1.publicKey)
|
||||
// println(j.encodeToString(sk1))
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user