Built and included modified libsodium with additional wrappers, working blake, sha256/512 hashes

This commit is contained in:
Ugljesa Jovanovic authored and Ugljesa Jovanovic committed 2020-06-09 20:30:12 +02:00
1 parent 710ac43e8c
commit 197aacac33
30 files changed
+170 -89

No files matched your search

@@ -8,6 +8,8 @@ var sodiumLoaded: Boolean = false
fun getSodium() : JsSodiumInterface = sodiumPointer
//fun getSodiumAdvanced() : JsSodiumAdvancedInterface = js("sodiumPointer.libsodium")
fun setSodiumPointer(jsSodiumInterface: JsSodiumInterface) {
js("sodiumPointer = jsSodiumInterface")
}
@@ -1,5 +1,6 @@
package ext.libsodium.com.ionspin.kotlin.crypto
import org.khronos.webgl.Uint8Array
/**
@@ -9,11 +10,25 @@ import org.khronos.webgl.Uint8Array
*/
interface JsSodiumInterface {
fun crypto_hash_sha256(message: Uint8Array) : Uint8Array
fun crypto_generichash(hashLength: Int, inputMessage: Uint8Array): Uint8Array
fun crypto_generichash(hashLength: Int, inputMessage: Uint8Array) : Uint8Array
fun randombytes_buf(numberOfBytes: Int): Uint8Array
fun randombytes_buf(numberOfBytes: Int) : Uint8Array
fun crypto_hash_sha256(message: Uint8Array): Uint8Array
fun crypto_hash_sha512(message: Uint8Array): Uint8Array
fun crypto_hash_sha256_init() : dynamic
fun crypto_hash_sha256_update(state: dynamic, message: Uint8Array)
fun crypto_hash_sha256_final(state: dynamic): Uint8Array
fun crypto_hash_sha512_init() : dynamic
fun crypto_hash_sha512_update(state: dynamic, message: Uint8Array)
fun crypto_hash_sha512_final(state: dynamic): Uint8Array
}
}
@@ -3,7 +3,7 @@ package ext.libsodium.com.ionspin.kotlin.crypto
import com.ionspin.kotlin.crypto.getSodiumLoaded
import com.ionspin.kotlin.crypto.setSodiumPointer
import com.ionspin.kotlin.crypto.sodiumLoaded
import ext.libsodium._libsodiumPromise
import ext.libsodium.*
import kotlin.coroutines.Continuation
import kotlin.coroutines.suspendCoroutine
@@ -14,6 +14,16 @@ import kotlin.coroutines.suspendCoroutine
*/
object JsSodiumLoader {
class _EmitJsSodiumFunction {
init {
println(::crypto_generichash)
println(::crypto_hash_sha256)
println(::crypto_hash_sha512)
println(::crypto_hash_sha256_init)
}
}
fun storeSodium(promisedSodium: dynamic, continuation: Continuation<Unit>) {
setSodiumPointer(promisedSodium)
sodiumLoaded = true
@@ -28,8 +28,6 @@ actual object SRNG {
actual fun getRandomBytes(amount: Int): UByteArray {
val randomBytes = getSodium().randombytes_buf(amount)
println("Random bytes: $randomBytes")
print("Byte at ${randomBytes[0]}")
val randomBytesUByteArray = UByteArray(amount) {
0U
}
@@ -3,6 +3,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
import org.khronos.webgl.get
/**
* Created by Ugljesa Jovanovic
@@ -19,9 +20,7 @@ actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: I
TODO("not implemented yet")
}
override fun update(data: String) {
TODO("not implemented yet")
}
override fun digest(): UByteArray {
TODO("not implemented yet")
@@ -38,11 +37,7 @@ actual object Blake2bDelegatedStateless : Blake2bStateless {
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]
"""
)
hash[i] = hashed[i].toUByte()
}
return hash
}
@@ -2,7 +2,11 @@ package com.ionspin.kotlin.crypto.hash.sha
import com.ionspin.kotlin.crypto.getSodium
import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bDelegatedStateless
import ext.libsodium.crypto_hash_sha256_init
import org.khronos.webgl.Uint8Array
import org.khronos.webgl.get
/**
* Created by Ugljesa Jovanovic
@@ -12,16 +16,25 @@ import org.khronos.webgl.Uint8Array
actual class Sha256Delegated actual constructor(key: UByteArray?, hashLength: Int) : Sha256 {
override fun update(data: UByteArray) {
TODO("not implemented yet")
val state : dynamic
init {
state = getSodium().crypto_hash_sha256_init()
}
override fun update(data: String) {
TODO("not implemented yet")
override fun update(data: UByteArray) {
getSodium().crypto_hash_sha256_update(state, Uint8Array(data.toByteArray().toTypedArray()))
}
override fun digest(): UByteArray {
TODO("not implemented yet")
val hashed = getSodium().crypto_hash_sha256_final(state)
val hash = UByteArray(Sha256StatelessDelegated.MAX_HASH_BYTES)
console.log(hashed)
for (i in 0 until Sha256StatelessDelegated.MAX_HASH_BYTES) {
hash[i] = hashed[i].toUByte()
}
return hash
}
}
@@ -32,11 +45,7 @@ actual object Sha256StatelessDelegated : StatelessSha256 {
val hashed = getSodium().crypto_hash_sha256(Uint8Array(inputMessage.toByteArray().toTypedArray()))
val hash = UByteArray(MAX_HASH_BYTES)
for (i in 0 until MAX_HASH_BYTES) {
js(
"""
hash[i] = hashed[i]
"""
)
hash[i] = hashed[i].toUByte()
}
return hash
}
@@ -1,5 +1,11 @@
package com.ionspin.kotlin.crypto.hash.sha
import com.ionspin.kotlin.crypto.getSodium
import com.ionspin.kotlin.crypto.getSodium
import org.khronos.webgl.Uint8Array
import org.khronos.webgl.get
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
@@ -8,16 +14,23 @@ package com.ionspin.kotlin.crypto.hash.sha
actual class Sha512Delegated actual constructor(key: UByteArray?, hashLength: Int) : Sha512 {
override fun update(data: UByteArray) {
TODO("not implemented yet")
val state : dynamic
init {
state = getSodium().crypto_hash_sha512_init()
}
override fun update(data: String) {
TODO("not implemented yet")
override fun update(data: UByteArray) {
getSodium().crypto_hash_sha512_update(state, Uint8Array(data.toByteArray().toTypedArray()))
}
override fun digest(): UByteArray {
TODO("not implemented yet")
val hashed = getSodium().crypto_hash_sha512_final(state)
val hash = UByteArray(Sha512StatelessDelegated.MAX_HASH_BYTES)
for (i in 0 until Sha512StatelessDelegated.MAX_HASH_BYTES) {
hash[i] = hashed[i].toUByte()
}
return hash
}
}
@@ -25,6 +38,11 @@ actual class Sha512Delegated actual constructor(key: UByteArray?, hashLength: In
actual object Sha512StatelessDelegated : StatelessSha512 {
override fun digest(inputMessage: UByteArray): UByteArray {
TODO("not implemented yet")
val hashed = getSodium().crypto_hash_sha512(Uint8Array(inputMessage.toByteArray().toTypedArray()))
val hash = UByteArray(Sha512StatelessDelegated.MAX_HASH_BYTES)
for (i in 0 until Sha512StatelessDelegated.MAX_HASH_BYTES) {
hash[i] = hashed[i].toUByte()
}
return hash
}
}
@@ -18,4 +18,11 @@ external val _libsodiumPromise : Promise<dynamic>
external fun crypto_generichash(hashLength: Int, inputMessage: Uint8Array) : Uint8Array
external fun crypto_hash_sha256(message: Uint8Array) : Uint8Array
external fun crypto_hash_sha512(message: Uint8Array) : Uint8Array
external fun crypto_hash_sha256_init(): dynamic