Blake2b cleanup

This commit is contained in:
Ugljesa Jovanovic 2020-06-06 22:20:27 +02:00 committed by Ugljesa Jovanovic
parent 63f90aa915
commit 4357124b48
No known key found for this signature in database
GPG Key ID: 178E6DFCECCB0E0F

View File

@ -2,6 +2,7 @@ package com.ionspin.kotlin.crypto.hash.blake2b
import com.ionspin.kotlin.crypto.util.toHexString
import kotlinx.cinterop.*
import libsodium.*
import platform.posix.malloc
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
@ -19,7 +20,9 @@ actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: I
requestedHashLength = hashLength
println("Size ${crypto_generichash_state.size}")
println("Align ${crypto_generichash_state.align}")
state = nativeHeap.alloc(crypto_generichash_state.size, 1).reinterpret()
println("Using sodium malloc for state")
val allocated = sodium_malloc(crypto_generichash_state.size.convert())!!
state = allocated.reinterpret<crypto_generichash_state>().pointed
println("allocated state")
crypto_generichash_init(state.ptr, key?.run { this.toUByteArray().toCValues() }, key?.size?.convert() ?: 0UL, hashLength.convert())
println("Initialized libsodium hash")
@ -41,33 +44,6 @@ actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: I
println("HashPointer: ${hashResult.toHexString()}")
return hashResult
// val inputString = "test"
// val hashLength = 64
// val key : String? = null
// val result2 = allocEverything(inputString, key, hashLength)
// val result2String = result2.toHexString()
// println(result2String)
// return ubyteArrayOf(0U)
}
fun allocEverything(inputString: String, key: String?, hashLength: Int) : UByteArray {
val res = memScoped {
val result = allocArray<UByteVar>(hashLength)
println("Alloced: $result")
crypto_generichash(
result,
hashLength.convert(),
inputString.encodeToByteArray().toUByteArray().toCValues(),
inputString.length.convert(),
key?.run { this.encodeToByteArray().toUByteArray().toCValues() },
key?.length?.convert() ?: 0UL
)
println("Result: $result")
UByteArray(hashLength) {
result[it]
}
}
return res
}
override fun digestString(): String {
@ -78,7 +54,6 @@ actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: I
@Suppress("EXPERIMENTAL_API_USAGE", "EXPERIMENTAL_UNSIGNED_LITERALS")
actual object Blake2bStateless : Blake2bStatelessInterface {
override fun digest(inputString: String, key: String?, hashLength: Int): UByteArray {
// return allocEverything(inputString, key, hashLength)
println("Input $inputString, ${key ?: "null"}, $hashLength")
val hashResult = UByteArray(MAX_HASH_BYTES)
val hashResultPinned = hashResult.pin()
@ -90,8 +65,6 @@ actual object Blake2bStateless : Blake2bStatelessInterface {
key?.run { this.encodeToByteArray().toUByteArray().toCValues() },
key?.length?.convert() ?: 0UL
)
println("HashPointer: ${hashResult.toHexString()}")
println(hashResult.toHexString())
return hashResult
}