Skip to content

Commit

Permalink
feat: All tests passing on all platforms
Browse files Browse the repository at this point in the history
Signed-off-by: KolbyRKunz <[email protected]>
  • Loading branch information
KolbyRKunz committed Sep 13, 2023
1 parent 58dbe2d commit a7307d8
Show file tree
Hide file tree
Showing 7 changed files with 509 additions and 491 deletions.
6 changes: 3 additions & 3 deletions wrappers/kotlin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ kotlin {
jvm{
compilations.all{
kotlinOptions.jvmTarget = "1.8"
this.kotlinOptions {
freeCompilerArgs += listOf("-Xdebug")
}
// this.kotlinOptions {
// freeCompilerArgs += listOf("-Xdebug")
// }
}
testRuns["test"].executionTask.configure{
useJUnitPlatform()
Expand Down
17 changes: 17 additions & 0 deletions wrappers/kotlin/src/commonMain/kotlin/askar/Types.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package askar

import aries_askar.AskarKeyAlg
import askar.crypto.Jwk
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

@Serializable
class ProtectedJson(val alg: String, val enc: String, val apu: String, val apv: String, val epk: Jwk) {

constructor(alg: String, enc: AskarKeyAlg, apu: String, apv: String, epk: Jwk) : this(alg, enc.name, apu, apv, epk)

override fun toString(): String {
return Json.encodeToString(this)
}
}
24 changes: 12 additions & 12 deletions wrappers/kotlin/src/commonMain/kotlin/askar/crypto/CryptoBox.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,55 +14,55 @@ class CryptoBox {
return temp.toUByteArray().toByteArray()
}

fun cryptoBox(recipientKey: AskarLocalKey, senderKey: AskarLocalKey, message: String, nonce: ByteArray): ByteArray {
fun cryptoBox(recipientKey: Key, senderKey: Key, message: String, nonce: ByteArray): ByteArray {
val messageList = message.map{
it.code.toUByte()
}
val nonceList = nonce.map{
it.toUByte()
}
return crypto.cryptoBox(recipientKey, senderKey, messageList, nonceList).toUByteArray().toByteArray()
return crypto.cryptoBox(recipientKey.handle(), senderKey.handle(), messageList, nonceList).toUByteArray().toByteArray()
}

fun cryptoBox(recipientKey: AskarLocalKey, senderKey: AskarLocalKey, message: ByteArray, nonce: ByteArray): ByteArray {
fun cryptoBox(recipientKey: Key, senderKey: Key, message: ByteArray, nonce: ByteArray): ByteArray {
val messageList = message.map{
it.toUByte()
}
val nonceList = nonce.map{
it.toUByte()
}
return crypto.cryptoBox(recipientKey, senderKey, messageList, nonceList).toUByteArray().toByteArray()
return crypto.cryptoBox(recipientKey.handle(), senderKey.handle(), messageList, nonceList).toUByteArray().toByteArray()
}

fun open(recipientKey: AskarLocalKey, senderKey: AskarLocalKey, message: ByteArray, nonce: ByteArray): ByteArray {
fun open(recipientKey: Key, senderKey: Key, message: ByteArray, nonce: ByteArray): ByteArray {
val messageList = message.map{
it.toUByte()
}
val nonceList = nonce.map{
it.toUByte()
}
return crypto.boxOpen(recipientKey, senderKey, messageList, nonceList).toUByteArray().toByteArray()
return crypto.boxOpen(recipientKey.handle(), senderKey.handle(), messageList, nonceList).toUByteArray().toByteArray()
}

fun seal(recipientKey: AskarLocalKey, message: ByteArray): ByteArray {
fun seal(recipientKey: Key, message: ByteArray): ByteArray {
val messageList = message.map {
it.toUByte()
}
return crypto.boxSeal(recipientKey, messageList).toUByteArray().toByteArray()
return crypto.boxSeal(recipientKey.handle(), messageList).toUByteArray().toByteArray()
}

fun seal(recipientKey: AskarLocalKey, message: String): ByteArray {
fun seal(recipientKey: Key, message: String): ByteArray {
val messageList = message.map {
it.code.toUByte()
}
return crypto.boxSeal(recipientKey, messageList).toUByteArray().toByteArray()
return crypto.boxSeal(recipientKey.handle(), messageList).toUByteArray().toByteArray()
}

fun sealOpen(recipientKey: AskarLocalKey, cipherText: ByteArray): ByteArray {
fun sealOpen(recipientKey: Key, cipherText: ByteArray): ByteArray {
val cipherList = cipherText.map {
it.toUByte()
}
return crypto.boxSealOpen(recipientKey, cipherList).toUByteArray().toByteArray()
return crypto.boxSealOpen(recipientKey.handle(), cipherList).toUByteArray().toByteArray()
}


Expand Down
8 changes: 4 additions & 4 deletions wrappers/kotlin/src/commonMain/kotlin/askar/crypto/Key.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ class Key(private val localKey: AskarLocalKey) {
}

fun jwkSecret(): Jwk {
val jwkStr = localKey.toJwkSecret()
return Json.decodeFromString<Jwk>(jwkStr.toString())
val jwkStr = localKey.toJwkSecret().toUByteArray().toByteArray().decodeToString()
return Json.decodeFromString<Jwk>(jwkStr)
}

fun jwkThumbprint(): String {
Expand All @@ -101,7 +101,7 @@ class Key(private val localKey: AskarLocalKey) {
fun aeadEncrypt(
message: String,
nonce: ByteArray = ByteArray(0),
aad: String = ""
aad: String
): EncryptedBuffer {
val messageList = message.map {
it.code.toUByte()
Expand Down Expand Up @@ -140,8 +140,8 @@ class Key(private val localKey: AskarLocalKey) {
): ByteArray {
return localKey.aeadDecrypt(
cipherText.toUbyteList(),
nonce.toUbyteList(),
tag.toUbyteList(),
nonce.toUbyteList(),
aad.toUbyteList()
).toUByteArray().toByteArray()
}
Expand Down
44 changes: 22 additions & 22 deletions wrappers/kotlin/src/commonTest/kotlin/CryptoBoxTest.kt
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
//import askar.crypto.CryptoBox
//import askar.crypto.Key
//import askar.enums.KeyAlgs
//import kotlinx.cinterop.toKString
//import kotlin.test.Test
//import kotlin.test.assertEquals
//
//class CryptoBoxTest {
//
// @Test
// fun seal() {
// val x25519Key = Key.generate(KeyAlgs.X25519)
//
// val message = "foobar"
// val sealed = CryptoBox.seal(x25519Key, message)
//
// val opened = CryptoBox.sealOpen(x25519Key, sealed)
// assertEquals(message, opened.toKString())
//
// x25519Key.handle().free()
// }
//}
import aries_askar.AskarKeyAlg
import askar.crypto.CryptoBox
import askar.crypto.Key

import kotlin.test.Test
import kotlin.test.assertEquals

class CryptoBoxTest {

@Test
fun seal() {
val x25519Key = Key.generate(AskarKeyAlg.X25519)

val message = "foobar"
val sealed = CryptoBox.seal(x25519Key, message)

val opened = CryptoBox.sealOpen(x25519Key, sealed)
assertEquals(message, opened.decodeToString())

x25519Key.handle().destroy()
}
}
Loading

0 comments on commit a7307d8

Please sign in to comment.