All platforms working with a smoke test, seems theres a bug with JS compiler (TODO in smoke test desccribes it)
This commit is contained in:
parent
1f0eaf59ca
commit
5d61858f81
@ -72,6 +72,7 @@ class ParameterDefinition(
|
|||||||
val isActuallyAnOutputParam: Boolean = false,
|
val isActuallyAnOutputParam: Boolean = false,
|
||||||
val isStateType: Boolean = false,
|
val isStateType: Boolean = false,
|
||||||
val dropParameterFromDefinition: Boolean = false,
|
val dropParameterFromDefinition: Boolean = false,
|
||||||
|
val specificJvmInitializer: String? = null,
|
||||||
)
|
)
|
||||||
|
|
||||||
interface GeneralTypeDefinition {
|
interface GeneralTypeDefinition {
|
||||||
@ -108,6 +109,7 @@ fun innerClassDef(
|
|||||||
javaName: String,
|
javaName: String,
|
||||||
jsName: String,
|
jsName: String,
|
||||||
nativeName: String,
|
nativeName: String,
|
||||||
|
specificConstructor : String? = null,
|
||||||
body: InnerClassDefinition.() -> Unit = {}
|
body: InnerClassDefinition.() -> Unit = {}
|
||||||
): InnerClassDefinition {
|
): InnerClassDefinition {
|
||||||
val genClass = InnerClassDefinition(
|
val genClass = InnerClassDefinition(
|
||||||
|
@ -14,7 +14,7 @@ fun ClassDefinition.defineGenericHashFunctions() {
|
|||||||
|
|
||||||
+innerClassDef(
|
+innerClassDef(
|
||||||
"GenericHashState",
|
"GenericHashState",
|
||||||
"ByteArray",
|
"kotlin.ByteArray",
|
||||||
"Uint8Array",
|
"Uint8Array",
|
||||||
"crypto_generichash_blake2b_state"
|
"crypto_generichash_blake2b_state"
|
||||||
)
|
)
|
||||||
@ -29,7 +29,8 @@ fun ClassDefinition.defineGenericHashFunctions() {
|
|||||||
"state",
|
"state",
|
||||||
CustomTypeDefinition((withPackageName("GenericHashState"))),
|
CustomTypeDefinition((withPackageName("GenericHashState"))),
|
||||||
isStateType = true,
|
isStateType = true,
|
||||||
dropParameterFromDefinition = true
|
dropParameterFromDefinition = true,
|
||||||
|
specificJvmInitializer = "sodium.crypto_generichash_statebytes()"
|
||||||
)
|
)
|
||||||
+ParameterDefinition("key", TypeDefinition.ARRAY_OF_UBYTES)
|
+ParameterDefinition("key", TypeDefinition.ARRAY_OF_UBYTES)
|
||||||
+ParameterDefinition("outlen", TypeDefinition.INT, modifiesReturn = true)
|
+ParameterDefinition("outlen", TypeDefinition.INT, modifiesReturn = true)
|
||||||
|
@ -18,6 +18,9 @@ fun ClassDefinition.defineHashFunctions() {
|
|||||||
"crypto_hash_sha256_state"
|
"crypto_hash_sha256_state"
|
||||||
)
|
)
|
||||||
+funcDef(
|
+funcDef(
|
||||||
|
"crypto_hash_sha256_init_spec",
|
||||||
|
"crypto_hash_sha256_init",
|
||||||
|
"crypto_hash_sha256_init",
|
||||||
"crypto_hash_sha256_init",
|
"crypto_hash_sha256_init",
|
||||||
CustomTypeDefinition(ClassName(packageName, "Sha256State")),
|
CustomTypeDefinition(ClassName(packageName, "Sha256State")),
|
||||||
dynamicJsReturn = true,
|
dynamicJsReturn = true,
|
||||||
|
@ -73,19 +73,19 @@ object JsLibsodiumGenerator {
|
|||||||
val constructJsCall = StringBuilder()
|
val constructJsCall = StringBuilder()
|
||||||
when (methodDefinition.returnType) {
|
when (methodDefinition.returnType) {
|
||||||
TypeDefinition.ARRAY_OF_UBYTES -> {
|
TypeDefinition.ARRAY_OF_UBYTES -> {
|
||||||
constructJsCall.append("return getSodium().${methodDefinition.javaName}")
|
constructJsCall.append("return getSodium().${methodDefinition.jsName}")
|
||||||
constructJsCall.append(paramsToString(methodDefinition) + ".toUByteArray()")
|
constructJsCall.append(paramsToString(methodDefinition) + ".toUByteArray()")
|
||||||
}
|
}
|
||||||
TypeDefinition.INT -> {
|
TypeDefinition.INT -> {
|
||||||
constructJsCall.append("return getSodium().${methodDefinition.javaName}")
|
constructJsCall.append("return getSodium().${methodDefinition.jsName}")
|
||||||
constructJsCall.append(paramsToString(methodDefinition))
|
constructJsCall.append(paramsToString(methodDefinition))
|
||||||
}
|
}
|
||||||
TypeDefinition.UNIT -> {
|
TypeDefinition.UNIT -> {
|
||||||
constructJsCall.append("getSodium().${methodDefinition.javaName}")
|
constructJsCall.append("getSodium().${methodDefinition.jsName}")
|
||||||
constructJsCall.append(paramsToString(methodDefinition))
|
constructJsCall.append(paramsToString(methodDefinition))
|
||||||
}
|
}
|
||||||
is CustomTypeDefinition -> {
|
is CustomTypeDefinition -> {
|
||||||
constructJsCall.append("return getSodium().${methodDefinition.javaName}")
|
constructJsCall.append("return getSodium().${methodDefinition.jsName}")
|
||||||
constructJsCall.append(paramsToString(methodDefinition))
|
constructJsCall.append(paramsToString(methodDefinition))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,18 @@
|
|||||||
package com.ionspin.kotlin.crypto.generator.libsodium.generator
|
package com.ionspin.kotlin.crypto.generator.libsodium.generator
|
||||||
|
|
||||||
import com.ionspin.kotlin.crypto.generator.libsodium.definitions.*
|
import com.ionspin.kotlin.crypto.generator.libsodium.definitions.CustomTypeDefinition
|
||||||
import com.squareup.kotlinpoet.*
|
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.CodeBlock
|
||||||
|
import com.squareup.kotlinpoet.FileSpec
|
||||||
|
import com.squareup.kotlinpoet.FunSpec
|
||||||
|
import com.squareup.kotlinpoet.ParameterSpec
|
||||||
|
import com.squareup.kotlinpoet.PropertySpec
|
||||||
|
import com.squareup.kotlinpoet.TypeAliasSpec
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by Ugljesa Jovanovic
|
* Created by Ugljesa Jovanovic
|
||||||
@ -49,10 +60,17 @@ object JvmLibsodiumGenerator {
|
|||||||
methodBuilder.modifiers += MultiplatformModifier.ACTUAL.modifierList
|
methodBuilder.modifiers += MultiplatformModifier.ACTUAL.modifierList
|
||||||
var returnModifierFound = false
|
var returnModifierFound = false
|
||||||
var returnModifierName = ""
|
var returnModifierName = ""
|
||||||
|
lateinit var actualReturnParameterDefinition: ParameterDefinition
|
||||||
|
var actualReturnTypeFound: Boolean = false
|
||||||
for (paramDefinition in methodDefinition.parameterList) {
|
for (paramDefinition in methodDefinition.parameterList) {
|
||||||
|
if (paramDefinition.isStateType && methodDefinition.isStateCreationFunction) {
|
||||||
|
createStateParam(paramDefinition, methodBuilder)
|
||||||
|
}
|
||||||
|
if ((paramDefinition.isStateType.not() || methodDefinition.isStateCreationFunction.not()) && paramDefinition.isActuallyAnOutputParam.not()) {
|
||||||
val parameterSpec =
|
val parameterSpec =
|
||||||
ParameterSpec.builder(paramDefinition.parameterName, paramDefinition.parameterType.typeName)
|
ParameterSpec.builder(paramDefinition.parameterName, paramDefinition.parameterType.typeName)
|
||||||
methodBuilder.addParameter(parameterSpec.build())
|
methodBuilder.addParameter(parameterSpec.build())
|
||||||
|
}
|
||||||
if (paramDefinition.modifiesReturn) {
|
if (paramDefinition.modifiesReturn) {
|
||||||
if (returnModifierFound == true) {
|
if (returnModifierFound == true) {
|
||||||
throw RuntimeException("Return modifier already found")
|
throw RuntimeException("Return modifier already found")
|
||||||
@ -60,33 +78,98 @@ object JvmLibsodiumGenerator {
|
|||||||
returnModifierFound = true
|
returnModifierFound = true
|
||||||
returnModifierName = paramDefinition.parameterName
|
returnModifierName = paramDefinition.parameterName
|
||||||
}
|
}
|
||||||
|
if (paramDefinition.isActuallyAnOutputParam) {
|
||||||
|
actualReturnParameterDefinition = paramDefinition
|
||||||
|
actualReturnTypeFound = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (actualReturnTypeFound) {
|
||||||
|
if (returnModifierFound) {
|
||||||
|
createOutputParam(
|
||||||
|
actualReturnParameterDefinition,
|
||||||
|
returnModifierName,
|
||||||
|
methodBuilder
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
if (methodDefinition.outputLengthWhenArray == -1) {
|
||||||
|
throw RuntimeException("Function definition lacks a way to define output array length, function ${methodDefinition.name}")
|
||||||
|
}
|
||||||
|
createOutputParam(
|
||||||
|
actualReturnParameterDefinition,
|
||||||
|
methodDefinition.outputLengthWhenArray.toString(),
|
||||||
|
methodBuilder
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
methodBuilder.addStatement("println(\"Debug\")")
|
methodBuilder.addStatement("println(\"Debug\")")
|
||||||
val constructJvmCall = StringBuilder()
|
val constructJvmCall = StringBuilder()
|
||||||
|
if (methodDefinition.isStateCreationFunction) {
|
||||||
|
constructJvmCall.append("sodium.${methodDefinition.nativeName}")
|
||||||
|
constructJvmCall.append(paramsToString(methodDefinition))
|
||||||
|
methodBuilder.addStatement(constructJvmCall.toString())
|
||||||
|
methodBuilder.addStatement("return state")
|
||||||
|
} else if (actualReturnTypeFound) {
|
||||||
|
constructJvmCall.append("sodium.${methodDefinition.nativeName}")
|
||||||
|
constructJvmCall.append(paramsToString(methodDefinition))
|
||||||
|
methodBuilder.addStatement(constructJvmCall.toString())
|
||||||
|
methodBuilder.addStatement("return out")
|
||||||
|
} else {
|
||||||
when (methodDefinition.returnType) {
|
when (methodDefinition.returnType) {
|
||||||
TypeDefinition.ARRAY_OF_UBYTES -> {
|
TypeDefinition.ARRAY_OF_UBYTES -> {
|
||||||
constructJvmCall.append("return sodium.${methodDefinition.nativeName}")
|
constructJvmCall.append("val result = sodium.${methodDefinition.nativeName}")
|
||||||
constructJvmCall.append(paramsToString(methodDefinition))
|
constructJvmCall.append(paramsToString(methodDefinition))
|
||||||
|
methodBuilder.addStatement(constructJvmCall.toString())
|
||||||
|
methodBuilder.addStatement("return result")
|
||||||
}
|
}
|
||||||
TypeDefinition.INT -> {
|
TypeDefinition.INT -> {
|
||||||
constructJvmCall.append("return sodium.${methodDefinition.nativeName}")
|
constructJvmCall.append("val result = sodium.${methodDefinition.nativeName}")
|
||||||
constructJvmCall.append(paramsToString(methodDefinition))
|
constructJvmCall.append(paramsToString(methodDefinition))
|
||||||
|
methodBuilder.addStatement(constructJvmCall.toString())
|
||||||
|
methodBuilder.addStatement("return result")
|
||||||
}
|
}
|
||||||
TypeDefinition.UNIT -> {
|
TypeDefinition.UNIT -> {
|
||||||
constructJvmCall.append("sodium.${methodDefinition.nativeName}")
|
constructJvmCall.append("sodium.${methodDefinition.nativeName}")
|
||||||
constructJvmCall.append(paramsToString(methodDefinition))
|
constructJvmCall.append(paramsToString(methodDefinition))
|
||||||
|
methodBuilder.addStatement(constructJvmCall.toString())
|
||||||
}
|
}
|
||||||
is CustomTypeDefinition -> {
|
is CustomTypeDefinition -> {
|
||||||
constructJvmCall.append("return sodium.${methodDefinition.nativeName}")
|
constructJvmCall.append("val result = sodium.${methodDefinition.nativeName}")
|
||||||
constructJvmCall.append(paramsToString(methodDefinition))
|
constructJvmCall.append(paramsToString(methodDefinition))
|
||||||
}
|
|
||||||
}
|
|
||||||
methodBuilder.addStatement(constructJvmCall.toString())
|
methodBuilder.addStatement(constructJvmCall.toString())
|
||||||
|
methodBuilder.addStatement("return result")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
methodBuilder.returns(methodDefinition.returnType.typeName)
|
methodBuilder.returns(methodDefinition.returnType.typeName)
|
||||||
return methodBuilder.build()
|
return methodBuilder.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun createOutputParam(outputParam: ParameterDefinition, length: String?, methodBuilder: FunSpec.Builder) {
|
||||||
|
/*
|
||||||
|
val hashed = ByteArray(Sha256Properties.MAX_HASH_BYTES)
|
||||||
|
sodium.crypto_hash_sha256_final(state, hashed)
|
||||||
|
return hashed.asUByteArray()
|
||||||
|
*/
|
||||||
|
when (outputParam.parameterType) {
|
||||||
|
TypeDefinition.ARRAY_OF_UBYTES, TypeDefinition.ARRAY_OF_UBYTES_NO_SIZE, TypeDefinition.ARRAY_OF_UBYTES_LONG_SIZE -> {
|
||||||
|
methodBuilder.addStatement("val out = UByteArray($length)")
|
||||||
|
}
|
||||||
|
else -> {
|
||||||
|
throw RuntimeException("Unhandled native output param type: ${outputParam.parameterType.typeName}")
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun createStateParam(stateParameterDefinition: ParameterDefinition, methodBuilder: FunSpec.Builder) {
|
||||||
|
/*
|
||||||
|
val state = Hash.State256()
|
||||||
|
*/
|
||||||
|
val specificInitializer = stateParameterDefinition.specificJvmInitializer ?: ""
|
||||||
|
methodBuilder.addStatement("val state = ${stateParameterDefinition.parameterType.typeName}($specificInitializer)")
|
||||||
|
}
|
||||||
|
|
||||||
fun paramsToString(methodDefinition: FunctionDefinition) : String {
|
fun paramsToString(methodDefinition: FunctionDefinition) : String {
|
||||||
val paramsBuilder = StringBuilder()
|
val paramsBuilder = StringBuilder()
|
||||||
paramsBuilder.append("(")
|
paramsBuilder.append("(")
|
||||||
|
@ -49,7 +49,7 @@ object NativeLibsodiumGenerator {
|
|||||||
val byteEmitter = PropertySpec.builder("_emitByte", Byte::class.asTypeName())
|
val byteEmitter = PropertySpec.builder("_emitByte", Byte::class.asTypeName())
|
||||||
byteEmitter.initializer(CodeBlock.of("0"))
|
byteEmitter.initializer(CodeBlock.of("0"))
|
||||||
val byteArrayEmitter = PropertySpec.builder("_emitByteArray", ByteArray::class.asTypeName())
|
val byteArrayEmitter = PropertySpec.builder("_emitByteArray", ByteArray::class.asTypeName())
|
||||||
byteArrayEmitter.initializer(CodeBlock.of("ByteArray(0) {}"))
|
byteArrayEmitter.initializer(CodeBlock.of("ByteArray(0)"))
|
||||||
commonClassSpec.addProperty(byteEmitter.build())
|
commonClassSpec.addProperty(byteEmitter.build())
|
||||||
commonClassSpec.addProperty(byteArrayEmitter.build())
|
commonClassSpec.addProperty(byteArrayEmitter.build())
|
||||||
fileBuilder.addType(commonClassSpec.build())
|
fileBuilder.addType(commonClassSpec.build())
|
||||||
|
@ -4,6 +4,7 @@ import com.ionspin.kotlin.crypto.generator.libsodium.definitions.ClassDefinition
|
|||||||
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.FunSpec
|
||||||
|
import com.squareup.kotlinpoet.KModifier
|
||||||
import com.squareup.kotlinpoet.TypeSpec
|
import com.squareup.kotlinpoet.TypeSpec
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -17,6 +18,15 @@ fun createClass(
|
|||||||
methodCreator: (FunctionDefinition) -> FunSpec
|
methodCreator: (FunctionDefinition) -> FunSpec
|
||||||
): TypeSpec.Builder {
|
): TypeSpec.Builder {
|
||||||
val commonClassBuilder = TypeSpec.classBuilder(classDefinition.name)
|
val commonClassBuilder = TypeSpec.classBuilder(classDefinition.name)
|
||||||
|
// Ugly
|
||||||
|
val primaryConstructor = FunSpec.constructorBuilder()
|
||||||
|
if (multiplatformModifier == MultiplatformModifier.EXPECT) {
|
||||||
|
primaryConstructor.addModifiers(KModifier.INTERNAL)
|
||||||
|
} else {
|
||||||
|
primaryConstructor.addModifiers(KModifier.INTERNAL, KModifier.ACTUAL)
|
||||||
|
}
|
||||||
|
|
||||||
|
commonClassBuilder.primaryConstructor(primaryConstructor.build())
|
||||||
commonClassBuilder.modifiers += multiplatformModifier.modifierList
|
commonClassBuilder.modifiers += multiplatformModifier.modifierList
|
||||||
for (methodDefinition in classDefinition.methods) {
|
for (methodDefinition in classDefinition.methods) {
|
||||||
commonClassBuilder.addFunction(methodCreator(methodDefinition))
|
commonClassBuilder.addFunction(methodCreator(methodDefinition))
|
||||||
|
@ -9,8 +9,8 @@ expect class Sha512State
|
|||||||
|
|
||||||
expect class GenericHashState
|
expect class GenericHashState
|
||||||
|
|
||||||
expect class Crypto {
|
expect class Crypto internal constructor() {
|
||||||
fun crypto_hash_sha256_init(): Sha256State
|
fun crypto_hash_sha256_init_spec(): Sha256State
|
||||||
|
|
||||||
fun crypto_hash_sha256_update(state: Sha256State, input: UByteArray)
|
fun crypto_hash_sha256_update(state: Sha256State, input: UByteArray)
|
||||||
|
|
||||||
|
@ -0,0 +1,30 @@
|
|||||||
|
package com.ionspin.kotlin.crypto
|
||||||
|
|
||||||
|
import com.ionspin.kotlin.crypto.hash.encodeToUByteArray
|
||||||
|
import com.ionspin.kotlin.crypto.util.testBlocking
|
||||||
|
import com.ionspin.kotlin.crypto.util.toHexString
|
||||||
|
import debug.test.Crypto
|
||||||
|
import kotlin.test.Test
|
||||||
|
import kotlin.test.assertTrue
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Ugljesa Jovanovic
|
||||||
|
* ugljesa.jovanovic@ionspin.com
|
||||||
|
* on 08-Aug-2020
|
||||||
|
*/
|
||||||
|
class SmokeTest {
|
||||||
|
@Test
|
||||||
|
fun testIfLibraryIsNotOnFire() = testBlocking {
|
||||||
|
Initializer.initialize()
|
||||||
|
val crypto = Crypto()
|
||||||
|
val state256 = crypto.crypto_hash_sha256_init_spec() //TODO seems to be a bug in JS compiler, if we have the same method name in crypto an in JsSodiumInterface, method tries to call wrong method name (unneeded suffix _0)
|
||||||
|
crypto.crypto_hash_sha256_update(state256, "Hello".encodeToUByteArray())
|
||||||
|
val result = crypto.crypto_hash_sha256_final(state256).toHexString()
|
||||||
|
println("Result: $result")
|
||||||
|
assertTrue {
|
||||||
|
"185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969" == result
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2019 Ugljesa Jovanovic
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.ionspin.kotlin.crypto.util
|
||||||
|
|
||||||
|
import kotlin.coroutines.Continuation
|
||||||
|
import kotlin.coroutines.EmptyCoroutineContext
|
||||||
|
import kotlin.coroutines.startCoroutine
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Ugljesa Jovanovic
|
||||||
|
* ugljesa.jovanovic@ionspin.com
|
||||||
|
* on 20-Jul-2019
|
||||||
|
*/
|
||||||
|
fun testBlocking(block : suspend () -> Unit) {
|
||||||
|
val continuation = Continuation<Unit>(EmptyCoroutineContext) {
|
||||||
|
//Do nothing
|
||||||
|
if (it.isFailure) {
|
||||||
|
throw it.exceptionOrNull()!!
|
||||||
|
}
|
||||||
|
}
|
||||||
|
block.startCoroutine(continuation)
|
||||||
|
}
|
@ -13,8 +13,8 @@ actual typealias Sha512State = Any
|
|||||||
|
|
||||||
actual typealias GenericHashState = Any
|
actual typealias GenericHashState = Any
|
||||||
|
|
||||||
actual class Crypto {
|
actual class Crypto internal actual constructor() {
|
||||||
actual fun crypto_hash_sha256_init(): dynamic {
|
actual fun crypto_hash_sha256_init_spec(): dynamic {
|
||||||
println("Debug")
|
println("Debug")
|
||||||
return getSodium().crypto_hash_sha256_init()
|
return getSodium().crypto_hash_sha256_init()
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
package debug.test
|
package debug.test
|
||||||
|
|
||||||
import ByteArray
|
|
||||||
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 kotlin.ByteArray
|
||||||
import kotlin.Int
|
import kotlin.Int
|
||||||
import kotlin.UByteArray
|
import kotlin.UByteArray
|
||||||
|
|
||||||
@ -14,10 +14,12 @@ actual typealias Sha512State = Hash.State512
|
|||||||
|
|
||||||
actual typealias GenericHashState = ByteArray
|
actual typealias GenericHashState = ByteArray
|
||||||
|
|
||||||
actual class Crypto {
|
actual class Crypto internal actual constructor() {
|
||||||
actual fun crypto_hash_sha256_init(state: Sha256State): Sha256State {
|
actual fun crypto_hash_sha256_init_spec(): Sha256State {
|
||||||
|
val state = debug.test.Sha256State()
|
||||||
println("Debug")
|
println("Debug")
|
||||||
return sodium.crypto_hash_sha256_init(state)
|
sodium.crypto_hash_sha256_init(state)
|
||||||
|
return state
|
||||||
}
|
}
|
||||||
|
|
||||||
actual fun crypto_hash_sha256_update(state: Sha256State, input: UByteArray) {
|
actual fun crypto_hash_sha256_update(state: Sha256State, input: UByteArray) {
|
||||||
@ -25,14 +27,18 @@ actual class Crypto {
|
|||||||
sodium.crypto_hash_sha256_update(state, input.asByteArray(), input.size.toLong())
|
sodium.crypto_hash_sha256_update(state, input.asByteArray(), input.size.toLong())
|
||||||
}
|
}
|
||||||
|
|
||||||
actual fun crypto_hash_sha256_final(state: Sha256State, out: UByteArray): UByteArray {
|
actual fun crypto_hash_sha256_final(state: Sha256State): UByteArray {
|
||||||
|
val out = UByteArray(32)
|
||||||
println("Debug")
|
println("Debug")
|
||||||
return sodium.crypto_hash_sha256_final(state, out.asByteArray())
|
sodium.crypto_hash_sha256_final(state, out.asByteArray())
|
||||||
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
actual fun crypto_hash_sha512_init(state: Sha512State): Sha512State {
|
actual fun crypto_hash_sha512_init(): Sha512State {
|
||||||
|
val state = debug.test.Sha512State()
|
||||||
println("Debug")
|
println("Debug")
|
||||||
return sodium.crypto_hash_sha512_init(state)
|
sodium.crypto_hash_sha512_init(state)
|
||||||
|
return state
|
||||||
}
|
}
|
||||||
|
|
||||||
actual fun crypto_hash_sha512_update(state: Sha512State, input: UByteArray) {
|
actual fun crypto_hash_sha512_update(state: Sha512State, input: UByteArray) {
|
||||||
@ -40,17 +46,17 @@ actual class Crypto {
|
|||||||
sodium.crypto_hash_sha512_update(state, input.asByteArray(), input.size.toLong())
|
sodium.crypto_hash_sha512_update(state, input.asByteArray(), input.size.toLong())
|
||||||
}
|
}
|
||||||
|
|
||||||
actual fun crypto_hash_sha512_final(state: Sha512State, out: UByteArray): UByteArray {
|
actual fun crypto_hash_sha512_final(state: Sha512State): UByteArray {
|
||||||
|
val out = UByteArray(64)
|
||||||
println("Debug")
|
println("Debug")
|
||||||
return sodium.crypto_hash_sha512_final(state, out.asByteArray())
|
sodium.crypto_hash_sha512_final(state, out.asByteArray())
|
||||||
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
actual fun crypto_generichash_init(
|
actual fun crypto_generichash_init(key: UByteArray, outlen: Int): GenericHashState {
|
||||||
state: GenericHashState,
|
val state = debug.test.GenericHashState(sodium.crypto_generichash_statebytes())
|
||||||
key: UByteArray,
|
|
||||||
outlen: Int
|
|
||||||
): GenericHashState {
|
|
||||||
println("Debug")
|
println("Debug")
|
||||||
return sodium.crypto_generichash_init(state, key.asByteArray(), key.size, outlen)
|
sodium.crypto_generichash_init(state, key.asByteArray(), key.size, outlen)
|
||||||
|
return state
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,28 +22,25 @@ actual typealias Sha512State = crypto_hash_sha512_state
|
|||||||
|
|
||||||
actual typealias GenericHashState = crypto_generichash_blake2b_state
|
actual typealias GenericHashState = crypto_generichash_blake2b_state
|
||||||
|
|
||||||
actual class Crypto {
|
actual class Crypto internal actual constructor() {
|
||||||
val _emitByte: Byte = 0
|
val _emitByte: Byte = 0
|
||||||
|
|
||||||
val _emitByteArray: ByteArray = ByteArray(0) {}
|
val _emitByteArray: ByteArray = ByteArray(0)
|
||||||
|
|
||||||
actual fun crypto_hash_sha256_init(): Sha256State {
|
actual fun crypto_hash_sha256_init_spec(): 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
|
||||||
println("Debug")
|
|
||||||
libsodium.crypto_hash_sha256_init(state.ptr)
|
libsodium.crypto_hash_sha256_init(state.ptr)
|
||||||
return state
|
return state
|
||||||
}
|
}
|
||||||
|
|
||||||
actual fun crypto_hash_sha256_update(state: Sha256State, input: UByteArray) {
|
actual fun crypto_hash_sha256_update(state: Sha256State, input: UByteArray) {
|
||||||
println("Debug")
|
|
||||||
val pinnedInput = input.pin()
|
val pinnedInput = input.pin()
|
||||||
libsodium.crypto_hash_sha256_update(state.ptr, pinnedInput.addressOf(0), input.size.convert())
|
libsodium.crypto_hash_sha256_update(state.ptr, pinnedInput.addressOf(0), input.size.convert())
|
||||||
pinnedInput.unpin()
|
pinnedInput.unpin()
|
||||||
}
|
}
|
||||||
|
|
||||||
actual fun crypto_hash_sha256_final(state: Sha256State): UByteArray {
|
actual fun crypto_hash_sha256_final(state: Sha256State): UByteArray {
|
||||||
println("Debug")
|
|
||||||
val out = UByteArray(32)
|
val out = UByteArray(32)
|
||||||
val pinnedOut = out.pin()
|
val pinnedOut = out.pin()
|
||||||
libsodium.crypto_hash_sha256_final(state.ptr, pinnedOut.addressOf(0))
|
libsodium.crypto_hash_sha256_final(state.ptr, pinnedOut.addressOf(0))
|
||||||
@ -54,20 +51,17 @@ actual class Crypto {
|
|||||||
actual fun crypto_hash_sha512_init(): Sha512State {
|
actual fun crypto_hash_sha512_init(): Sha512State {
|
||||||
val allocated = sodium_malloc(debug.test.Sha512State.size.convert())!!
|
val allocated = sodium_malloc(debug.test.Sha512State.size.convert())!!
|
||||||
val state = allocated.reinterpret<debug.test.Sha512State>().pointed
|
val state = allocated.reinterpret<debug.test.Sha512State>().pointed
|
||||||
println("Debug")
|
|
||||||
libsodium.crypto_hash_sha512_init(state.ptr)
|
libsodium.crypto_hash_sha512_init(state.ptr)
|
||||||
return state
|
return state
|
||||||
}
|
}
|
||||||
|
|
||||||
actual fun crypto_hash_sha512_update(state: Sha512State, input: UByteArray) {
|
actual fun crypto_hash_sha512_update(state: Sha512State, input: UByteArray) {
|
||||||
println("Debug")
|
|
||||||
val pinnedInput = input.pin()
|
val pinnedInput = input.pin()
|
||||||
libsodium.crypto_hash_sha512_update(state.ptr, pinnedInput.addressOf(0), input.size.convert())
|
libsodium.crypto_hash_sha512_update(state.ptr, pinnedInput.addressOf(0), input.size.convert())
|
||||||
pinnedInput.unpin()
|
pinnedInput.unpin()
|
||||||
}
|
}
|
||||||
|
|
||||||
actual fun crypto_hash_sha512_final(state: Sha512State): UByteArray {
|
actual fun crypto_hash_sha512_final(state: Sha512State): UByteArray {
|
||||||
println("Debug")
|
|
||||||
val out = UByteArray(64)
|
val out = UByteArray(64)
|
||||||
val pinnedOut = out.pin()
|
val pinnedOut = out.pin()
|
||||||
libsodium.crypto_hash_sha512_final(state.ptr, pinnedOut.addressOf(0))
|
libsodium.crypto_hash_sha512_final(state.ptr, pinnedOut.addressOf(0))
|
||||||
@ -78,7 +72,6 @@ actual class Crypto {
|
|||||||
actual fun crypto_generichash_init(key: UByteArray, outlen: Int): GenericHashState {
|
actual fun crypto_generichash_init(key: UByteArray, outlen: Int): GenericHashState {
|
||||||
val allocated = sodium_malloc(debug.test.GenericHashState.size.convert())!!
|
val allocated = sodium_malloc(debug.test.GenericHashState.size.convert())!!
|
||||||
val state = allocated.reinterpret<debug.test.GenericHashState>().pointed
|
val state = allocated.reinterpret<debug.test.GenericHashState>().pointed
|
||||||
println("Debug")
|
|
||||||
val pinnedKey = key.pin()
|
val pinnedKey = key.pin()
|
||||||
libsodium.crypto_generichash_init(state.ptr, pinnedKey.addressOf(0), key.size.convert(),
|
libsodium.crypto_generichash_init(state.ptr, pinnedKey.addressOf(0), key.size.convert(),
|
||||||
outlen.convert())
|
outlen.convert())
|
||||||
|
@ -14,8 +14,13 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.ionspin.kotlin.crypto.util
|
package com.ionspin.kotlin.crypto
|
||||||
|
|
||||||
|
import com.ionspin.kotlin.crypto.util.chunked
|
||||||
|
import com.ionspin.kotlin.crypto.util.fromBigEndianArrayToULong
|
||||||
|
import com.ionspin.kotlin.crypto.util.fromLittleEndianArrayToULong
|
||||||
|
import com.ionspin.kotlin.crypto.util.toBigEndianUByteArray
|
||||||
|
import com.ionspin.kotlin.crypto.util.toLittleEndianTypedUByteArray
|
||||||
import kotlin.test.Test
|
import kotlin.test.Test
|
||||||
import kotlin.test.assertTrue
|
import kotlin.test.assertTrue
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user