Add stateless implementation

This commit is contained in:
Ugljesa Jovanovic 2020-06-07 19:04:23 +02:00 committed by Ugljesa Jovanovic
parent 3a81d8e57c
commit 710ac43e8c
No known key found for this signature in database
GPG Key ID: 178E6DFCECCB0E0F
9 changed files with 66 additions and 11 deletions

View File

@ -43,6 +43,12 @@ fun KotlinMultiplatformExtension.isNotRunningInIdea(block: KotlinMultiplatformEx
} }
} }
fun KotlinMultiplatformExtension.isRunningInTravis(block: KotlinMultiplatformExtension.() -> Unit) {
if (isInTravis()) {
block(this)
}
}
fun KotlinMultiplatformExtension.runningOnLinuxx86_64(block: KotlinMultiplatformExtension.() -> Unit) { fun KotlinMultiplatformExtension.runningOnLinuxx86_64(block: KotlinMultiplatformExtension.() -> Unit) {
if (getHostOsName() == "linux" && getHostArchitecture() == "x86-64") { if (getHostOsName() == "linux" && getHostArchitecture() == "x86-64") {
block(this) block(this)

View File

@ -61,7 +61,9 @@ kotlin {
js { js {
browser { browser {
testTask { testTask {
isRunningInTravis {
enabled = false //Until I sort out testing on travis enabled = false //Until I sort out testing on travis
}
useKarma { useKarma {
useChrome() useChrome()
} }

View File

@ -36,8 +36,8 @@ object Crypto : CryptoProvider {
return Sha256Delegated() return Sha256Delegated()
} }
fun stateless(message: UByteArray, key: UByteArray? = null, hashLength: Int = Sha256Properties.MAX_HASH_BYTES) { fun stateless(message: UByteArray) : UByteArray{
return Sha256StatelessDelegated.digest(inputMessage = message)
} }
} }
@ -46,8 +46,8 @@ object Crypto : CryptoProvider {
return Sha512Delegated() return Sha512Delegated()
} }
fun stateless(message: UByteArray, key: UByteArray? = null, hashLength: Int = Sha512Properties.MAX_HASH_BYTES) { fun stateless(message: UByteArray) : UByteArray {
TODO()
} }
} }

View File

@ -0,0 +1,22 @@
package com.ionspin.kotlin.crypto.hash.sha
import com.ionspin.kotlin.crypto.Crypto
import com.ionspin.kotlin.crypto.hash.encodeToUByteArray
import com.ionspin.kotlin.crypto.util.toHexString
import kotlin.test.Test
import kotlin.test.assertTrue
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 07-Jun-2020
*/
class Sha256Test {
@Test
fun statelessSimpleTest() {
val expected = "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"
val result = Crypto.Sha256.stateless("test".encodeToUByteArray()).toHexString()
println("Result: $result")
assertTrue { result == expected }
}
}

View File

@ -9,6 +9,8 @@ import org.khronos.webgl.Uint8Array
*/ */
interface JsSodiumInterface { 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

View File

@ -1,5 +1,9 @@
package com.ionspin.kotlin.crypto.hash.sha package com.ionspin.kotlin.crypto.hash.sha
import com.ionspin.kotlin.crypto.getSodium
import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bDelegatedStateless
import org.khronos.webgl.Uint8Array
/** /**
* Created by Ugljesa Jovanovic * Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com * ugljesa.jovanovic@ionspin.com
@ -25,6 +29,15 @@ actual class Sha256Delegated actual constructor(key: UByteArray?, hashLength: In
actual object Sha256StatelessDelegated : StatelessSha256 { actual object Sha256StatelessDelegated : StatelessSha256 {
override fun digest(inputMessage: UByteArray): UByteArray { override fun digest(inputMessage: UByteArray): UByteArray {
TODO("not implemented yet") 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]
"""
)
}
return hash
} }
} }

View File

@ -15,5 +15,7 @@ import kotlin.js.Promise
@JsName("ready") @JsName("ready")
external val _libsodiumPromise : Promise<dynamic> external val _libsodiumPromise : Promise<dynamic>
external fun crypto_generichash(hashLength: Int, inputMessage: String) : String external fun crypto_generichash(hashLength: Int, inputMessage: Uint8Array) : Uint8Array
external fun crypto_hash_sha256(message: Uint8Array) : Uint8Array

View File

@ -1,5 +1,6 @@
package com.ionspin.kotlin.crypto.hash.sha package com.ionspin.kotlin.crypto.hash.sha
import java.security.MessageDigest
/** /**
@ -27,6 +28,8 @@ actual class Sha256Delegated actual constructor(key: UByteArray?, hashLength: In
actual object Sha256StatelessDelegated : StatelessSha256 { actual object Sha256StatelessDelegated : StatelessSha256 {
override fun digest(inputMessage: UByteArray): UByteArray { override fun digest(inputMessage: UByteArray): UByteArray {
TODO("not implemented yet") val messageDigest = MessageDigest.getInstance("SHA-256")
messageDigest.update(inputMessage.toByteArray())
return messageDigest.digest().toUByteArray()
} }
} }

View File

@ -1,7 +1,11 @@
package com.ionspin.kotlin.crypto.hash.sha package com.ionspin.kotlin.crypto.hash.sha
import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bDelegatedStateless import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bDelegatedStateless
import kotlinx.cinterop.addressOf
import kotlinx.cinterop.convert
import kotlinx.cinterop.pin import kotlinx.cinterop.pin
import kotlinx.cinterop.toCValues
import libsodium.crypto_hash_sha256
/** /**
* Created by Ugljesa Jovanovic * Created by Ugljesa Jovanovic
@ -31,10 +35,11 @@ actual class Sha256Delegated actual constructor(key: UByteArray?, hashLength: In
actual object Sha256StatelessDelegated : StatelessSha256 { actual object Sha256StatelessDelegated : StatelessSha256 {
override fun digest(inputMessage: UByteArray): UByteArray { override fun digest(inputMessage: UByteArray): UByteArray {
val hashResult = UByteArray(Blake2bDelegatedStateless.MAX_HASH_BYTES) val hashResult = UByteArray(MAX_HASH_BYTES)
val hashResultPinned = hashResult.pin() val hashResultPinned = hashResult.pin()
// crypto_ crypto_hash_sha256(hashResultPinned.addressOf(0), inputMessage.toCValues(), inputMessage.size.convert())
TODO("not implemented yet") hashResultPinned.unpin()
return hashResult
} }
} }