Adding custom code blocks

This commit is contained in:
Ugljesa Jovanovic committed 2020-08-14 14:25:26 +02:00
1 parent a5b20daf5a
commit efe9661c30
11 files changed
+87 -27

No files matched your search

@@ -11,6 +11,11 @@ expect class GenericHashState
expect class SecretStreamState
data class SecretStreamStateAndHeader(
val state: SecretStreamState,
val header: UByteArray
)
expect class Crypto internal constructor() {
/**
* Initialize the SHA256 hash
@@ -31,9 +36,4 @@ expect class Crypto internal constructor() {
fun crypto_generichash_init(key: UByteArray, outlen: Int): GenericHashState
fun crypto_secretstream_xchacha20poly1305_init_push(key: UByteArray): UByteArray
data class SecretStreamStateAndHeader(
val state: SecretStreamState,
val header: UByteArray
)
}
@@ -6,6 +6,7 @@ import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array
import kotlin.Any
import kotlin.Int
import kotlin.UByteArray
import org.khronos.webgl.Uint8Array
actual typealias Sha256State = Any
@@ -59,6 +60,10 @@ actual class Crypto internal actual constructor() {
actual fun crypto_secretstream_xchacha20poly1305_init_push(key: UByteArray): dynamic {
println("Debug crypto_secretstream_xchacha20poly1305_init_push")
val stateAndHeader =
getSodium().crypto_secretstream_xchacha20poly1305_init_push(key.toUInt8Array())
val state = stateAndHeader.state
val header = (stateAndHeader.header as Uint8Array).toUByteArray()
return SecretStreamStateAndHeader(state, header)
}
}
@@ -69,7 +69,10 @@ actual class Crypto internal actual constructor() {
actual fun crypto_secretstream_xchacha20poly1305_init_push(key: UByteArray): UByteArray {
println("Debug crypto_secretstream_xchacha20poly1305_init_push")
sodium.crypto_secretstream_xchacha20poly1305_init_push(key.asByteArray())
return state
val header = UByteArray(24)
val state = SecretStream.State()
sodium.crypto_secretstream_xchacha20poly1305_init_push(state, header.asByteArray(),
key.asByteArray())
return SecretStreamStateAndHeader(state, header)
}
}
@@ -95,8 +95,15 @@ actual class Crypto internal actual constructor() {
actual fun crypto_secretstream_xchacha20poly1305_init_push(key: UByteArray): UByteArray {
println("Debug crypto_secretstream_xchacha20poly1305_init_push")
val pinnedKey = key.pin()
libsodium.crypto_secretstream_xchacha20poly1305_init_push(pinnedKey.addressOf(0))
pinnedKey.unpin()
return state
val state =
libsodium.sodium_malloc(libsodium.crypto_secretstream_xchacha20poly1305_state.size.convert())!!
.reinterpret<libsodium.crypto_secretstream_xchacha20poly1305_state>()
.pointed
val header = UByteArray(crypto_secretstream_xchacha20poly1305_HEADERBYTES.toInt()) { 0U }
val pinnedHeader = header.pin()
libsodium.crypto_secretstream_xchacha20poly1305_init_push(state.ptr,
pinnedHeader.addressOf(0), key.toCValues())
pinnedHeader.unpin()
return SecretStreamStateAndHeader(state, header)
}
}