Adding data classes
This commit is contained in:
parent
fcc4d87610
commit
a5b20daf5a
@ -1,30 +0,0 @@
|
|||||||
package debug.test
|
|
||||||
|
|
||||||
import kotlin.Int
|
|
||||||
import kotlin.UByteArray
|
|
||||||
|
|
||||||
expect class Sha256State
|
|
||||||
|
|
||||||
expect class Sha512State
|
|
||||||
|
|
||||||
expect class GenericHashState
|
|
||||||
|
|
||||||
expect class Crypto internal constructor() {
|
|
||||||
/**
|
|
||||||
* Initialize the SHA256 hash
|
|
||||||
* returns sha 256 state
|
|
||||||
*/
|
|
||||||
fun crypto_hash_sha256_init(): Sha256State
|
|
||||||
|
|
||||||
fun crypto_hash_sha256_update(state: Sha256State, input: UByteArray)
|
|
||||||
|
|
||||||
fun crypto_hash_sha256_final(state: Sha256State): UByteArray
|
|
||||||
|
|
||||||
fun crypto_hash_sha512_init(): Sha512State
|
|
||||||
|
|
||||||
fun crypto_hash_sha512_update(state: Sha512State, input: UByteArray)
|
|
||||||
|
|
||||||
fun crypto_hash_sha512_final(state: Sha512State): UByteArray
|
|
||||||
|
|
||||||
fun crypto_generichash_init(key: UByteArray, outlen: Int): GenericHashState
|
|
||||||
}
|
|
@ -1,57 +0,0 @@
|
|||||||
package debug.test
|
|
||||||
|
|
||||||
import com.ionspin.kotlin.crypto.getSodium
|
|
||||||
import ext.libsodium.com.ionspin.kotlin.crypto.toUByteArray
|
|
||||||
import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array
|
|
||||||
import kotlin.Any
|
|
||||||
import kotlin.Int
|
|
||||||
import kotlin.UByteArray
|
|
||||||
|
|
||||||
actual typealias Sha256State = Any
|
|
||||||
|
|
||||||
actual typealias Sha512State = Any
|
|
||||||
|
|
||||||
actual typealias GenericHashState = Any
|
|
||||||
|
|
||||||
actual class Crypto internal actual constructor() {
|
|
||||||
/**
|
|
||||||
* Initialize the SHA256 hash
|
|
||||||
* returns sha 256 state
|
|
||||||
*/
|
|
||||||
actual fun crypto_hash_sha256_init(): dynamic {
|
|
||||||
println("Debug crypto_hash_sha256_init")
|
|
||||||
val result = js("getSodium().crypto_hash_sha256_init()")
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
actual fun crypto_hash_sha256_update(state: Sha256State, input: UByteArray) {
|
|
||||||
println("Debug crypto_hash_sha256_update")
|
|
||||||
getSodium().crypto_hash_sha256_update(state, input.toUInt8Array(), )
|
|
||||||
}
|
|
||||||
|
|
||||||
actual fun crypto_hash_sha256_final(state: Sha256State): UByteArray {
|
|
||||||
println("Debug crypto_hash_sha256_final")
|
|
||||||
return getSodium().crypto_hash_sha256_final(state).toUByteArray()
|
|
||||||
}
|
|
||||||
|
|
||||||
actual fun crypto_hash_sha512_init(): dynamic {
|
|
||||||
println("Debug crypto_hash_sha512_init")
|
|
||||||
val result = js("getSodium().crypto_hash_sha512_init()")
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
actual fun crypto_hash_sha512_update(state: Sha512State, input: UByteArray) {
|
|
||||||
println("Debug crypto_hash_sha512_update")
|
|
||||||
getSodium().crypto_hash_sha512_update(state, input.toUInt8Array(), )
|
|
||||||
}
|
|
||||||
|
|
||||||
actual fun crypto_hash_sha512_final(state: Sha512State): UByteArray {
|
|
||||||
println("Debug crypto_hash_sha512_final")
|
|
||||||
return getSodium().crypto_hash_sha512_final(state).toUByteArray()
|
|
||||||
}
|
|
||||||
|
|
||||||
actual fun crypto_generichash_init(key: UByteArray, outlen: Int): dynamic {
|
|
||||||
println("Debug crypto_generichash_init")
|
|
||||||
return getSodium().crypto_generichash_init(key.toUInt8Array(), outlen)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,66 +0,0 @@
|
|||||||
package debug.test
|
|
||||||
|
|
||||||
import com.goterl.lazycode.lazysodium.SodiumJava
|
|
||||||
import com.goterl.lazycode.lazysodium.interfaces.Hash
|
|
||||||
import kotlin.ByteArray
|
|
||||||
import kotlin.Int
|
|
||||||
import kotlin.UByteArray
|
|
||||||
|
|
||||||
val sodium: SodiumJava = SodiumJava()
|
|
||||||
|
|
||||||
actual typealias Sha256State = Hash.State256
|
|
||||||
|
|
||||||
actual typealias Sha512State = Hash.State512
|
|
||||||
|
|
||||||
actual typealias GenericHashState = ByteArray
|
|
||||||
|
|
||||||
actual class Crypto internal actual constructor() {
|
|
||||||
/**
|
|
||||||
* Initialize the SHA256 hash
|
|
||||||
* returns sha 256 state
|
|
||||||
*/
|
|
||||||
actual fun crypto_hash_sha256_init(): Sha256State {
|
|
||||||
val state = debug.test.Sha256State()
|
|
||||||
println("Debug crypto_hash_sha256_init")
|
|
||||||
sodium.crypto_hash_sha256_init(state)
|
|
||||||
return state
|
|
||||||
}
|
|
||||||
|
|
||||||
actual fun crypto_hash_sha256_update(state: Sha256State, input: UByteArray) {
|
|
||||||
println("Debug crypto_hash_sha256_update")
|
|
||||||
sodium.crypto_hash_sha256_update(state, input.asByteArray(), input.size.toLong())
|
|
||||||
}
|
|
||||||
|
|
||||||
actual fun crypto_hash_sha256_final(state: Sha256State): UByteArray {
|
|
||||||
val out = UByteArray(32)
|
|
||||||
println("Debug crypto_hash_sha256_final")
|
|
||||||
sodium.crypto_hash_sha256_final(state, out.asByteArray())
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
actual fun crypto_hash_sha512_init(): Sha512State {
|
|
||||||
val state = debug.test.Sha512State()
|
|
||||||
println("Debug crypto_hash_sha512_init")
|
|
||||||
sodium.crypto_hash_sha512_init(state)
|
|
||||||
return state
|
|
||||||
}
|
|
||||||
|
|
||||||
actual fun crypto_hash_sha512_update(state: Sha512State, input: UByteArray) {
|
|
||||||
println("Debug crypto_hash_sha512_update")
|
|
||||||
sodium.crypto_hash_sha512_update(state, input.asByteArray(), input.size.toLong())
|
|
||||||
}
|
|
||||||
|
|
||||||
actual fun crypto_hash_sha512_final(state: Sha512State): UByteArray {
|
|
||||||
val out = UByteArray(64)
|
|
||||||
println("Debug crypto_hash_sha512_final")
|
|
||||||
sodium.crypto_hash_sha512_final(state, out.asByteArray())
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
actual fun crypto_generichash_init(key: UByteArray, outlen: Int): GenericHashState {
|
|
||||||
val state = debug.test.GenericHashState(sodium.crypto_generichash_statebytes())
|
|
||||||
println("Debug crypto_generichash_init")
|
|
||||||
sodium.crypto_generichash_init(state, key.asByteArray(), key.size, outlen)
|
|
||||||
return state
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,92 +0,0 @@
|
|||||||
package debug.test
|
|
||||||
|
|
||||||
import kotlin.Byte
|
|
||||||
import kotlin.ByteArray
|
|
||||||
import kotlin.Int
|
|
||||||
import kotlin.UByteArray
|
|
||||||
import kotlinx.cinterop.addressOf
|
|
||||||
import kotlinx.cinterop.convert
|
|
||||||
import kotlinx.cinterop.pin
|
|
||||||
import kotlinx.cinterop.pointed
|
|
||||||
import kotlinx.cinterop.ptr
|
|
||||||
import kotlinx.cinterop.reinterpret
|
|
||||||
import kotlinx.cinterop.toCValues
|
|
||||||
import libsodium.crypto_generichash_blake2b_state
|
|
||||||
import libsodium.crypto_hash_sha256_state
|
|
||||||
import libsodium.crypto_hash_sha512_state
|
|
||||||
import libsodium.sodium_malloc
|
|
||||||
|
|
||||||
actual typealias Sha256State = crypto_hash_sha256_state
|
|
||||||
|
|
||||||
actual typealias Sha512State = crypto_hash_sha512_state
|
|
||||||
|
|
||||||
actual typealias GenericHashState = crypto_generichash_blake2b_state
|
|
||||||
|
|
||||||
actual class Crypto internal actual constructor() {
|
|
||||||
val _emitByte: Byte = 0
|
|
||||||
|
|
||||||
val _emitByteArray: ByteArray = ByteArray(0)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initialize the SHA256 hash
|
|
||||||
* returns sha 256 state
|
|
||||||
*/
|
|
||||||
actual fun crypto_hash_sha256_init(): Sha256State {
|
|
||||||
val allocated = sodium_malloc(debug.test.Sha256State.size.convert())!!
|
|
||||||
val state = allocated.reinterpret<debug.test.Sha256State>().pointed
|
|
||||||
println("Debug crypto_hash_sha256_init")
|
|
||||||
libsodium.crypto_hash_sha256_init(state.ptr)
|
|
||||||
return state
|
|
||||||
}
|
|
||||||
|
|
||||||
actual fun crypto_hash_sha256_update(state: Sha256State, input: UByteArray) {
|
|
||||||
println("Debug crypto_hash_sha256_update")
|
|
||||||
val pinnedInput = input.pin()
|
|
||||||
libsodium.crypto_hash_sha256_update(state.ptr, pinnedInput.addressOf(0), input.size.convert())
|
|
||||||
pinnedInput.unpin()
|
|
||||||
}
|
|
||||||
|
|
||||||
actual fun crypto_hash_sha256_final(state: Sha256State): UByteArray {
|
|
||||||
val out = UByteArray(32)
|
|
||||||
println("Debug crypto_hash_sha256_final")
|
|
||||||
val pinnedOut = out.pin()
|
|
||||||
libsodium.crypto_hash_sha256_final(state.ptr, pinnedOut.addressOf(0))
|
|
||||||
pinnedOut.unpin()
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
actual fun crypto_hash_sha512_init(): Sha512State {
|
|
||||||
val allocated = sodium_malloc(debug.test.Sha512State.size.convert())!!
|
|
||||||
val state = allocated.reinterpret<debug.test.Sha512State>().pointed
|
|
||||||
println("Debug crypto_hash_sha512_init")
|
|
||||||
libsodium.crypto_hash_sha512_init(state.ptr)
|
|
||||||
return state
|
|
||||||
}
|
|
||||||
|
|
||||||
actual fun crypto_hash_sha512_update(state: Sha512State, input: UByteArray) {
|
|
||||||
println("Debug crypto_hash_sha512_update")
|
|
||||||
val pinnedInput = input.pin()
|
|
||||||
libsodium.crypto_hash_sha512_update(state.ptr, pinnedInput.addressOf(0), input.size.convert())
|
|
||||||
pinnedInput.unpin()
|
|
||||||
}
|
|
||||||
|
|
||||||
actual fun crypto_hash_sha512_final(state: Sha512State): UByteArray {
|
|
||||||
val out = UByteArray(64)
|
|
||||||
println("Debug crypto_hash_sha512_final")
|
|
||||||
val pinnedOut = out.pin()
|
|
||||||
libsodium.crypto_hash_sha512_final(state.ptr, pinnedOut.addressOf(0))
|
|
||||||
pinnedOut.unpin()
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
actual fun crypto_generichash_init(key: UByteArray, outlen: Int): GenericHashState {
|
|
||||||
val allocated = sodium_malloc(debug.test.GenericHashState.size.convert())!!
|
|
||||||
val state = allocated.reinterpret<debug.test.GenericHashState>().pointed
|
|
||||||
println("Debug crypto_generichash_init")
|
|
||||||
val pinnedKey = key.pin()
|
|
||||||
libsodium.crypto_generichash_init(state.ptr, pinnedKey.addressOf(0), key.size.convert(),
|
|
||||||
outlen.convert())
|
|
||||||
pinnedKey.unpin()
|
|
||||||
return state
|
|
||||||
}
|
|
||||||
}
|
|
@ -27,7 +27,8 @@ class ClassDefinition(
|
|||||||
val name: String,
|
val name: String,
|
||||||
val codeDocumentation: String = "",
|
val codeDocumentation: String = "",
|
||||||
val innerClasses: MutableList<InnerClassDefinition> = mutableListOf(),
|
val innerClasses: MutableList<InnerClassDefinition> = mutableListOf(),
|
||||||
val methods: MutableList<FunctionDefinition> = mutableListOf()
|
val methods: MutableList<FunctionDefinition> = mutableListOf(),
|
||||||
|
val dataClasses: MutableList<DataClassDefinition> = mutableListOf()
|
||||||
) {
|
) {
|
||||||
operator fun InnerClassDefinition.unaryPlus() {
|
operator fun InnerClassDefinition.unaryPlus() {
|
||||||
innerClasses.add(this)
|
innerClasses.add(this)
|
||||||
@ -37,6 +38,10 @@ class ClassDefinition(
|
|||||||
methods.add(this)
|
methods.add(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
operator fun DataClassDefinition.unaryPlus() {
|
||||||
|
dataClasses.add(this)
|
||||||
|
}
|
||||||
|
|
||||||
operator fun List<FunctionDefinition>.unaryPlus() {
|
operator fun List<FunctionDefinition>.unaryPlus() {
|
||||||
methods.addAll(this)
|
methods.addAll(this)
|
||||||
}
|
}
|
||||||
@ -51,6 +56,17 @@ class InnerClassDefinition(
|
|||||||
val functions: MutableList<FunctionDefinition> = mutableListOf()
|
val functions: MutableList<FunctionDefinition> = mutableListOf()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
class DataClassDefinition(
|
||||||
|
val name: String,
|
||||||
|
val codeDocumentation: String = "",
|
||||||
|
val parameters : List<ParameterDefinition>
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* outputLengthWhenArray - if output is an a array and there is no parameter that modifies output length we need
|
||||||
|
* to tell the function what to expect (think SHA256 32byte output always, and Blake2 dynamic output controlled
|
||||||
|
* by outputLen parameter)
|
||||||
|
*/
|
||||||
class FunctionDefinition(
|
class FunctionDefinition(
|
||||||
val name: String,
|
val name: String,
|
||||||
val javaName: String,
|
val javaName: String,
|
||||||
@ -61,7 +77,8 @@ class FunctionDefinition(
|
|||||||
val returnType: GeneralTypeDefinition,
|
val returnType: GeneralTypeDefinition,
|
||||||
val dynamicJsReturn: Boolean = false,
|
val dynamicJsReturn: Boolean = false,
|
||||||
val isStateCreationFunction: Boolean = false,
|
val isStateCreationFunction: Boolean = false,
|
||||||
val outputLengthWhenArray: Int = -1
|
val outputLengthWhenArray: Int = -1,
|
||||||
|
val customCodeBlockReplacesFunctionBody: CodeBlockDefinition? = null
|
||||||
) {
|
) {
|
||||||
operator fun ParameterDefinition.unaryPlus() {
|
operator fun ParameterDefinition.unaryPlus() {
|
||||||
parameterList.add(this)
|
parameterList.add(this)
|
||||||
@ -79,7 +96,13 @@ class ParameterDefinition(
|
|||||||
)
|
)
|
||||||
|
|
||||||
class CodeBlockDefinition(
|
class CodeBlockDefinition(
|
||||||
codeBlock: String
|
val codeBlock: String,
|
||||||
|
val applyOnTargets: Set<TargetPlatform> = setOf(
|
||||||
|
TargetPlatform.COMMON,
|
||||||
|
TargetPlatform.JVM,
|
||||||
|
TargetPlatform.JS,
|
||||||
|
TargetPlatform.NATIVE
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
interface GeneralTypeDefinition {
|
interface GeneralTypeDefinition {
|
||||||
@ -98,6 +121,10 @@ enum class TypeDefinition(override val typeName: TypeName) : GeneralTypeDefiniti
|
|||||||
UNIT(Unit::class.asTypeName())
|
UNIT(Unit::class.asTypeName())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum class TargetPlatform {
|
||||||
|
JVM, NATIVE, JS, COMMON
|
||||||
|
}
|
||||||
|
|
||||||
fun fileDef(name: String, body: KotlinFileDefinition.() -> Unit): KotlinFileDefinition {
|
fun fileDef(name: String, body: KotlinFileDefinition.() -> Unit): KotlinFileDefinition {
|
||||||
val commonKotlinFileInstance = KotlinFileDefinition(name)
|
val commonKotlinFileInstance = KotlinFileDefinition(name)
|
||||||
commonKotlinFileInstance.body()
|
commonKotlinFileInstance.body()
|
||||||
@ -105,14 +132,26 @@ fun fileDef(name: String, body: KotlinFileDefinition.() -> Unit): KotlinFileDefi
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fun classDef(name: String, codeDocumentation: String = "",body: ClassDefinition.() -> Unit): ClassDefinition {
|
fun classDef(name: String, codeDocumentation: String = "", body: ClassDefinition.() -> Unit): ClassDefinition {
|
||||||
val commonClass = ClassDefinition(name, codeDocumentation)
|
val commonClass = ClassDefinition(name, codeDocumentation)
|
||||||
commonClass.body()
|
commonClass.body()
|
||||||
return commonClass
|
return commonClass
|
||||||
}
|
}
|
||||||
|
|
||||||
fun codeBlock(codeBlock: String) : CodeBlockDefinition {
|
fun dataClassDef(name : String, codeDocumentation: String = "", parameters: List<ParameterDefinition>) : DataClassDefinition {
|
||||||
val codeBlockDefinition = CodeBlockDefinition(codeBlock)
|
return DataClassDefinition(name, codeDocumentation, parameters)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun codeBlock(
|
||||||
|
codeBlock: String,
|
||||||
|
applyOnTargets: Set<TargetPlatform> = setOf(
|
||||||
|
TargetPlatform.COMMON,
|
||||||
|
TargetPlatform.JVM,
|
||||||
|
TargetPlatform.JS,
|
||||||
|
TargetPlatform.NATIVE
|
||||||
|
)
|
||||||
|
): CodeBlockDefinition {
|
||||||
|
val codeBlockDefinition = CodeBlockDefinition(codeBlock, applyOnTargets)
|
||||||
return codeBlockDefinition
|
return codeBlockDefinition
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -123,7 +162,7 @@ fun innerClassDef(
|
|||||||
jsName: String,
|
jsName: String,
|
||||||
nativeName: String,
|
nativeName: String,
|
||||||
codeDocumentation: String = "",
|
codeDocumentation: String = "",
|
||||||
specificConstructor : String? = null,
|
specificConstructor: String? = null,
|
||||||
body: InnerClassDefinition.() -> Unit = {}
|
body: InnerClassDefinition.() -> Unit = {}
|
||||||
): InnerClassDefinition {
|
): InnerClassDefinition {
|
||||||
val genClass = InnerClassDefinition(
|
val genClass = InnerClassDefinition(
|
||||||
@ -147,6 +186,7 @@ fun funcDef(
|
|||||||
dynamicJsReturn: Boolean = false,
|
dynamicJsReturn: Boolean = false,
|
||||||
isStateCreationFunction: Boolean = false,
|
isStateCreationFunction: Boolean = false,
|
||||||
outputLengthWhenArray: Int = -1,
|
outputLengthWhenArray: Int = -1,
|
||||||
|
customCodeBlockReplacesFunctionBody: CodeBlockDefinition? = null,
|
||||||
body: FunctionDefinition.() -> Unit
|
body: FunctionDefinition.() -> Unit
|
||||||
): FunctionDefinition {
|
): FunctionDefinition {
|
||||||
val function = FunctionDefinition(
|
val function = FunctionDefinition(
|
||||||
@ -154,11 +194,12 @@ fun funcDef(
|
|||||||
javaName,
|
javaName,
|
||||||
jsName,
|
jsName,
|
||||||
nativeName,
|
nativeName,
|
||||||
codeDocumentation,
|
codeDocumentation = codeDocumentation,
|
||||||
returnType = returnType,
|
returnType = returnType,
|
||||||
dynamicJsReturn = dynamicJsReturn,
|
dynamicJsReturn = dynamicJsReturn,
|
||||||
isStateCreationFunction = isStateCreationFunction,
|
isStateCreationFunction = isStateCreationFunction,
|
||||||
outputLengthWhenArray = outputLengthWhenArray
|
outputLengthWhenArray = outputLengthWhenArray,
|
||||||
|
customCodeBlockReplacesFunctionBody = customCodeBlockReplacesFunctionBody
|
||||||
)
|
)
|
||||||
function.body()
|
function.body()
|
||||||
return function
|
return function
|
||||||
@ -171,6 +212,7 @@ fun funcDef(
|
|||||||
dynamicJsReturn: Boolean = false,
|
dynamicJsReturn: Boolean = false,
|
||||||
isStateCreationFunction: Boolean = false,
|
isStateCreationFunction: Boolean = false,
|
||||||
outputLengthWhenArray: Int = -1,
|
outputLengthWhenArray: Int = -1,
|
||||||
|
customCodeBlockReplacesFunctionBody: CodeBlockDefinition? = null,
|
||||||
body: FunctionDefinition.() -> Unit
|
body: FunctionDefinition.() -> Unit
|
||||||
): FunctionDefinition {
|
): FunctionDefinition {
|
||||||
val function =
|
val function =
|
||||||
@ -183,7 +225,8 @@ fun funcDef(
|
|||||||
returnType = returnType,
|
returnType = returnType,
|
||||||
dynamicJsReturn = dynamicJsReturn,
|
dynamicJsReturn = dynamicJsReturn,
|
||||||
isStateCreationFunction = isStateCreationFunction,
|
isStateCreationFunction = isStateCreationFunction,
|
||||||
outputLengthWhenArray = outputLengthWhenArray
|
outputLengthWhenArray = outputLengthWhenArray,
|
||||||
|
customCodeBlockReplacesFunctionBody = customCodeBlockReplacesFunctionBody
|
||||||
)
|
)
|
||||||
function.body()
|
function.body()
|
||||||
return function
|
return function
|
||||||
|
@ -10,6 +10,7 @@ object LibSodiumDefinitions {
|
|||||||
+classDef("Crypto") {
|
+classDef("Crypto") {
|
||||||
defineHashFunctions()
|
defineHashFunctions()
|
||||||
defineGenericHashFunctions()
|
defineGenericHashFunctions()
|
||||||
|
defineSecretStreamFunctions()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,17 +12,33 @@ fun ClassDefinition.defineSecretStreamFunctions() {
|
|||||||
"SecretStreamState",
|
"SecretStreamState",
|
||||||
"crypto_hash_sha256_state"
|
"crypto_hash_sha256_state"
|
||||||
)
|
)
|
||||||
|
+dataClassDef(
|
||||||
|
"SecretStreamStateAndHeader",
|
||||||
|
"""
|
||||||
|
This data class wraps the state and header objects returned when initializing secret stream encryption
|
||||||
|
""".trimIndent(),
|
||||||
|
listOf(
|
||||||
|
ParameterDefinition(
|
||||||
|
parameterName = "state",
|
||||||
|
parameterType = CustomTypeDefinition(withPackageName("SecretStreamState"))
|
||||||
|
),
|
||||||
|
ParameterDefinition(
|
||||||
|
parameterName = "header",
|
||||||
|
parameterType = TypeDefinition.ARRAY_OF_UBYTES
|
||||||
|
)
|
||||||
|
|
||||||
|
)
|
||||||
|
|
||||||
|
)
|
||||||
+funcDef(
|
+funcDef(
|
||||||
"crypto_secretstream_xchacha20poly1305_init_push",
|
"crypto_secretstream_xchacha20poly1305_init_push",
|
||||||
returnType = TypeDefinition.ARRAY_OF_UBYTES,
|
returnType = TypeDefinition.ARRAY_OF_UBYTES_NO_SIZE,
|
||||||
dynamicJsReturn = true,
|
dynamicJsReturn = true,
|
||||||
isStateCreationFunction = true
|
isStateCreationFunction = true
|
||||||
) {
|
) {
|
||||||
+ParameterDefinition(
|
+ParameterDefinition(
|
||||||
"state",
|
"key",
|
||||||
CustomTypeDefinition((withPackageName("SecretStreamState"))),
|
parameterType = TypeDefinition.ARRAY_OF_UBYTES_NO_SIZE
|
||||||
dropParameterFromDefinition = true,
|
|
||||||
isActuallyAnOutputParam = true
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,8 +10,8 @@ import java.io.File
|
|||||||
* on 31-Jul-2020
|
* on 31-Jul-2020
|
||||||
*/
|
*/
|
||||||
object Coordinator {
|
object Coordinator {
|
||||||
|
@JvmStatic
|
||||||
fun run() {
|
fun main(args : Array<String>) {
|
||||||
|
|
||||||
val commonFileSpec = CommonLibsodiumGenerator.createCommonFile(packageName, LibSodiumDefinitions.testKotlinFile)
|
val commonFileSpec = CommonLibsodiumGenerator.createCommonFile(packageName, LibSodiumDefinitions.testKotlinFile)
|
||||||
val jvmFileSpec = JvmLibsodiumGenerator.createJvmFile(packageName, LibSodiumDefinitions.testKotlinFile)
|
val jvmFileSpec = JvmLibsodiumGenerator.createJvmFile(packageName, LibSodiumDefinitions.testKotlinFile)
|
||||||
|
@ -97,6 +97,10 @@ object JsLibsodiumGenerator {
|
|||||||
methodBuilder.modifiers += MultiplatformModifier.ACTUAL.modifierList
|
methodBuilder.modifiers += MultiplatformModifier.ACTUAL.modifierList
|
||||||
methodBuilder.addStatement("println(\"Debug ${methodDefinition.name}\")")
|
methodBuilder.addStatement("println(\"Debug ${methodDefinition.name}\")")
|
||||||
val constructJsCall = StringBuilder()
|
val constructJsCall = StringBuilder()
|
||||||
|
if (methodDefinition.customCodeBlockReplacesFunctionBody != null &&
|
||||||
|
methodDefinition.customCodeBlockReplacesFunctionBody.applyOnTargets.contains(TargetPlatform.JS)) {
|
||||||
|
constructJsCall.append(methodDefinition.customCodeBlockReplacesFunctionBody.codeBlock)
|
||||||
|
} else {
|
||||||
when (methodDefinition.returnType) {
|
when (methodDefinition.returnType) {
|
||||||
TypeDefinition.ARRAY_OF_UBYTES -> {
|
TypeDefinition.ARRAY_OF_UBYTES -> {
|
||||||
constructJsCall.append("return getSodium().${methodDefinition.jsName}")
|
constructJsCall.append("return getSodium().${methodDefinition.jsName}")
|
||||||
@ -120,6 +124,7 @@ object JsLibsodiumGenerator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
methodBuilder.addStatement(constructJsCall.toString())
|
methodBuilder.addStatement(constructJsCall.toString())
|
||||||
return methodBuilder
|
return methodBuilder
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,6 @@
|
|||||||
package com.ionspin.kotlin.crypto.generator.libsodium.generator
|
package com.ionspin.kotlin.crypto.generator.libsodium.generator
|
||||||
|
|
||||||
import com.ionspin.kotlin.crypto.generator.libsodium.definitions.CustomTypeDefinition
|
import com.ionspin.kotlin.crypto.generator.libsodium.definitions.*
|
||||||
import com.ionspin.kotlin.crypto.generator.libsodium.definitions.FunctionDefinition
|
|
||||||
import com.ionspin.kotlin.crypto.generator.libsodium.definitions.InnerClassDefinition
|
|
||||||
import com.ionspin.kotlin.crypto.generator.libsodium.definitions.KotlinFileDefinition
|
|
||||||
import com.ionspin.kotlin.crypto.generator.libsodium.definitions.ParameterDefinition
|
|
||||||
import com.ionspin.kotlin.crypto.generator.libsodium.definitions.TypeDefinition
|
|
||||||
import com.squareup.kotlinpoet.ClassName
|
import com.squareup.kotlinpoet.ClassName
|
||||||
import com.squareup.kotlinpoet.CodeBlock
|
import com.squareup.kotlinpoet.CodeBlock
|
||||||
import com.squareup.kotlinpoet.FileSpec
|
import com.squareup.kotlinpoet.FileSpec
|
||||||
@ -103,6 +98,10 @@ object JvmLibsodiumGenerator {
|
|||||||
}
|
}
|
||||||
methodBuilder.addStatement("println(\"Debug ${methodDefinition.name}\")")
|
methodBuilder.addStatement("println(\"Debug ${methodDefinition.name}\")")
|
||||||
val constructJvmCall = StringBuilder()
|
val constructJvmCall = StringBuilder()
|
||||||
|
if (methodDefinition.customCodeBlockReplacesFunctionBody != null &&
|
||||||
|
methodDefinition.customCodeBlockReplacesFunctionBody.applyOnTargets.contains(TargetPlatform.JVM)) {
|
||||||
|
constructJvmCall.append(methodDefinition.customCodeBlockReplacesFunctionBody.codeBlock)
|
||||||
|
} else {
|
||||||
if (methodDefinition.isStateCreationFunction) {
|
if (methodDefinition.isStateCreationFunction) {
|
||||||
constructJvmCall.append("sodium.${methodDefinition.nativeName}")
|
constructJvmCall.append("sodium.${methodDefinition.nativeName}")
|
||||||
constructJvmCall.append(paramsToString(methodDefinition))
|
constructJvmCall.append(paramsToString(methodDefinition))
|
||||||
@ -140,6 +139,7 @@ object JvmLibsodiumGenerator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
methodBuilder.returns(methodDefinition.returnType.typeName)
|
methodBuilder.returns(methodDefinition.returnType.typeName)
|
||||||
return methodBuilder
|
return methodBuilder
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,6 @@
|
|||||||
package com.ionspin.kotlin.crypto.generator.libsodium.generator
|
package com.ionspin.kotlin.crypto.generator.libsodium.generator
|
||||||
|
|
||||||
import com.ionspin.kotlin.crypto.generator.libsodium.definitions.CustomTypeDefinition
|
import com.ionspin.kotlin.crypto.generator.libsodium.definitions.*
|
||||||
import com.ionspin.kotlin.crypto.generator.libsodium.definitions.FunctionDefinition
|
|
||||||
import com.ionspin.kotlin.crypto.generator.libsodium.definitions.InnerClassDefinition
|
|
||||||
import com.ionspin.kotlin.crypto.generator.libsodium.definitions.KotlinFileDefinition
|
|
||||||
import com.ionspin.kotlin.crypto.generator.libsodium.definitions.ParameterDefinition
|
|
||||||
import com.ionspin.kotlin.crypto.generator.libsodium.definitions.TypeDefinition
|
|
||||||
import com.squareup.kotlinpoet.ClassName
|
import com.squareup.kotlinpoet.ClassName
|
||||||
import com.squareup.kotlinpoet.CodeBlock
|
import com.squareup.kotlinpoet.CodeBlock
|
||||||
import com.squareup.kotlinpoet.FileSpec
|
import com.squareup.kotlinpoet.FileSpec
|
||||||
@ -111,6 +106,10 @@ object NativeLibsodiumGenerator {
|
|||||||
methodBuilder.addStatement("println(\"Debug ${methodDefinition.name}\")")
|
methodBuilder.addStatement("println(\"Debug ${methodDefinition.name}\")")
|
||||||
pinParams(methodDefinition, methodBuilder)
|
pinParams(methodDefinition, methodBuilder)
|
||||||
val constructNativeCall = StringBuilder()
|
val constructNativeCall = StringBuilder()
|
||||||
|
if (methodDefinition.customCodeBlockReplacesFunctionBody != null &&
|
||||||
|
methodDefinition.customCodeBlockReplacesFunctionBody.applyOnTargets.contains(TargetPlatform.JS)) {
|
||||||
|
constructNativeCall.append(methodDefinition.customCodeBlockReplacesFunctionBody.codeBlock)
|
||||||
|
} else {
|
||||||
if (methodDefinition.isStateCreationFunction) {
|
if (methodDefinition.isStateCreationFunction) {
|
||||||
constructNativeCall.append("libsodium.${methodDefinition.nativeName}")
|
constructNativeCall.append("libsodium.${methodDefinition.nativeName}")
|
||||||
constructNativeCall.append(paramsToString(methodDefinition))
|
constructNativeCall.append(paramsToString(methodDefinition))
|
||||||
@ -156,6 +155,7 @@ object NativeLibsodiumGenerator {
|
|||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
methodBuilder.returns(methodDefinition.returnType.typeName)
|
methodBuilder.returns(methodDefinition.returnType.typeName)
|
||||||
|
|
||||||
|
@ -1,11 +1,10 @@
|
|||||||
package com.ionspin.kotlin.crypto.generator.libsodium.generator
|
package com.ionspin.kotlin.crypto.generator.libsodium.generator
|
||||||
|
|
||||||
import com.ionspin.kotlin.crypto.generator.libsodium.definitions.ClassDefinition
|
import com.ionspin.kotlin.crypto.generator.libsodium.definitions.ClassDefinition
|
||||||
|
import com.ionspin.kotlin.crypto.generator.libsodium.definitions.DataClassDefinition
|
||||||
import com.ionspin.kotlin.crypto.generator.libsodium.definitions.FunctionDefinition
|
import com.ionspin.kotlin.crypto.generator.libsodium.definitions.FunctionDefinition
|
||||||
import com.ionspin.kotlin.crypto.generator.libsodium.definitions.InnerClassDefinition
|
import com.ionspin.kotlin.crypto.generator.libsodium.definitions.InnerClassDefinition
|
||||||
import com.squareup.kotlinpoet.FunSpec
|
import com.squareup.kotlinpoet.*
|
||||||
import com.squareup.kotlinpoet.KModifier
|
|
||||||
import com.squareup.kotlinpoet.TypeSpec
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by Ugljesa Jovanovic
|
* Created by Ugljesa Jovanovic
|
||||||
@ -22,6 +21,9 @@ fun createClass(
|
|||||||
val primaryConstructor = FunSpec.constructorBuilder()
|
val primaryConstructor = FunSpec.constructorBuilder()
|
||||||
if (multiplatformModifier == MultiplatformModifier.EXPECT) {
|
if (multiplatformModifier == MultiplatformModifier.EXPECT) {
|
||||||
primaryConstructor.addModifiers(KModifier.INTERNAL)
|
primaryConstructor.addModifiers(KModifier.INTERNAL)
|
||||||
|
for (dataClassDefinition in classDefinition.dataClasses) {
|
||||||
|
generateDataClass(commonClassBuilder, dataClassDefinition)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
primaryConstructor.addModifiers(KModifier.INTERNAL, KModifier.ACTUAL)
|
primaryConstructor.addModifiers(KModifier.INTERNAL, KModifier.ACTUAL)
|
||||||
}
|
}
|
||||||
@ -36,6 +38,25 @@ fun createClass(
|
|||||||
return commonClassBuilder
|
return commonClassBuilder
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun generateDataClass(classBuilder: TypeSpec.Builder, dataClassDefinition: DataClassDefinition) {
|
||||||
|
val dataClassBuilder = TypeSpec.classBuilder(dataClassDefinition.name)
|
||||||
|
dataClassBuilder.addModifiers(KModifier.DATA)
|
||||||
|
val dataClassConstructor = FunSpec.constructorBuilder()
|
||||||
|
for (parameter in dataClassDefinition.parameters) {
|
||||||
|
val parameterBuilder = ParameterSpec.builder(parameter.parameterName, parameter.parameterType.typeName)
|
||||||
|
dataClassConstructor.addParameter(parameterBuilder.build())
|
||||||
|
}
|
||||||
|
dataClassBuilder.primaryConstructor(dataClassConstructor.build())
|
||||||
|
for (parameter in dataClassDefinition.parameters) {
|
||||||
|
dataClassBuilder.addProperty(
|
||||||
|
PropertySpec.builder(parameter.parameterName, parameter.parameterType.typeName)
|
||||||
|
.initializer(parameter.parameterName)
|
||||||
|
.build()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
classBuilder.addType(dataClassBuilder.build())
|
||||||
|
}
|
||||||
|
|
||||||
fun generateDocumentationForMethod(builder: FunSpec.Builder, methodSpec: FunctionDefinition) {
|
fun generateDocumentationForMethod(builder: FunSpec.Builder, methodSpec: FunctionDefinition) {
|
||||||
builder.addKdoc(methodSpec.codeDocumentation)
|
builder.addKdoc(methodSpec.codeDocumentation)
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
package com.ionspin.kotlin.crypto.generator
|
package com.ionspin.kotlin.crypto.generator
|
||||||
|
|
||||||
import com.ionspin.kotlin.crypto.generator.libsodium.generator.CommonLibsodiumGenerator
|
|
||||||
import com.ionspin.kotlin.crypto.generator.libsodium.definitions.LibSodiumDefinitions
|
|
||||||
import com.ionspin.kotlin.crypto.generator.libsodium.generator.Coordinator
|
import com.ionspin.kotlin.crypto.generator.libsodium.generator.Coordinator
|
||||||
|
import com.squareup.kotlinpoet.*
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -11,8 +10,5 @@ import org.junit.Test
|
|||||||
* on 31-Jul-2020
|
* on 31-Jul-2020
|
||||||
*/
|
*/
|
||||||
class DebugTest {
|
class DebugTest {
|
||||||
@Test
|
|
||||||
fun debugTest() {
|
|
||||||
Coordinator.run()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,13 @@ expect class Sha512State
|
|||||||
|
|
||||||
expect class GenericHashState
|
expect class GenericHashState
|
||||||
|
|
||||||
|
expect class SecretStreamState
|
||||||
|
|
||||||
expect class Crypto internal constructor() {
|
expect class Crypto internal constructor() {
|
||||||
|
/**
|
||||||
|
* Initialize the SHA256 hash
|
||||||
|
* returns sha 256 state
|
||||||
|
*/
|
||||||
fun crypto_hash_sha256_init(): Sha256State
|
fun crypto_hash_sha256_init(): Sha256State
|
||||||
|
|
||||||
fun crypto_hash_sha256_update(state: Sha256State, input: UByteArray)
|
fun crypto_hash_sha256_update(state: Sha256State, input: UByteArray)
|
||||||
@ -23,4 +29,11 @@ expect class Crypto internal constructor() {
|
|||||||
fun crypto_hash_sha512_final(state: Sha512State): UByteArray
|
fun crypto_hash_sha512_final(state: Sha512State): UByteArray
|
||||||
|
|
||||||
fun crypto_generichash_init(key: UByteArray, outlen: Int): GenericHashState
|
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
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,13 @@ actual typealias Sha512State = Any
|
|||||||
|
|
||||||
actual typealias GenericHashState = Any
|
actual typealias GenericHashState = Any
|
||||||
|
|
||||||
|
actual typealias SecretStreamState = Any
|
||||||
|
|
||||||
actual class Crypto internal actual constructor() {
|
actual class Crypto internal actual constructor() {
|
||||||
|
/**
|
||||||
|
* Initialize the SHA256 hash
|
||||||
|
* returns sha 256 state
|
||||||
|
*/
|
||||||
actual fun crypto_hash_sha256_init(): dynamic {
|
actual fun crypto_hash_sha256_init(): dynamic {
|
||||||
println("Debug crypto_hash_sha256_init")
|
println("Debug crypto_hash_sha256_init")
|
||||||
val result = js("getSodium().crypto_hash_sha256_init()")
|
val result = js("getSodium().crypto_hash_sha256_init()")
|
||||||
@ -50,4 +56,9 @@ actual class Crypto internal actual constructor() {
|
|||||||
println("Debug crypto_generichash_init")
|
println("Debug crypto_generichash_init")
|
||||||
return getSodium().crypto_generichash_init(key.toUInt8Array(), outlen)
|
return getSodium().crypto_generichash_init(key.toUInt8Array(), outlen)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
actual fun crypto_secretstream_xchacha20poly1305_init_push(key: UByteArray): dynamic {
|
||||||
|
println("Debug crypto_secretstream_xchacha20poly1305_init_push")
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package debug.test
|
|||||||
|
|
||||||
import com.goterl.lazycode.lazysodium.SodiumJava
|
import com.goterl.lazycode.lazysodium.SodiumJava
|
||||||
import com.goterl.lazycode.lazysodium.interfaces.Hash
|
import com.goterl.lazycode.lazysodium.interfaces.Hash
|
||||||
|
import com.goterl.lazycode.lazysodium.interfaces.SecretStream
|
||||||
import kotlin.ByteArray
|
import kotlin.ByteArray
|
||||||
import kotlin.Int
|
import kotlin.Int
|
||||||
import kotlin.UByteArray
|
import kotlin.UByteArray
|
||||||
@ -14,7 +15,13 @@ actual typealias Sha512State = Hash.State512
|
|||||||
|
|
||||||
actual typealias GenericHashState = ByteArray
|
actual typealias GenericHashState = ByteArray
|
||||||
|
|
||||||
|
actual typealias SecretStreamState = SecretStream.State
|
||||||
|
|
||||||
actual class Crypto internal actual constructor() {
|
actual class Crypto internal actual constructor() {
|
||||||
|
/**
|
||||||
|
* Initialize the SHA256 hash
|
||||||
|
* returns sha 256 state
|
||||||
|
*/
|
||||||
actual fun crypto_hash_sha256_init(): Sha256State {
|
actual fun crypto_hash_sha256_init(): Sha256State {
|
||||||
val state = debug.test.Sha256State()
|
val state = debug.test.Sha256State()
|
||||||
println("Debug crypto_hash_sha256_init")
|
println("Debug crypto_hash_sha256_init")
|
||||||
@ -59,4 +66,10 @@ actual class Crypto internal actual constructor() {
|
|||||||
sodium.crypto_generichash_init(state, key.asByteArray(), key.size, outlen)
|
sodium.crypto_generichash_init(state, key.asByteArray(), key.size, outlen)
|
||||||
return state
|
return state
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,11 +22,17 @@ actual typealias Sha512State = crypto_hash_sha512_state
|
|||||||
|
|
||||||
actual typealias GenericHashState = crypto_generichash_blake2b_state
|
actual typealias GenericHashState = crypto_generichash_blake2b_state
|
||||||
|
|
||||||
|
actual typealias SecretStreamState = crypto_hash_sha256_state
|
||||||
|
|
||||||
actual class Crypto internal actual constructor() {
|
actual class Crypto internal actual constructor() {
|
||||||
val _emitByte: Byte = 0
|
val _emitByte: Byte = 0
|
||||||
|
|
||||||
val _emitByteArray: ByteArray = ByteArray(0)
|
val _emitByteArray: ByteArray = ByteArray(0)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize the SHA256 hash
|
||||||
|
* returns sha 256 state
|
||||||
|
*/
|
||||||
actual fun crypto_hash_sha256_init(): Sha256State {
|
actual fun crypto_hash_sha256_init(): Sha256State {
|
||||||
val allocated = sodium_malloc(debug.test.Sha256State.size.convert())!!
|
val allocated = sodium_malloc(debug.test.Sha256State.size.convert())!!
|
||||||
val state = allocated.reinterpret<debug.test.Sha256State>().pointed
|
val state = allocated.reinterpret<debug.test.Sha256State>().pointed
|
||||||
@ -85,4 +91,12 @@ actual class Crypto internal actual constructor() {
|
|||||||
pinnedKey.unpin()
|
pinnedKey.unpin()
|
||||||
return state
|
return state
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user