diff --git a/buildSrc/src/main/kotlin/Utils.kt b/buildSrc/src/main/kotlin/Utils.kt index 0a190bf..9db097b 100644 --- a/buildSrc/src/main/kotlin/Utils.kt +++ b/buildSrc/src/main/kotlin/Utils.kt @@ -43,6 +43,12 @@ fun KotlinMultiplatformExtension.isNotRunningInIdea(block: KotlinMultiplatformEx } } +fun KotlinMultiplatformExtension.isRunningInTravis(block: KotlinMultiplatformExtension.() -> Unit) { + if (isInTravis()) { + block(this) + } +} + fun KotlinMultiplatformExtension.runningOnLinuxx86_64(block: KotlinMultiplatformExtension.() -> Unit) { if (getHostOsName() == "linux" && getHostArchitecture() == "x86-64") { block(this) diff --git a/multiplatform-crypto-delegated/build.gradle.kts b/multiplatform-crypto-delegated/build.gradle.kts index c810422..9b1391e 100644 --- a/multiplatform-crypto-delegated/build.gradle.kts +++ b/multiplatform-crypto-delegated/build.gradle.kts @@ -61,7 +61,9 @@ kotlin { js { browser { testTask { - enabled = false //Until I sort out testing on travis + isRunningInTravis { + enabled = false //Until I sort out testing on travis + } useKarma { useChrome() } diff --git a/multiplatform-crypto-delegated/src/commonMain/kotlin/com/ionspin/kotlin/crypto/Crypto.kt b/multiplatform-crypto-delegated/src/commonMain/kotlin/com/ionspin/kotlin/crypto/Crypto.kt index 02c80fe..a513fa4 100644 --- a/multiplatform-crypto-delegated/src/commonMain/kotlin/com/ionspin/kotlin/crypto/Crypto.kt +++ b/multiplatform-crypto-delegated/src/commonMain/kotlin/com/ionspin/kotlin/crypto/Crypto.kt @@ -36,8 +36,8 @@ object Crypto : CryptoProvider { return Sha256Delegated() } - fun stateless(message: UByteArray, key: UByteArray? = null, hashLength: Int = Sha256Properties.MAX_HASH_BYTES) { - + fun stateless(message: UByteArray) : UByteArray{ + return Sha256StatelessDelegated.digest(inputMessage = message) } } @@ -46,8 +46,8 @@ object Crypto : CryptoProvider { return Sha512Delegated() } - fun stateless(message: UByteArray, key: UByteArray? = null, hashLength: Int = Sha512Properties.MAX_HASH_BYTES) { - + fun stateless(message: UByteArray) : UByteArray { + TODO() } } diff --git a/multiplatform-crypto-delegated/src/commonTest/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha256Test.kt b/multiplatform-crypto-delegated/src/commonTest/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha256Test.kt new file mode 100644 index 0000000..e6c6565 --- /dev/null +++ b/multiplatform-crypto-delegated/src/commonTest/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha256Test.kt @@ -0,0 +1,22 @@ +package com.ionspin.kotlin.crypto.hash.sha + +import com.ionspin.kotlin.crypto.Crypto +import com.ionspin.kotlin.crypto.hash.encodeToUByteArray +import com.ionspin.kotlin.crypto.util.toHexString +import kotlin.test.Test +import kotlin.test.assertTrue + +/** + * Created by Ugljesa Jovanovic + * ugljesa.jovanovic@ionspin.com + * on 07-Jun-2020 + */ +class Sha256Test { + @Test + fun statelessSimpleTest() { + val expected = "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08" + val result = Crypto.Sha256.stateless("test".encodeToUByteArray()).toHexString() + println("Result: $result") + assertTrue { result == expected } + } +} \ No newline at end of file diff --git a/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumInterface.kt b/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumInterface.kt index baf10da..bd7c81d 100644 --- a/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumInterface.kt +++ b/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumInterface.kt @@ -9,6 +9,8 @@ import org.khronos.webgl.Uint8Array */ interface JsSodiumInterface { + fun crypto_hash_sha256(message: Uint8Array) : Uint8Array + fun crypto_generichash(hashLength: Int, inputMessage: Uint8Array) : Uint8Array fun randombytes_buf(numberOfBytes: Int) : Uint8Array diff --git a/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha256Delegated.kt b/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha256Delegated.kt index 88aeb14..59646da 100644 --- a/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha256Delegated.kt +++ b/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha256Delegated.kt @@ -1,5 +1,9 @@ package com.ionspin.kotlin.crypto.hash.sha +import com.ionspin.kotlin.crypto.getSodium +import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bDelegatedStateless +import org.khronos.webgl.Uint8Array + /** * Created by Ugljesa Jovanovic * ugljesa.jovanovic@ionspin.com @@ -25,6 +29,15 @@ actual class Sha256Delegated actual constructor(key: UByteArray?, hashLength: In actual object Sha256StatelessDelegated : StatelessSha256 { override fun digest(inputMessage: UByteArray): UByteArray { - TODO("not implemented yet") + val hashed = getSodium().crypto_hash_sha256(Uint8Array(inputMessage.toByteArray().toTypedArray())) + val hash = UByteArray(MAX_HASH_BYTES) + for (i in 0 until MAX_HASH_BYTES) { + js( + """ + hash[i] = hashed[i] + """ + ) + } + return hash } } \ No newline at end of file diff --git a/multiplatform-crypto-delegated/src/jsMain/kotlin/libsodium.kt b/multiplatform-crypto-delegated/src/jsMain/kotlin/libsodium.kt index 6424781..428b3bd 100644 --- a/multiplatform-crypto-delegated/src/jsMain/kotlin/libsodium.kt +++ b/multiplatform-crypto-delegated/src/jsMain/kotlin/libsodium.kt @@ -15,5 +15,7 @@ import kotlin.js.Promise @JsName("ready") external val _libsodiumPromise : Promise -external fun crypto_generichash(hashLength: Int, inputMessage: String) : String +external fun crypto_generichash(hashLength: Int, inputMessage: Uint8Array) : Uint8Array + +external fun crypto_hash_sha256(message: Uint8Array) : Uint8Array diff --git a/multiplatform-crypto-delegated/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha256StatelessDelegated.kt b/multiplatform-crypto-delegated/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha256StatelessDelegated.kt index 29a3d57..0b2e83c 100644 --- a/multiplatform-crypto-delegated/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha256StatelessDelegated.kt +++ b/multiplatform-crypto-delegated/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha256StatelessDelegated.kt @@ -1,5 +1,6 @@ package com.ionspin.kotlin.crypto.hash.sha +import java.security.MessageDigest /** @@ -27,6 +28,8 @@ actual class Sha256Delegated actual constructor(key: UByteArray?, hashLength: In actual object Sha256StatelessDelegated : StatelessSha256 { override fun digest(inputMessage: UByteArray): UByteArray { - TODO("not implemented yet") + val messageDigest = MessageDigest.getInstance("SHA-256") + messageDigest.update(inputMessage.toByteArray()) + return messageDigest.digest().toUByteArray() } } \ No newline at end of file diff --git a/multiplatform-crypto-delegated/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha256Delegated.kt b/multiplatform-crypto-delegated/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha256Delegated.kt index c5cebe7..52fa7ce 100644 --- a/multiplatform-crypto-delegated/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha256Delegated.kt +++ b/multiplatform-crypto-delegated/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha256Delegated.kt @@ -1,7 +1,11 @@ package com.ionspin.kotlin.crypto.hash.sha import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bDelegatedStateless +import kotlinx.cinterop.addressOf +import kotlinx.cinterop.convert import kotlinx.cinterop.pin +import kotlinx.cinterop.toCValues +import libsodium.crypto_hash_sha256 /** * Created by Ugljesa Jovanovic @@ -31,10 +35,11 @@ actual class Sha256Delegated actual constructor(key: UByteArray?, hashLength: In actual object Sha256StatelessDelegated : StatelessSha256 { override fun digest(inputMessage: UByteArray): UByteArray { - val hashResult = UByteArray(Blake2bDelegatedStateless.MAX_HASH_BYTES) + val hashResult = UByteArray(MAX_HASH_BYTES) val hashResultPinned = hashResult.pin() -// crypto_ - TODO("not implemented yet") + crypto_hash_sha256(hashResultPinned.addressOf(0), inputMessage.toCValues(), inputMessage.size.convert()) + hashResultPinned.unpin() + return hashResult } } \ No newline at end of file