58 lines
1.8 KiB
Kotlin
58 lines
1.8 KiB
Kotlin
import com.ionspin.kotlin.crypto.secretbox.SecretBox
|
|
import com.ionspin.kotlin.crypto.util.decodeFromUByteArray
|
|
import com.ionspin.kotlin.crypto.util.encodeToUByteArray
|
|
import kotlinx.coroutines.test.runTest
|
|
import net.sergeych.crypto2.*
|
|
import net.sergeych.utools.pack
|
|
import net.sergeych.utools.unpack
|
|
import kotlin.test.*
|
|
|
|
class KeysTest {
|
|
@Test
|
|
fun testCreationAndMap() = runTest {
|
|
initCrypto()
|
|
val (stk,pbk) = SigningKey.Secret.pair()
|
|
|
|
val x = mapOf( stk to "STK!", pbk to "PBK!")
|
|
assertEquals("STK!", x[stk])
|
|
val s1 = SigningKey.Secret(stk.packed)
|
|
assertEquals(stk, s1)
|
|
assertEquals("STK!", x[s1])
|
|
assertEquals("PBK!", x[pbk])
|
|
|
|
val data = "8 rays dev!".encodeToUByteArray()
|
|
val data1 = "8 rays dev!".encodeToUByteArray()
|
|
val s = SignedBox.Seal.create(stk, data)
|
|
assertTrue(s.verify(data))
|
|
|
|
data1[0] = 0x01u
|
|
assertFalse(s.verify(data1))
|
|
val p2 = SigningKey.Secret.pair()
|
|
val p3 = SigningKey.Secret.pair()
|
|
|
|
val ms = SignedBox(data, s1) + p2.signing
|
|
|
|
// non tampered:
|
|
val ms1 = unpack<SignedBox>(pack(ms))
|
|
assertContentEquals(data, ms1.message)
|
|
assertTrue(pbk in ms1)
|
|
assertTrue(p2.aPublic in ms1)
|
|
assertTrue(p3.aPublic !in ms1)
|
|
|
|
assertThrows<IllegalSignatureException> {
|
|
unpack<SignedBox>(pack(ms).also { it[3] = 1u })
|
|
}
|
|
}
|
|
|
|
@Test
|
|
fun secretEncryptTest() = runTest {
|
|
initCrypto()
|
|
val key = SecretBox.keygen()
|
|
val key1 = SecretBox.keygen()
|
|
assertEquals("hello", decrypt(key, encrypt(key, "hello".encodeToUByteArray())).decodeFromUByteArray())
|
|
assertThrows<DecryptionFailedException> {
|
|
decrypt(key, encrypt(key1, "hello".encodeToUByteArray())).decodeFromUByteArray()
|
|
}
|
|
}
|
|
|
|
} |