js attempts to integrate libsodium

This commit is contained in:
Ugljesa Jovanovic 2020-05-26 00:23:27 +02:00 committed by Ugljesa Jovanovic
parent 1a89ee5154
commit 1009b92d33
No known key found for this signature in database
GPG Key ID: 178E6DFCECCB0E0F
6 changed files with 79 additions and 20 deletions

View File

@ -49,6 +49,11 @@ object Deps {
val coroutines = "org.jetbrains.kotlinx:kotlinx-coroutines-core-js:${Versions.kotlinCoroutines}" val coroutines = "org.jetbrains.kotlinx:kotlinx-coroutines-core-js:${Versions.kotlinCoroutines}"
val serialization = "org.jetbrains.kotlinx:kotlinx-serialization-runtime-js:${Versions.kotlinSerialization}" val serialization = "org.jetbrains.kotlinx:kotlinx-serialization-runtime-js:${Versions.kotlinSerialization}"
object Npm {
val libsodium = Pair("libsodium", "0.7.6")
val libsodiumWrappers = Pair("libsodium-wrappers", "0.7.6")
}
} }
object Jvm { object Jvm {

View File

@ -249,12 +249,15 @@ kotlin {
dependencies { dependencies {
implementation(kotlin(Deps.Js.stdLib)) implementation(kotlin(Deps.Js.stdLib))
implementation(Deps.Js.coroutines) implementation(Deps.Js.coroutines)
implementation(npm(Deps.Js.Npm.libsodium.first, Deps.Js.Npm.libsodium.second))
implementation(npm(Deps.Js.Npm.libsodiumWrappers.first, Deps.Js.Npm.libsodiumWrappers.second))
} }
} }
val jsTest by getting { val jsTest by getting {
dependencies { dependencies {
implementation(Deps.Js.coroutines) implementation(Deps.Js.coroutines)
implementation(kotlin(Deps.Js.test)) implementation(kotlin(Deps.Js.test))
implementation(npm(Deps.Js.Npm.libsodium.first, Deps.Js.Npm.libsodium.second))
} }
} }
val linuxMain by getting { val linuxMain by getting {

View File

@ -1,5 +1,10 @@
package com.ionspin.kotlin.crypto.hash.blake2b package com.ionspin.kotlin.crypto.hash.blake2b
import crypto_generichash
import org.khronos.webgl.Uint8Array
import org.khronos.webgl.get
import kotlin.js.Promise
/** /**
* Created by Ugljesa Jovanovic * Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com * ugljesa.jovanovic@ionspin.com
@ -8,7 +13,7 @@ package com.ionspin.kotlin.crypto.hash.blake2b
@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 = 128 override val MAX_HASH_BYTES: Int = 64
override fun update(data: UByteArray) { override fun update(data: UByteArray) {
@ -28,17 +33,24 @@ actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: I
} }
} }
@ExperimentalStdlibApi
@ExperimentalUnsignedTypes @ExperimentalUnsignedTypes
actual object Blake2bStateless : Blake2bStatelessInterface { actual object Blake2bStateless : Blake2bStatelessInterface {
override val MAX_HASH_BYTES: Int = 64
override fun digest(inputString: String, key: String?, hashLength: Int): UByteArray { override fun digest(inputString: String, key: String?, hashLength: Int): UByteArray {
TODO("not implemented yet") // val hashed = crypto_generichash(64, Uint8Array(inputString.encodeToByteArray().toTypedArray()), null)
// return UByteArray(MAX_HASH_BYTES) { hashed[it].toUByte() }
val hash = crypto_generichash(64, Uint8Array(arrayOf(0U.toByte())));
println("Hash $hash")
return ubyteArrayOf(0U)
} }
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
get() = TODO("not implemented yet")
} }

View File

@ -0,0 +1,10 @@
import org.khronos.webgl.Uint8Array
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 25-May-2020
*/
@JsModule("libsodium")
@JsNonModule
external fun crypto_generichash(hashLength: Int, inputMessage: Uint8Array) : Uint8Array

View File

@ -0,0 +1,18 @@
package com.ionspin.kotlin.crypto.hash.blake2b
import kotlin.test.Test
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 25-May-2020
*/
@ExperimentalUnsignedTypes
@ExperimentalStdlibApi
class Blake2bJsTest {
@Test
fun testBlake2BSodiumInterop() {
Blake2bStateless.digest("test")
}
}

View File

@ -35,25 +35,36 @@ 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 memScoped { val hashResult = UByteArray(MAX_HASH_BYTES)
val hashResult = UByteArray(MAX_HASH_BYTES) val hashResultPinned = hashResult.pin()
val hashPointer = hashResult.toCValues().getPointer(this) crypto_generichash(
crypto_generichash( hashResultPinned.addressOf(0),
hashPointer, hashLength.toULong(),
hashLength.toULong(), inputString.encodeToByteArray().toUByteArray().toCValues(),
inputString.cstr.getPointer(this).reinterpret(), inputString.length.toULong(),
inputString.length.toULong(), key?.cstr?.getPointer(this)?.reinterpret(), key?.run { this.encodeToByteArray().toUByteArray().toCValues() },
key?.length?.toULong() ?: 0UL key?.length?.toULong() ?: 0UL
) )
println("HashPointer: ${hashPointer.readBytes(MAX_HASH_BYTES).toUByteArray().toHexString()}") println("HashPointer: ${hashResult.toHexString()}")
println(hashResult.toHexString()) println(hashResult.toHexString())
hashResult return 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") val hashResult = UByteArray(MAX_HASH_BYTES)
crypto_generichash(
StableRef.create(hashResult).asCPointer().reinterpret(),
hashLength.toULong(),
inputMessage.toCValues(),
inputMessage.size.toULong(),
key.toCValues(),
key.size.toULong() ?: 0UL
)
println("HashPointer: ${hashResult.toHexString()}")
println(hashResult.toHexString())
return hashResult
} }
override val MAX_HASH_BYTES: Int = 64 override val MAX_HASH_BYTES: Int = 64