diff --git a/multiplatform-crypto-delegated/src/commonMain/kotlin/com/ionspin/kotlin/crypto/CryptoProvider.kt b/multiplatform-crypto-delegated/src/commonMain/kotlin/com/ionspin/kotlin/crypto/CryptoProvider.kt index 4cde2df..fb757dd 100644 --- a/multiplatform-crypto-delegated/src/commonMain/kotlin/com/ionspin/kotlin/crypto/CryptoProvider.kt +++ b/multiplatform-crypto-delegated/src/commonMain/kotlin/com/ionspin/kotlin/crypto/CryptoProvider.kt @@ -1,5 +1,6 @@ package com.ionspin.kotlin.crypto +import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bStateless import com.ionspin.kotlin.crypto.hash.sha.Sha256Pure import com.ionspin.kotlin.crypto.hash.sha.Sha512Pure @@ -9,5 +10,6 @@ import com.ionspin.kotlin.crypto.hash.sha.Sha512Pure * on 24-May-2020 */ + typealias Sha256Stateless = Sha256Pure.Companion typealias Sha512Stateless = Sha512Pure.Companion \ No newline at end of file diff --git a/multiplatform-crypto-delegated/src/commonMain/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bDelegated.kt b/multiplatform-crypto-delegated/src/commonMain/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bDelegated.kt index ec48dfd..6b522e6 100644 --- a/multiplatform-crypto-delegated/src/commonMain/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bDelegated.kt +++ b/multiplatform-crypto-delegated/src/commonMain/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bDelegated.kt @@ -30,6 +30,6 @@ import com.ionspin.kotlin.crypto.util.rotateRight @ExperimentalUnsignedTypes expect class Blake2bDelegated(key: UByteArray? = null, hashLength: Int = 64) : Blake2b -expect class Blake2bStateless : Blake2bStatelessInterface +expect object Blake2bStateless : Blake2bStatelessInterface diff --git a/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bDelegated.kt b/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bDelegated.kt index 642d812..94b6559 100644 --- a/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bDelegated.kt +++ b/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bDelegated.kt @@ -29,7 +29,7 @@ actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: I } @ExperimentalUnsignedTypes -actual class Blake2bStateless : Blake2bStatelessInterface { +actual object Blake2bStateless : Blake2bStatelessInterface { override fun digest(inputString: String, key: String?, hashLength: Int): UByteArray { TODO("not implemented yet") } diff --git a/multiplatform-crypto-delegated/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bDelegated.kt b/multiplatform-crypto-delegated/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bDelegated.kt index 12a2555..b15cc5f 100644 --- a/multiplatform-crypto-delegated/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bDelegated.kt +++ b/multiplatform-crypto-delegated/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bDelegated.kt @@ -28,7 +28,7 @@ actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: I } } @ExperimentalUnsignedTypes -actual class Blake2bStateless : Blake2bStatelessInterface { +actual object Blake2bStateless : Blake2bStatelessInterface { override fun digest(inputString: String, key: String?, hashLength: Int): UByteArray { TODO("not implemented yet") } diff --git a/multiplatform-crypto-delegated/src/linuxMain/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bDelegated.kt b/multiplatform-crypto-delegated/src/linuxMain/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bDelegated.kt index 001b6c6..c437e85 100644 --- a/multiplatform-crypto-delegated/src/linuxMain/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bDelegated.kt +++ b/multiplatform-crypto-delegated/src/linuxMain/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bDelegated.kt @@ -1,4 +1,5 @@ package com.ionspin.kotlin.crypto.hash.blake2b +import com.ionspin.kotlin.crypto.util.toHexString import interop.* import kotlinx.cinterop.* import libsodium.* @@ -10,7 +11,7 @@ import libsodium.* @ExperimentalUnsignedTypes actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: Int) : Blake2b { - override val MAX_HASH_BYTES: Int = 1024 + override val MAX_HASH_BYTES: Int = 64 override fun update(data: UByteArray) { TODO("not implemented yet") @@ -31,16 +32,30 @@ actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: I } } -actual class Blake2bStateless : Blake2bStatelessInterface { +@Suppress("EXPERIMENTAL_API_USAGE", "EXPERIMENTAL_UNSIGNED_LITERALS") +actual object Blake2bStateless : Blake2bStatelessInterface { override fun digest(inputString: String, key: String?, hashLength: Int): UByteArray { - TODO("not implemented yet") + return memScoped { + val hashResult = UByteArray(MAX_HASH_BYTES) + val hashPointer = hashResult.toCValues().getPointer(this) + crypto_generichash( + hashPointer, + hashLength.toULong(), + inputString.cstr.getPointer(this).reinterpret(), + inputString.length.toULong(), key?.cstr?.getPointer(this)?.reinterpret(), + key?.length?.toULong() ?: 0UL + ) + println("HashPointer: ${hashPointer.readBytes(MAX_HASH_BYTES).toUByteArray().toHexString()}") + println(hashResult.toHexString()) + hashResult + } + } override fun digest(inputMessage: UByteArray, key: UByteArray, hashLength: Int): UByteArray { TODO("not implemented yet") } - override val MAX_HASH_BYTES: Int - get() = TODO("not implemented yet") + override val MAX_HASH_BYTES: Int = 64 } \ No newline at end of file diff --git a/multiplatform-crypto-delegated/src/linuxTest/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bLinuxTest.kt b/multiplatform-crypto-delegated/src/linuxTest/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bLinuxTest.kt index d2d82a0..c9a5298 100644 --- a/multiplatform-crypto-delegated/src/linuxTest/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bLinuxTest.kt +++ b/multiplatform-crypto-delegated/src/linuxTest/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bLinuxTest.kt @@ -20,4 +20,9 @@ class Blake2bLinuxTest { println("Sodium init $sodiumInitResult") println("1") } + + @Test + fun testBlake2BSodiumInterop() { + Blake2bStateless.digest("test") + } } \ No newline at end of file diff --git a/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/ReadmeTest.kt b/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/ReadmeTest.kt index fc42829..51808f6 100644 --- a/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/ReadmeTest.kt +++ b/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/ReadmeTest.kt @@ -22,6 +22,7 @@ import com.ionspin.kotlin.crypto.hash.sha.Sha512Pure import com.ionspin.kotlin.crypto.keyderivation.argon2.Argon2Pure import com.ionspin.kotlin.crypto.keyderivation.argon2.ArgonType import com.ionspin.kotlin.crypto.util.testBlocking +import com.ionspin.kotlin.crypto.util.toHexString import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertTrue @@ -155,5 +156,11 @@ class ReadmeTest { } + @Test + fun debugTest() { + val result = Blake2bStateless.digest("test") + println(result.toHexString()) + } + } \ No newline at end of file