This commit is contained in:
Ugljesa Jovanovic 2020-05-28 19:48:44 +02:00 committed by Ugljesa Jovanovic
parent f51374ce15
commit ccbb8ebe5b
No known key found for this signature in database
GPG Key ID: 178E6DFCECCB0E0F
15 changed files with 41 additions and 33 deletions

View File

@ -39,7 +39,7 @@ interface UpdatableHash : Hash {
@ExperimentalUnsignedTypes @ExperimentalUnsignedTypes
interface StatelessHash : Hash { interface StatelessHash : Hash {
suspend fun digest(inputString: String, key: String? = null, hashLength: Int = MAX_HASH_BYTES): UByteArray fun digest(inputString: String, key: String? = null, hashLength: Int = MAX_HASH_BYTES): UByteArray
fun digest( fun digest(
inputMessage: UByteArray = ubyteArrayOf(), inputMessage: UByteArray = ubyteArrayOf(),

View File

@ -9,5 +9,9 @@ import com.ionspin.kotlin.crypto.hash.UpdatableHash
* on 24-May-2020 * on 24-May-2020
*/ */
interface Blake2b : UpdatableHash interface Blake2b : UpdatableHash
interface Blake2bStatelessInterface : StatelessHash interface Blake2bStatelessInterface : StatelessHash {
@ExperimentalUnsignedTypes
override val MAX_HASH_BYTES: Int
get() = 64
}

View File

@ -61,12 +61,21 @@ fun getHostOsName(): String {
} }
kotlin { kotlin {
val libsodiumCompilation : org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTargetWithHostTests.() -> Unit = {
compilations.getByName("main") {
val libsodiumCinterop by cinterops.creating {
defFile(project.file("src/nativeInterop/cinterop/libsodium.def"))
}
}
}
val hostOsName = getHostOsName() val hostOsName = getHostOsName()
if (ideaActive) { if (ideaActive) {
when(hostOsName) { when(hostOsName) {
"linux" -> linuxX64("native") "linux" -> linuxX64("native", libsodiumCompilation)
"macos" -> macosX64("native") "macos" -> macosX64("native", libsodiumCompilation)
"windows" -> mingwX64("native") "windows" -> mingwX64("native", libsodiumCompilation)
} }
} }
if (hostOsName == "linux") { if (hostOsName == "linux") {
@ -90,17 +99,7 @@ kotlin {
} }
linuxX64("linux") { linuxX64("linux") {
compilations.getByName("main") { libsodiumCompilation(this)
val libsodiumCinterop by cinterops.creating {
defFile(project.file("src/nativeInterop/cinterop/libsodium.def"))
// packageName("sodium")
// includeDirs.apply {
// allHeaders("/usr/include/sodium")
// header("/usr/include/sodium.h")
// }
// linkerOpts("-lsodium")
}
}
binaries { binaries {
staticLib { staticLib {
} }

View File

@ -66,7 +66,7 @@ class Sha256Pure : Sha256 {
) )
@ExperimentalStdlibApi @ExperimentalStdlibApi
override suspend fun digest(inputString: String, key: String?, hashLength: Int): UByteArray { override fun digest(inputString: String, key: String?, hashLength: Int): UByteArray {
return digest( return digest(
inputString.encodeToByteArray().toUByteArray(), inputString.encodeToByteArray().toUByteArray(),
key?.run { encodeToByteArray().toUByteArray()} ?: ubyteArrayOf(), key?.run { encodeToByteArray().toUByteArray()} ?: ubyteArrayOf(),

View File

@ -134,7 +134,7 @@ class Sha512Pure : Sha512 {
) )
@ExperimentalStdlibApi @ExperimentalStdlibApi
override suspend fun digest(inputString: String, key: String?, hashLength: Int): UByteArray { override fun digest(inputString: String, key: String?, hashLength: Int): UByteArray {
return digest( return digest(
inputString.encodeToByteArray().toUByteArray(), inputString.encodeToByteArray().toUByteArray(),
key?.run { encodeToByteArray().toUByteArray() } ?: ubyteArrayOf(), key?.run { encodeToByteArray().toUByteArray() } ?: ubyteArrayOf(),

View File

@ -36,7 +36,7 @@ actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: I
actual object Blake2bStateless : Blake2bStatelessInterface { actual object Blake2bStateless : Blake2bStatelessInterface {
override val MAX_HASH_BYTES: Int = 64 override val MAX_HASH_BYTES: Int = 64
override suspend fun digest(inputString: String, key: String?, hashLength: Int): UByteArray { override fun digest(inputString: String, key: String?, hashLength: Int): UByteArray {
val hashed = getSodium().crypto_generichash(64, inputString) val hashed = getSodium().crypto_generichash(64, inputString)
val hash = UByteArray(MAX_HASH_BYTES) val hash = UByteArray(MAX_HASH_BYTES)
for (i in 0 until MAX_HASH_BYTES) { for (i in 0 until MAX_HASH_BYTES) {

View File

@ -3,4 +3,7 @@ package com.ionspin.kotlin.crypto
actual object Initializer { actual object Initializer {
actual suspend fun initialize() { actual suspend fun initialize() {
} }
actual fun initializeWithCallback(done: () -> Unit) {
}
} }

View File

@ -29,7 +29,10 @@ actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: I
} }
@ExperimentalUnsignedTypes @ExperimentalUnsignedTypes
actual object Blake2bStateless : Blake2bStatelessInterface { actual object Blake2bStateless : Blake2bStatelessInterface {
suspend override fun digest(inputString: String, key: String?, hashLength: Int): UByteArray {
override fun digest(inputString: String, key: String?, hashLength: Int): UByteArray {
TODO("not implemented yet") TODO("not implemented yet")
} }
@ -37,7 +40,4 @@ actual object Blake2bStateless : Blake2bStatelessInterface {
TODO("not implemented yet") TODO("not implemented yet")
} }
override val MAX_HASH_BYTES: Int
get() = TODO("not implemented yet")
} }

View File

@ -2,5 +2,9 @@ package com.ionspin.kotlin.crypto
actual object Initializer { actual object Initializer {
actual suspend fun initialize() { actual suspend fun initialize() {
// sodi
}
actual fun initializeWithCallback(done: () -> Unit) {
} }
} }

View File

@ -34,7 +34,7 @@ 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 {
suspend override fun digest(inputString: String, key: String?, hashLength: Int): UByteArray { override fun digest(inputString: String, key: String?, hashLength: Int): UByteArray {
val hashResult = UByteArray(MAX_HASH_BYTES) val hashResult = UByteArray(MAX_HASH_BYTES)
val hashResultPinned = hashResult.pin() val hashResultPinned = hashResult.pin()
crypto_generichash( crypto_generichash(
@ -67,6 +67,5 @@ actual object Blake2bStateless : Blake2bStatelessInterface {
} }
override val MAX_HASH_BYTES: Int = 64
} }

View File

@ -6,6 +6,7 @@ package com.ionspin.kotlin.crypto.hash.blake2b
* on 24-May-2020 * on 24-May-2020
*/ */
import com.ionspin.kotlin.crypto.util.testBlocking
import interop.* import interop.*
import kotlinx.cinterop.* import kotlinx.cinterop.*
import libsodium.* import libsodium.*
@ -22,7 +23,7 @@ class Blake2bLinuxTest {
} }
@Test @Test
fun testBlake2BSodiumInterop() { fun testBlake2BSodiumInterop() = testBlocking {
Blake2bStateless.digest("test") Blake2bStateless.digest("test")
} }
} }

View File

@ -18,6 +18,7 @@ package com.ionspin.kotlin.crypto
import kotlinx.cinterop.* import kotlinx.cinterop.*
import platform.posix.* import platform.posix.*
//import libsod
/** /**
* Created by Ugljesa Jovanovic * Created by Ugljesa Jovanovic
@ -29,10 +30,7 @@ actual object SRNG {
actual fun getRandomBytes(amount: Int): UByteArray { actual fun getRandomBytes(amount: Int): UByteArray {
memScoped { memScoped {
val array = allocArray<UByteVar>(amount) val array = allocArray<UByteVar>(amount)
val urandomFile = fopen("/dev/urandom", "rb") // randombytes_buf()
if (urandomFile != null) {
fread(array, 1, amount.convert(), urandomFile)
}
return UByteArray(amount) { return UByteArray(amount) {
array[it] array[it]
} }

View File

@ -145,7 +145,7 @@ class Blake2bPure(val key: UByteArray? = null, val hashLength: Int = 64) : Blake
} }
@ExperimentalStdlibApi @ExperimentalStdlibApi
override suspend fun digest(inputString: String, key: String?, hashLength: Int): UByteArray { override fun digest(inputString: String, key: String?, hashLength: Int): UByteArray {
val array = inputString.encodeToByteArray().toUByteArray() val array = inputString.encodeToByteArray().toUByteArray()
val keyBytes = key?.run { val keyBytes = key?.run {
encodeToByteArray().toUByteArray() encodeToByteArray().toUByteArray()

View File

@ -66,7 +66,7 @@ class Sha256Pure : Sha256 {
) )
@ExperimentalStdlibApi @ExperimentalStdlibApi
suspend override fun digest(inputString: String, key: String?, hashLength: Int): UByteArray { override fun digest(inputString: String, key: String?, hashLength: Int): UByteArray {
return digest( return digest(
inputString.encodeToByteArray().toUByteArray(), inputString.encodeToByteArray().toUByteArray(),
key?.run { encodeToByteArray().toUByteArray()} ?: ubyteArrayOf(), key?.run { encodeToByteArray().toUByteArray()} ?: ubyteArrayOf(),

View File

@ -134,7 +134,7 @@ class Sha512Pure : Sha512 {
) )
@ExperimentalStdlibApi @ExperimentalStdlibApi
override suspend fun digest(inputString: String, key: String?, hashLength: Int): UByteArray { override fun digest(inputString: String, key: String?, hashLength: Int): UByteArray {
return digest( return digest(
inputString.encodeToByteArray().toUByteArray(), inputString.encodeToByteArray().toUByteArray(),
key?.run { encodeToByteArray().toUByteArray() } ?: ubyteArrayOf(), key?.run { encodeToByteArray().toUByteArray() } ?: ubyteArrayOf(),