diff --git a/multiplatform-crypto-libsodium-bindings/src/commonMain/kotlin/com.ionspin.kotlin.crypto/hash/Hash.kt b/multiplatform-crypto-libsodium-bindings/src/commonMain/kotlin/com.ionspin.kotlin.crypto/hash/Hash.kt index 77a787c..f0fb7eb 100644 --- a/multiplatform-crypto-libsodium-bindings/src/commonMain/kotlin/com.ionspin.kotlin.crypto/hash/Hash.kt +++ b/multiplatform-crypto-libsodium-bindings/src/commonMain/kotlin/com.ionspin.kotlin.crypto/hash/Hash.kt @@ -15,7 +15,8 @@ expect class Sha512State expect object Hash { - fun hash(data: UByteArray) : UByteArray + //Not present in LazySodium + //fun hash(data: UByteArray) : UByteArray fun sha256(data: UByteArray) : UByteArray fun sha256Init() : Sha256State diff --git a/multiplatform-crypto-libsodium-bindings/src/commonTest/kotlin/com/ionspin/kotlin/crypto/hash/HashTest.kt b/multiplatform-crypto-libsodium-bindings/src/commonTest/kotlin/com/ionspin/kotlin/crypto/hash/HashTest.kt index b88df15..70800e3 100644 --- a/multiplatform-crypto-libsodium-bindings/src/commonTest/kotlin/com/ionspin/kotlin/crypto/hash/HashTest.kt +++ b/multiplatform-crypto-libsodium-bindings/src/commonTest/kotlin/com/ionspin/kotlin/crypto/hash/HashTest.kt @@ -13,20 +13,21 @@ import kotlin.test.assertTrue * on 31-Aug-2020 */ class HashTest { - @Test - fun hashTest() { - LibsodiumInitializer.initializeWithCallback { - val input = ("Input for various hash functions").encodeToUByteArray() - val expected = ("34fcbcdcfe9e6aa3e6d5a64649afcfafb449c4b8435a65e5e7b7c2b6af3b04da350acee" + - "838246d7c2637021def0c844fcb79ac42d6a50279f1078e535997b6e6").hexStringToUByteArray() - - val result = Hash.hash(input) - assertTrue { - result.contentEquals(expected) - } - } - - } + //Not present in Lazy sodium +// @Test +// fun hashTest() { +// LibsodiumInitializer.initializeWithCallback { +// val input = ("Input for various hash functions").encodeToUByteArray() +// val expected = ("34fcbcdcfe9e6aa3e6d5a64649afcfafb449c4b8435a65e5e7b7c2b6af3b04da350acee" + +// "838246d7c2637021def0c844fcb79ac42d6a50279f1078e535997b6e6").hexStringToUByteArray() +// +// val result = Hash.hash(input) +// assertTrue { +// result.contentEquals(expected) +// } +// } +// +// } @Test diff --git a/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/hash/Hash.kt b/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/hash/Hash.kt new file mode 100644 index 0000000..13fe9bd --- /dev/null +++ b/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/hash/Hash.kt @@ -0,0 +1,47 @@ +package com.ionspin.kotlin.crypto.hash + +import com.ionspin.kotlin.crypto.getSodium +import ext.libsodium.com.ionspin.kotlin.crypto.toUByteArray +import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array + +actual typealias Sha256State = Any +actual typealias Sha512State = Any + +actual object Hash { + + //Not present in LazySodium + //fun hash(data: UByteArray) : UByteArray + actual fun sha256(data: UByteArray): UByteArray { + return getSodium().crypto_hash_sha256(data.toUInt8Array()).toUByteArray() + } + + actual fun sha256Init(): Sha256State { + return getSodium().crypto_hash_sha256_init() + } + + actual fun sha256Update(state: Sha256State, data: UByteArray) { + getSodium().crypto_hash_sha256_update(state, data.toUInt8Array()) + } + + actual fun sha256Final(state: Sha256State): UByteArray { + return getSodium().crypto_hash_sha256_final(state).toUByteArray() + } + + actual fun sha512(data: UByteArray): UByteArray { + return getSodium().crypto_hash_sha512(data.toUInt8Array()).toUByteArray() + } + + actual fun sha512Init(): Sha512State { + return getSodium().crypto_hash_sha512_init() + } + + actual fun sha512Update(state: Sha512State, data: UByteArray) { + getSodium().crypto_hash_sha512_update(state, data.toUInt8Array()) + } + + actual fun sha512Final(state: Sha512State): UByteArray { + return getSodium().crypto_hash_sha512_final(state).toUByteArray() + } + + +} diff --git a/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/hash/Hash.kt b/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/hash/Hash.kt deleted file mode 100644 index 9746dd6..0000000 --- a/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/hash/Hash.kt +++ /dev/null @@ -1,39 +0,0 @@ -package com.ionspin.kotlin.crypto.hash - -actual object Hash { - actual fun hash(data: UByteArray): UByteArray { - TODO("not implemented yet") - } - - actual fun sha256(data: UByteArray): UByteArray { - TODO("not implemented yet") - } - - actual fun sha256Init(): Sha256State { - TODO("not implemented yet") - } - - actual fun sha256Update(state: Sha256State, data: UByteArray) { - } - - actual fun sha256Final(state: Sha256State): UByteArray { - TODO("not implemented yet") - } - - actual fun sha512(data: UByteArray): UByteArray { - TODO("not implemented yet") - } - - actual fun sha512Init(): Sha512State { - TODO("not implemented yet") - } - - actual fun sha512Update(state: Sha512State, data: UByteArray) { - } - - actual fun sha512Final(state: Sha512State): UByteArray { - TODO("not implemented yet") - } - - -} diff --git a/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/hash/HashJvm.kt b/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/hash/HashJvm.kt new file mode 100644 index 0000000..cdaf573 --- /dev/null +++ b/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/hash/HashJvm.kt @@ -0,0 +1,56 @@ +package com.ionspin.kotlin.crypto.hash + +import com.goterl.lazycode.lazysodium.interfaces.Hash +import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodium + +actual typealias Sha256State = Hash.State256 +actual typealias Sha512State = Hash.State512 + +actual object Hash { + + actual fun sha256(data: UByteArray): UByteArray { + val resultHash = UByteArray(crypto_hash_sha256_BYTES) + sodium.crypto_hash_sha256(resultHash.asByteArray(), data.asByteArray(), data.size.toLong()) + return resultHash + } + + actual fun sha256Init(): Sha256State { + val state = Hash.State256() + sodium.crypto_hash_sha256_init(state) + return state + } + + actual fun sha256Update(state: Sha256State, data: UByteArray) { + sodium.crypto_hash_sha256_update(state, data.asByteArray(), data.size.toLong()) + } + + actual fun sha256Final(state: Sha256State): UByteArray { + val resultHash = UByteArray(crypto_hash_sha256_BYTES) + sodium.crypto_hash_sha256_final(state, resultHash.asByteArray()) + return resultHash + } + + actual fun sha512(data: UByteArray): UByteArray { + val resultHash = UByteArray(crypto_hash_sha512_BYTES) + sodium.crypto_hash_sha512(resultHash.asByteArray(), data.asByteArray(), data.size.toLong()) + return resultHash + } + + actual fun sha512Init(): Sha512State { + val state = Hash.State512() + sodium.crypto_hash_sha512_init(state) + return state + } + + actual fun sha512Update(state: Sha512State, data: UByteArray) { + sodium.crypto_hash_sha512_update(state, data.asByteArray(), data.size.toLong()) + } + + actual fun sha512Final(state: Sha512State): UByteArray { + val resultHash = UByteArray(crypto_hash_sha512_BYTES) + sodium.crypto_hash_sha512_final(state, resultHash.asByteArray()) + return resultHash + } + + +} diff --git a/multiplatform-crypto-libsodium-bindings/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/hash/Hash.kt b/multiplatform-crypto-libsodium-bindings/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/hash/Hash.kt index 5b76344..25d9458 100644 --- a/multiplatform-crypto-libsodium-bindings/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/hash/Hash.kt +++ b/multiplatform-crypto-libsodium-bindings/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/hash/Hash.kt @@ -23,16 +23,17 @@ actual typealias Sha256State = crypto_hash_sha256_state actual typealias Sha512State = crypto_hash_sha512_state actual object Hash { - actual fun hash(data: UByteArray): UByteArray { - val hashResult = UByteArray(crypto_hash_BYTES) - val hashResultPinned = hashResult.pin() - val dataPinned = data.pin() - crypto_hash(hashResultPinned.toPtr(), dataPinned.toPtr(), data.size.convert()) - hashResultPinned.unpin() - dataPinned.unpin() - - return hashResult - } + //Not present in Lazy Sodium +// actual fun hash(data: UByteArray): UByteArray { +// val hashResult = UByteArray(crypto_hash_BYTES) +// val hashResultPinned = hashResult.pin() +// val dataPinned = data.pin() +// crypto_hash(hashResultPinned.toPtr(), dataPinned.toPtr(), data.size.convert()) +// hashResultPinned.unpin() +// dataPinned.unpin() +// +// return hashResult +// } actual fun sha256(data: UByteArray): UByteArray { val hashResult = UByteArray(crypto_hash_sha256_BYTES)