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
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(
inputMessage: UByteArray = ubyteArrayOf(),

View File

@ -9,5 +9,9 @@ import com.ionspin.kotlin.crypto.hash.UpdatableHash
* on 24-May-2020
*/
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 {
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()
if (ideaActive) {
when(hostOsName) {
"linux" -> linuxX64("native")
"macos" -> macosX64("native")
"windows" -> mingwX64("native")
"linux" -> linuxX64("native", libsodiumCompilation)
"macos" -> macosX64("native", libsodiumCompilation)
"windows" -> mingwX64("native", libsodiumCompilation)
}
}
if (hostOsName == "linux") {
@ -90,17 +99,7 @@ kotlin {
}
linuxX64("linux") {
compilations.getByName("main") {
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")
}
}
libsodiumCompilation(this)
binaries {
staticLib {
}

View File

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

View File

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

View File

@ -36,7 +36,7 @@ actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: I
actual object Blake2bStateless : Blake2bStatelessInterface {
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 hash = UByteArray(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 suspend fun initialize() {
}
actual fun initializeWithCallback(done: () -> Unit) {
}
}

View File

@ -29,7 +29,10 @@ actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: I
}
@ExperimentalUnsignedTypes
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")
}
@ -37,7 +40,4 @@ actual object Blake2bStateless : Blake2bStatelessInterface {
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 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")
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 hashResultPinned = hashResult.pin()
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
*/
import com.ionspin.kotlin.crypto.util.testBlocking
import interop.*
import kotlinx.cinterop.*
import libsodium.*
@ -22,7 +23,7 @@ class Blake2bLinuxTest {
}
@Test
fun testBlake2BSodiumInterop() {
fun testBlake2BSodiumInterop() = testBlocking {
Blake2bStateless.digest("test")
}
}

View File

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

View File

@ -145,7 +145,7 @@ class Blake2bPure(val key: UByteArray? = null, val hashLength: Int = 64) : Blake
}
@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 keyBytes = key?.run {
encodeToByteArray().toUByteArray()

View File

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

View File

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