Completed blake2b, sha256 and sha512 delegated implementations

This commit is contained in:
Ugljesa Jovanovic authored and Ugljesa Jovanovic committed 2020-06-09 22:13:21 +02:00
1 parent 6170dc0464
commit ce7b27da98
22 files changed
+244 -181

No files matched your search

@@ -21,12 +21,22 @@ fun setSodiumLoaded(loaded: Boolean) {
}
actual object Initializer {
private var isPlatformInitialized = false
actual suspend fun initialize() {
JsSodiumLoader.load()
isPlatformInitialized = true
}
actual fun initializeWithCallback(done: () -> Unit) {
JsSodiumLoader.loadWithCallback {
isPlatformInitialized = true
done()
}
}
actual fun isInitialized(): Boolean {
return isPlatformInitialized
}
@@ -10,14 +10,23 @@ import org.khronos.webgl.Uint8Array
*/
interface JsSodiumInterface {
fun crypto_generichash(hashLength: Int, inputMessage: Uint8Array): Uint8Array
fun randombytes_buf(numberOfBytes: Int): Uint8Array
fun crypto_generichash(hashLength: Int, inputMessage: Uint8Array, key: Uint8Array,): Uint8Array
fun crypto_hash_sha256(message: Uint8Array): Uint8Array
fun crypto_hash_sha512(message: Uint8Array): Uint8Array
//Updateable
fun crypto_generichash_init(key : Uint8Array, hashLength: Int) : dynamic
fun crypto_generichash_update(state: dynamic, inputMessage: Uint8Array)
fun crypto_generichash_final(state: dynamic, hashLength: Int) : Uint8Array
fun crypto_hash_sha256_init() : dynamic
fun crypto_hash_sha256_update(state: dynamic, message: Uint8Array)
@@ -1,6 +1,7 @@
package com.ionspin.kotlin.crypto.hash.blake2b
import com.ionspin.kotlin.crypto.getSodium
import com.ionspin.kotlin.crypto.hash.sha.Sha256StatelessDelegated
import com.ionspin.kotlin.crypto.util.toHexString
import org.khronos.webgl.Uint8Array
import org.khronos.webgl.get
@@ -12,18 +13,30 @@ import org.khronos.webgl.get
*/
actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: Int) : Blake2b {
actual class Blake2bDelegated actual constructor(key: UByteArray?, val hashLength: Int) : Blake2b {
override val MAX_HASH_BYTES: Int = 64
override fun update(data: UByteArray) {
TODO("not implemented yet")
val state : dynamic
init {
state = getSodium().crypto_generichash_init(
Uint8Array(key?.toByteArray()?.toTypedArray() ?: arrayOf()),
hashLength
)
}
override fun update(data: UByteArray) {
getSodium().crypto_generichash_update(state, Uint8Array(data.toByteArray().toTypedArray()))
}
override fun digest(): UByteArray {
TODO("not implemented yet")
val hashed = getSodium().crypto_generichash_final(state, hashLength)
val hash = UByteArray(hashLength)
for (i in 0 until hashLength) {
hash[i] = hashed[i].toUByte()
}
return hash
}
}
@@ -34,9 +47,12 @@ actual object Blake2bDelegatedStateless : Blake2bStateless {
override val MAX_HASH_BYTES: Int = 64
override fun digest(inputMessage: UByteArray, key: UByteArray, hashLength: Int): UByteArray {
val hashed = getSodium().crypto_generichash(64, Uint8Array(inputMessage.toByteArray().toTypedArray()))
val hash = UByteArray(MAX_HASH_BYTES)
for (i in 0 until MAX_HASH_BYTES) {
val hashed = getSodium().crypto_generichash(hashLength,
Uint8Array(inputMessage.toByteArray().toTypedArray()),
Uint8Array(key.toByteArray().toTypedArray())
)
val hash = UByteArray(hashLength)
for (i in 0 until hashLength) {
hash[i] = hashed[i].toUByte()
}
return hash
@@ -1,9 +1,6 @@
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
@@ -15,7 +12,7 @@ import org.khronos.webgl.get
*/
actual class Sha256Delegated actual constructor() : Sha256 {
actual class Sha256Delegated : Sha256 {
val state : dynamic
@@ -13,7 +13,7 @@ import org.khronos.webgl.get
*/
actual class Sha512Delegated actual constructor(key: UByteArray?, hashLength: Int) : Sha512 {
actual class Sha512Delegated : Sha512 {
val state : dynamic
init {