Api cleanup

This commit is contained in:
Ugljesa Jovanovic authored and Ugljesa Jovanovic committed 2020-06-07 00:37:09 +02:00
1 parent 25adc330bd
commit f03e94cf5e
15 files changed
+91 -83

No files matched your search

@@ -9,7 +9,7 @@ import org.khronos.webgl.Uint8Array
*/
interface JsSodiumInterface {
fun crypto_generichash(hashLength: Int, inputMessage: String) : Uint8Array
fun crypto_generichash(hashLength: Int, inputMessage: Uint8Array) : Uint8Array
fun randombytes_buf(numberOfBytes: Int) : Uint8Array
@@ -2,6 +2,7 @@ package com.ionspin.kotlin.crypto.hash.blake2b
import com.ionspin.kotlin.crypto.getSodium
import com.ionspin.kotlin.crypto.util.toHexString
import org.khronos.webgl.Uint8Array
/**
* Created by Ugljesa Jovanovic
@@ -33,36 +34,17 @@ actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: I
actual object Blake2bDelegatedStateless : Blake2bStateless {
override val MAX_HASH_BYTES: Int = 64
override fun digest(inputString: String, key: String?, hashLength: Int): UByteArray {
val hashed = getSodium().crypto_generichash(64, inputString)
val hash = UByteArray(MAX_HASH_BYTES)
for (i in 0 until MAX_HASH_BYTES) {
js(
"""
hash[i] = hashed[i]
"""
)
}
println("Hash ${hash.toHexString()}")
return hash
}
fun digestBlocking(inputString: String, key: String?, hashLength: Int): UByteArray {
val hashed = getSodium().crypto_generichash(hashLength, inputString)
val hash = UByteArray(MAX_HASH_BYTES)
for (i in 0 until MAX_HASH_BYTES) {
js(
"""
hash[i] = hashed[i]
"""
)
}
println("Hash ${hash.toHexString()}")
return hash
}
override fun digest(inputMessage: UByteArray, key: UByteArray, hashLength: Int): UByteArray {
TODO("not implemented yet")
val hashed = getSodium().crypto_generichash(64, 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
}
@@ -23,9 +23,6 @@ actual class Sha256Delegated actual constructor(key: UByteArray?, hashLength: In
}
actual object Sha256StatelessDelegated : StatelessSha256 {
override fun digest(inputString: String, key: String?, hashLength: Int): UByteArray {
TODO("not implemented yet")
}
override fun digest(inputMessage: UByteArray, key: UByteArray, hashLength: Int): UByteArray {
TODO("not implemented yet")
@@ -23,9 +23,6 @@ actual class Sha512Delegated actual constructor(key: UByteArray?, hashLength: In
}
actual object Sha512StatelessDelegated : StatelessSha512 {
override fun digest(inputString: String, key: String?, hashLength: Int): UByteArray {
TODO("not implemented yet")
}
override fun digest(inputMessage: UByteArray, key: UByteArray, hashLength: Int): UByteArray {
TODO("not implemented yet")
@@ -1,6 +1,7 @@
package com.ionspin.kotlin.crypto.hash.blake2b
import com.ionspin.kotlin.crypto.Crypto
import com.ionspin.kotlin.crypto.hash.encodeToUByteArray
import com.ionspin.kotlin.crypto.util.testBlocking
import com.ionspin.kotlin.crypto.util.toHexString
import kotlin.test.Test
@@ -18,18 +19,9 @@ class Blake2bJsTest {
@Test
fun testBlake2BSodiumInterop() = testBlocking {
Crypto.initialize()
val hash = Blake2bDelegatedStateless.digest("test")
val hash = Blake2bDelegatedStateless.digest("test".encodeToUByteArray())
assertEquals(hash.toHexString(), "a71079d42853dea26e453004338670a53814b78137ffbed07603a41d76a4" +
"83aa9bc33b582f77d30a65e6f29a896c0411f38312e1d66e0bf16386c86a89bea572")
}
@Test
fun testBlake2BSodiumBlockingInterop() = testBlocking {
Crypto.initialize()
val hash = Blake2bDelegatedStateless.digestBlocking("test", null, 64)
assertEquals(hash.toHexString(), "a71079d42853dea26e453004338670a53814b78137ffbed07603a41d76a4" +
"83aa9bc33b582f77d30a65e6f29a896c0411f38312e1d66e0bf16386c86a89bea572")
}
}
@@ -50,20 +50,20 @@ actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: I
@Suppress("EXPERIMENTAL_API_USAGE", "EXPERIMENTAL_UNSIGNED_LITERALS")
actual object Blake2bDelegatedStateless : Blake2bStateless {
override fun digest(inputString: String, key: String?, hashLength: Int): UByteArray {
println("Input $inputString, ${key ?: "null"}, $hashLength")
val hashResult = UByteArray(MAX_HASH_BYTES)
val hashResultPinned = hashResult.pin()
crypto_generichash(
hashResultPinned.addressOf(0),
hashLength.convert(),
inputString.encodeToByteArray().toUByteArray().toCValues(),
inputString.length.convert(),
key?.run { this.encodeToByteArray().toUByteArray().toCValues() },
key?.length?.convert() ?: 0UL
)
return hashResult
}
// override fun digest(inputString: String, key: String?, hashLength: Int): UByteArray {
// println("Input $inputString, ${key ?: "null"}, $hashLength")
// val hashResult = UByteArray(MAX_HASH_BYTES)
// val hashResultPinned = hashResult.pin()
// crypto_generichash(
// hashResultPinned.addressOf(0),
// hashLength.convert(),
// inputString.encodeToByteArray().toUByteArray().toCValues(),
// inputString.length.convert(),
// key?.run { this.encodeToByteArray().toUByteArray().toCValues() },
// key?.length?.convert() ?: 0UL
// )
// return hashResult
// }
@@ -79,8 +79,6 @@ actual object Blake2bDelegatedStateless : Blake2bStateless {
key.toCValues(),
key.size.convert() ?: 0UL
)
println("HashPointer: ${hashResult.toHexString()}")
println(hashResult.toHexString())
return hashResult
}
@@ -26,9 +26,6 @@ actual class Sha256Delegated actual constructor(key: UByteArray?, hashLength: In
}
actual object Sha256StatelessDelegated : StatelessSha256 {
override fun digest(inputString: String, key: String?, hashLength: Int): UByteArray {
TODO("not implemented yet")
}
override fun digest(inputMessage: UByteArray, key: UByteArray, hashLength: Int): UByteArray {
TODO("not implemented yet")
@@ -23,9 +23,6 @@ actual class Sha512Delegated actual constructor(key: UByteArray?, hashLength: In
}
actual object Sha512StatelessDelegated : StatelessSha512 {
override fun digest(inputString: String, key: String?, hashLength: Int): UByteArray {
TODO("not implemented yet")
}
override fun digest(inputMessage: UByteArray, key: UByteArray, hashLength: Int): UByteArray {
TODO("not implemented yet")
@@ -7,6 +7,7 @@ package com.ionspin.kotlin.crypto.hash.blake2b
*/
import com.ionspin.kotlin.crypto.Crypto
import com.ionspin.kotlin.crypto.hash.encodeToUByteArray
import com.ionspin.kotlin.crypto.util.testBlocking
import com.ionspin.kotlin.crypto.util.toHexString
import kotlinx.coroutines.runBlocking
@@ -34,6 +35,6 @@ class Blake2bLinuxTest {
@Test
fun testBlake2BStateless() = testBlocking {
Blake2bDelegatedStateless.digest("test")
Blake2bDelegatedStateless.digest("test".encodeToUByteArray())
}
}
@@ -0,0 +1,40 @@
package com.ionspin.kotlin.crypto.hash.blake2b
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 24-May-2020
*/
import com.ionspin.kotlin.crypto.Crypto
import com.ionspin.kotlin.crypto.hash.encodeToUByteArray
import com.ionspin.kotlin.crypto.util.testBlocking
import com.ionspin.kotlin.crypto.util.toHexString
import kotlinx.coroutines.runBlocking
import kotlin.test.Test
import kotlin.test.assertTrue
class Sha512DelegatedLinuxTest {
@Test
fun testCinterop() {
runBlocking {
Crypto.initialize()
}
}
@Test
fun testBlake2bUpdateable() = testBlocking {
val blake2b = Crypto.Blake2b.updateable()
blake2b.update("test")
val result = blake2b.digest().toHexString()
println(result)
assertTrue { result.length > 2 }
}
@Test
fun testBlake2BStateless() = testBlocking {
Blake2bDelegatedStateless.digest("test".encodeToUByteArray())
}
}