Blake2b cleanup
This commit is contained in:
		
							parent
							
								
									63f90aa915
								
							
						
					
					
						commit
						4357124b48
					
				@ -2,6 +2,7 @@ package com.ionspin.kotlin.crypto.hash.blake2b
 | 
				
			|||||||
import com.ionspin.kotlin.crypto.util.toHexString
 | 
					import com.ionspin.kotlin.crypto.util.toHexString
 | 
				
			||||||
import kotlinx.cinterop.*
 | 
					import kotlinx.cinterop.*
 | 
				
			||||||
import libsodium.*
 | 
					import libsodium.*
 | 
				
			||||||
 | 
					import platform.posix.malloc
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Created by Ugljesa Jovanovic
 | 
					 * Created by Ugljesa Jovanovic
 | 
				
			||||||
 * ugljesa.jovanovic@ionspin.com
 | 
					 * ugljesa.jovanovic@ionspin.com
 | 
				
			||||||
@ -19,7 +20,9 @@ actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: I
 | 
				
			|||||||
        requestedHashLength = hashLength
 | 
					        requestedHashLength = hashLength
 | 
				
			||||||
        println("Size ${crypto_generichash_state.size}")
 | 
					        println("Size ${crypto_generichash_state.size}")
 | 
				
			||||||
        println("Align ${crypto_generichash_state.align}")
 | 
					        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")
 | 
					        println("allocated state")
 | 
				
			||||||
        crypto_generichash_init(state.ptr, key?.run { this.toUByteArray().toCValues() }, key?.size?.convert() ?: 0UL, hashLength.convert())
 | 
					        crypto_generichash_init(state.ptr, key?.run { this.toUByteArray().toCValues() }, key?.size?.convert() ?: 0UL, hashLength.convert())
 | 
				
			||||||
        println("Initialized libsodium hash")
 | 
					        println("Initialized libsodium hash")
 | 
				
			||||||
@ -41,33 +44,6 @@ actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: I
 | 
				
			|||||||
        println("HashPointer: ${hashResult.toHexString()}")
 | 
					        println("HashPointer: ${hashResult.toHexString()}")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return hashResult
 | 
					        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 {
 | 
					    override fun digestString(): String {
 | 
				
			||||||
@ -78,7 +54,6 @@ actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: I
 | 
				
			|||||||
@Suppress("EXPERIMENTAL_API_USAGE", "EXPERIMENTAL_UNSIGNED_LITERALS")
 | 
					@Suppress("EXPERIMENTAL_API_USAGE", "EXPERIMENTAL_UNSIGNED_LITERALS")
 | 
				
			||||||
actual object 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 {
 | 
				
			||||||
//        return allocEverything(inputString, key, hashLength)
 | 
					 | 
				
			||||||
        println("Input $inputString, ${key ?: "null"}, $hashLength")
 | 
					        println("Input $inputString, ${key ?: "null"}, $hashLength")
 | 
				
			||||||
        val hashResult = UByteArray(MAX_HASH_BYTES)
 | 
					        val hashResult = UByteArray(MAX_HASH_BYTES)
 | 
				
			||||||
        val hashResultPinned = hashResult.pin()
 | 
					        val hashResultPinned = hashResult.pin()
 | 
				
			||||||
@ -90,8 +65,6 @@ actual object Blake2bStateless : Blake2bStatelessInterface {
 | 
				
			|||||||
            key?.run { this.encodeToByteArray().toUByteArray().toCValues() },
 | 
					            key?.run { this.encodeToByteArray().toUByteArray().toCValues() },
 | 
				
			||||||
            key?.length?.convert() ?: 0UL
 | 
					            key?.length?.convert() ?: 0UL
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
        println("HashPointer: ${hashResult.toHexString()}")
 | 
					 | 
				
			||||||
        println(hashResult.toHexString())
 | 
					 | 
				
			||||||
        return hashResult
 | 
					        return hashResult
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user