Got valid blake2b hash

This commit is contained in:
Ugljesa Jovanovic 2020-05-24 23:46:07 +02:00 committed by Ugljesa Jovanovic
parent cc2f392bb7
commit 1a89ee5154
No known key found for this signature in database
GPG Key ID: 178E6DFCECCB0E0F
7 changed files with 37 additions and 8 deletions

View File

@ -1,5 +1,6 @@
package com.ionspin.kotlin.crypto 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.Sha256Pure
import com.ionspin.kotlin.crypto.hash.sha.Sha512Pure import com.ionspin.kotlin.crypto.hash.sha.Sha512Pure
@ -9,5 +10,6 @@ import com.ionspin.kotlin.crypto.hash.sha.Sha512Pure
* on 24-May-2020 * on 24-May-2020
*/ */
typealias Sha256Stateless = Sha256Pure.Companion typealias Sha256Stateless = Sha256Pure.Companion
typealias Sha512Stateless = Sha512Pure.Companion typealias Sha512Stateless = Sha512Pure.Companion

View File

@ -30,6 +30,6 @@ import com.ionspin.kotlin.crypto.util.rotateRight
@ExperimentalUnsignedTypes @ExperimentalUnsignedTypes
expect class Blake2bDelegated(key: UByteArray? = null, hashLength: Int = 64) : Blake2b expect class Blake2bDelegated(key: UByteArray? = null, hashLength: Int = 64) : Blake2b
expect class Blake2bStateless : Blake2bStatelessInterface expect object Blake2bStateless : Blake2bStatelessInterface

View File

@ -29,7 +29,7 @@ actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: I
} }
@ExperimentalUnsignedTypes @ExperimentalUnsignedTypes
actual class Blake2bStateless : Blake2bStatelessInterface { actual object Blake2bStateless : Blake2bStatelessInterface {
override fun digest(inputString: String, key: String?, hashLength: Int): UByteArray { override fun digest(inputString: String, key: String?, hashLength: Int): UByteArray {
TODO("not implemented yet") TODO("not implemented yet")
} }

View File

@ -28,7 +28,7 @@ actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: I
} }
} }
@ExperimentalUnsignedTypes @ExperimentalUnsignedTypes
actual class Blake2bStateless : Blake2bStatelessInterface { actual object Blake2bStateless : Blake2bStatelessInterface {
override fun digest(inputString: String, key: String?, hashLength: Int): UByteArray { override fun digest(inputString: String, key: String?, hashLength: Int): UByteArray {
TODO("not implemented yet") TODO("not implemented yet")
} }

View File

@ -1,4 +1,5 @@
package com.ionspin.kotlin.crypto.hash.blake2b package com.ionspin.kotlin.crypto.hash.blake2b
import com.ionspin.kotlin.crypto.util.toHexString
import interop.* import interop.*
import kotlinx.cinterop.* import kotlinx.cinterop.*
import libsodium.* import libsodium.*
@ -10,7 +11,7 @@ import libsodium.*
@ExperimentalUnsignedTypes @ExperimentalUnsignedTypes
actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: Int) : Blake2b { 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) { override fun update(data: UByteArray) {
TODO("not implemented yet") 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 { 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 { override fun digest(inputMessage: UByteArray, key: UByteArray, hashLength: Int): UByteArray {
TODO("not implemented yet") TODO("not implemented yet")
} }
override val MAX_HASH_BYTES: Int override val MAX_HASH_BYTES: Int = 64
get() = TODO("not implemented yet")
} }

View File

@ -20,4 +20,9 @@ class Blake2bLinuxTest {
println("Sodium init $sodiumInitResult") println("Sodium init $sodiumInitResult")
println("1") println("1")
} }
@Test
fun testBlake2BSodiumInterop() {
Blake2bStateless.digest("test")
}
} }

View File

@ -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.Argon2Pure
import com.ionspin.kotlin.crypto.keyderivation.argon2.ArgonType import com.ionspin.kotlin.crypto.keyderivation.argon2.ArgonType
import com.ionspin.kotlin.crypto.util.testBlocking import com.ionspin.kotlin.crypto.util.testBlocking
import com.ionspin.kotlin.crypto.util.toHexString
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
import kotlin.test.assertTrue import kotlin.test.assertTrue
@ -155,5 +156,11 @@ class ReadmeTest {
} }
@Test
fun debugTest() {
val result = Blake2bStateless.digest("test")
println(result.toHexString())
}
} }