Compare commits

..

No commits in common. "186db7478ade8c1ab379aee9f9cad939f2148d05" and "59f2d77890ca913271572d8c575c922b7c6e1d05" have entirely different histories.

27 changed files with 640 additions and 914 deletions

View File

@ -18,7 +18,7 @@
buildscript { buildscript {
dependencies { dependencies {
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:2.0.21") classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.23")
classpath("com.android.tools.build:gradle:7.2.2") classpath("com.android.tools.build:gradle:7.2.2")
classpath("org.jetbrains.dokka:dokka-gradle-plugin:1.9.20") classpath("org.jetbrains.dokka:dokka-gradle-plugin:1.9.20")
} }

View File

@ -28,7 +28,7 @@ repositories {
} }
dependencies { dependencies {
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:2.0.21") implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.23")
implementation("com.android.tools.build:gradle:7.2.2") implementation("com.android.tools.build:gradle:7.2.2")
implementation ("org.jetbrains.dokka:dokka-gradle-plugin:1.9.20") implementation ("org.jetbrains.dokka:dokka-gradle-plugin:1.9.20")
} }

View File

@ -16,8 +16,7 @@
object Versions { object Versions {
val kotlinCoroutines = "1.8.0" val kotlinCoroutines = "1.8.0"
val kotlinCoroutinesTest = "1.9.0" val kotlin = "1.9.23"
val kotlin = "2.0.21"
val kotlinSerialization = "1.6.3" val kotlinSerialization = "1.6.3"
val kotlinSerializationPlugin = kotlin val kotlinSerializationPlugin = kotlin
val taskTreePlugin = "1.5" val taskTreePlugin = "1.5"
@ -37,7 +36,7 @@ object Versions {
object ReleaseInfo { object ReleaseInfo {
val group = "com.ionspin.kotlin" val group = "com.ionspin.kotlin"
val bindingsVersion = "0.9.4-SNAPSHOT" val bindingsVersion = "0.9.3-SNAPSHOT"
} }
object Deps { object Deps {
@ -47,7 +46,6 @@ object Deps {
val test = "test-common" val test = "test-common"
val testAnnotation = "test-annotations-common" val testAnnotation = "test-annotations-common"
val coroutines = "org.jetbrains.kotlinx:kotlinx-coroutines-core:${Versions.kotlinCoroutines}" val coroutines = "org.jetbrains.kotlinx:kotlinx-coroutines-core:${Versions.kotlinCoroutines}"
val coroutinesTest = "org.jetbrains.kotlinx:kotlinx-coroutines-test:${Versions.kotlinCoroutinesTest}"
val serialization = "org.jetbrains.kotlinx:kotlinx-serialization-json:${Versions.kotlinSerialization}" val serialization = "org.jetbrains.kotlinx:kotlinx-serialization-json:${Versions.kotlinSerialization}"
val kotlinBigNum = "com.ionspin.kotlin:bignum:${Versions.kotlinBigNumVersion}" val kotlinBigNum = "com.ionspin.kotlin:bignum:${Versions.kotlinBigNumVersion}"
@ -75,6 +73,13 @@ object Deps {
} }
object wasmJs { object wasmJs {
// val stdLib = "stdlib-wasm"
// val test = "test-wasm"
// TODO: написано от балды \/
// val coroutines = "org.jetbrains.kotlinx:kotlinx-coroutines-core-js:${Versions.kotlinCoroutines}"
// val serialization = "org.jetbrains.kotlinx:kotlinx-serialization-runtime-js:${Versions.kotlinSerialization}"
object Npm { object Npm {
val libsodiumWrappers = Pair("libsodium-wrappers-sumo", "0.7.13") val libsodiumWrappers = Pair("libsodium-wrappers-sumo", "0.7.13")

File diff suppressed because it is too large Load Diff

View File

@ -17,7 +17,7 @@
@file:Suppress("UnstableApiUsage") @file:Suppress("UnstableApiUsage")
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.targets.native.tasks.KotlinNativeTest import org.jetbrains.kotlin.gradle.targets.native.tasks.KotlinNativeTest
plugins { plugins {
@ -41,6 +41,18 @@ kotlin {
runningOnLinuxx86_64 { runningOnLinuxx86_64 {
jvm() jvm()
// TODO: wasm тут копирует не апишный, если поменялся тот, то поменять и тут
@OptIn(ExperimentalWasmDsl::class)
wasmJs {
browser {
testTask {
useKarma {
useChrome()
}
}
}
}
js(IR) { js(IR) {
browser { browser {
testTask { testTask {
@ -59,16 +71,6 @@ kotlin {
} }
} }
@OptIn(ExperimentalWasmDsl::class)
wasmJs {
browser {
testTask {
useKarma {
useChrome()
}
}
}
}
linuxX64("linux") { linuxX64("linux") {
binaries { binaries {
staticLib { staticLib {
@ -124,8 +126,6 @@ kotlin {
dependencies { dependencies {
implementation(kotlin(Deps.Common.test)) implementation(kotlin(Deps.Common.test))
implementation(kotlin(Deps.Common.testAnnotation)) implementation(kotlin(Deps.Common.testAnnotation))
implementation(Deps.Common.coroutinesTest)
implementation(kotlin("test"))
} }
} }
@ -154,17 +154,24 @@ kotlin {
implementation(kotlin(Deps.Js.test)) implementation(kotlin(Deps.Js.test))
} }
} }
val wasmJsMain by getting { }
dependencies {
implementation(npm(Deps.wasmJs.Npm.libsodiumWrappers.first, Deps.wasmJs.Npm.libsodiumWrappers.second)) // TODO: может сунуть обратно в ранинг онг линукс, как и то, что выше
} val wasmJsMain by getting {
} dependencies {
val wasmJsTest by getting { // implementation(kotlin(Deps.wasmJs.stdLib))
dependencies { implementation(npm(Deps.wasmJs.Npm.libsodiumWrappers.first, Deps.wasmJs.Npm.libsodiumWrappers.second))
implementation(npm(Deps.wasmJs.Npm.libsodiumWrappers.first, Deps.wasmJs.Npm.libsodiumWrappers.second))
}
} }
} }
val wasmJsTest by getting {
dependencies {
implementation(npm(Deps.wasmJs.Npm.libsodiumWrappers.first, Deps.wasmJs.Npm.libsodiumWrappers.second))
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.9.0")
implementation(kotlin("test"))
}
}
runningOnMacos { runningOnMacos {
val tvosX64Main by getting { val tvosX64Main by getting {
dependsOn(commonMain) dependsOn(commonMain)

View File

@ -18,9 +18,10 @@
@file:Suppress("UnstableApiUsage") @file:Suppress("UnstableApiUsage")
import org.gradle.api.tasks.testing.logging.TestExceptionFormat import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.targets.js.testing.KotlinJsTest import org.jetbrains.kotlin.gradle.targets.js.testing.KotlinJsTest
import org.jetbrains.kotlin.gradle.targets.js.webpack.KotlinWebpackConfig
import org.jetbrains.kotlin.gradle.targets.native.tasks.KotlinNativeTest import org.jetbrains.kotlin.gradle.targets.native.tasks.KotlinNativeTest
plugins { plugins {
@ -89,11 +90,20 @@ kotlin {
} }
jvm() jvm()
val projectRef = project val projectRef = project
runningOnLinuxx86_64 { runningOnLinuxx86_64 {
println("Configuring Linux X86-64 targets") println("Configuring Linux X86-64 targets")
wasmJs {
browser {
testTask {
useKarma {
useChromeHeadless()
}
}
}
}
js { js {
browser { browser {
testTask { testTask {
@ -111,18 +121,6 @@ kotlin {
} }
} }
@OptIn(ExperimentalWasmDsl::class)
wasmJs {
browser {
testTask {
useKarma {
useChrome()
}
}
}
}
linuxX64() { linuxX64() {
compilations.getByName("main") { compilations.getByName("main") {
val libsodiumCinterop by cinterops.creating { val libsodiumCinterop by cinterops.creating {
@ -306,8 +304,10 @@ kotlin {
implementation(kotlin(Deps.Common.test)) implementation(kotlin(Deps.Common.test))
implementation(kotlin(Deps.Common.testAnnotation)) implementation(kotlin(Deps.Common.testAnnotation))
implementation(Deps.Common.coroutines) implementation(Deps.Common.coroutines)
implementation(Deps.Common.coroutinesTest)
// implementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.9.0")
implementation(kotlin("test")) implementation(kotlin("test"))
// implementation(kotlin("test-junit"))
} }
} }
@ -333,6 +333,24 @@ kotlin {
} }
} }
// TODO: это скопипасчено с блока runningOnLinuxx86_64 (примерно 590 строка)
val wasmJsMain by getting {
dependencies {
// implementation(kotlin(Deps.wasmJs.stdLib))
implementation(npm(Deps.wasmJs.Npm.libsodiumWrappers.first, Deps.wasmJs.Npm.libsodiumWrappers.second))
}
}
val wasmJsTest by getting {
dependencies {
dependsOn(commonTest)
implementation(npm(Deps.wasmJs.Npm.libsodiumWrappers.first, Deps.wasmJs.Npm.libsodiumWrappers.second))
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.9.0")
implementation(kotlin("test"))
}
}
//Set up shared source sets //Set up shared source sets
//linux, linuxArm32Hfp, linuxArm64 //linux, linuxArm32Hfp, linuxArm64
val linux64Bit = setOf( val linux64Bit = setOf(
@ -389,7 +407,7 @@ kotlin {
) )
) )
this@withType.compilations.getByName("main") { compilations.getByName("main") {
val libsodiumCinterop by cinterops.creating { val libsodiumCinterop by cinterops.creating {
defFile(projectRef.file("src/nativeInterop/cinterop/libsodium.def")) defFile(projectRef.file("src/nativeInterop/cinterop/libsodium.def"))
compilerOpts.add("-I${projectRef.rootDir}/sodiumWrapper/static-arm64/include/") compilerOpts.add("-I${projectRef.rootDir}/sodiumWrapper/static-arm64/include/")
@ -584,17 +602,6 @@ kotlin {
implementation(npm(Deps.Js.Npm.libsodiumWrappers.first, Deps.Js.Npm.libsodiumWrappers.second)) implementation(npm(Deps.Js.Npm.libsodiumWrappers.first, Deps.Js.Npm.libsodiumWrappers.second))
} }
} }
val wasmJsMain by getting {
dependencies {
implementation(npm(Deps.wasmJs.Npm.libsodiumWrappers.first, Deps.wasmJs.Npm.libsodiumWrappers.second))
}
}
val wasmJsTest by getting {
dependencies {
implementation(npm(Deps.wasmJs.Npm.libsodiumWrappers.first, Deps.wasmJs.Npm.libsodiumWrappers.second))
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.9.0")
}
}
val linuxX64Main by getting { val linuxX64Main by getting {
isRunningInIdea { isRunningInIdea {
kotlin.srcDir("src/nativeMain/kotlin") kotlin.srcDir("src/nativeMain/kotlin")
@ -733,12 +740,13 @@ tasks {
// } // }
// } // }
// val wasmJsBrowserTest by getting(KotlinJsTest::class) { // TODO: ваще не жс тест, помогите
// testLogging { val wasmJsBrowserTest by getting(KotlinJsTest::class) {
// events("PASSED", "FAILED", "SKIPPED") testLogging {
// showStandardStreams = true events("PASSED", "FAILED", "SKIPPED")
// } showStandardStreams = true
// } }
}
val jsBrowserTest by getting(KotlinJsTest::class) { val jsBrowserTest by getting(KotlinJsTest::class) {
testLogging { testLogging {

View File

@ -4,6 +4,7 @@ import com.ionspin.kotlin.crypto.generichash.GenericHash
import com.ionspin.kotlin.crypto.util.encodeToUByteArray import com.ionspin.kotlin.crypto.util.encodeToUByteArray
import com.ionspin.kotlin.crypto.util.testBlocking import com.ionspin.kotlin.crypto.util.testBlocking
import com.ionspin.kotlin.crypto.util.toHexString import com.ionspin.kotlin.crypto.util.toHexString
import com.ionspin.kotlin.crypto.util.runTest
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertTrue import kotlin.test.assertTrue
@ -16,6 +17,7 @@ class SmokeTest {
//TODO Browser ignores our testBlocking, node works fine though //TODO Browser ignores our testBlocking, node works fine though
@Test @Test
fun testIfLibraryIsNotOnFire() { fun testIfLibraryIsNotOnFire() {
throw Exception("aoaoao")
testBlocking { testBlocking {
LibsodiumInitializer.initialize() LibsodiumInitializer.initialize()
val hashResult = GenericHash.genericHash("Hello".encodeToUByteArray(), 64) val hashResult = GenericHash.genericHash("Hello".encodeToUByteArray(), 64)

View File

@ -36,6 +36,6 @@ fun testBlocking(block : suspend () -> Unit) {
block.startCoroutine(continuation) block.startCoroutine(continuation)
} }
fun runTest(block: suspend (scope : CoroutineScope) -> Unit) = kotlinx.coroutines.test.runTest { block(this) } expect fun runTest(block: suspend (scope : CoroutineScope) -> Unit)

View File

@ -1,7 +1,7 @@
package com.ionspin.kotlin.crypto.util package com.ionspin.kotlin.crypto.util
//import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
//import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
//import kotlinx.coroutines.promise import kotlinx.coroutines.promise
//
//actual fun runTest(block: suspend (scope : CoroutineScope) -> Unit): dynamic = GlobalScope.promise { block(this) } actual fun runTest(block: suspend (scope : CoroutineScope) -> Unit): dynamic = GlobalScope.promise { block(this) }

View File

@ -1,7 +1,7 @@
package com.ionspin.kotlin.crypto.util package com.ionspin.kotlin.crypto.util
//import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
//import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
//
//actual fun runTest(block: suspend (scope : CoroutineScope) -> Unit) = runBlocking { block(this) } actual fun runTest(block: suspend (scope : CoroutineScope) -> Unit) = runBlocking { block(this) }

View File

@ -1,7 +1,7 @@
package com.ionspin.kotlin.crypto.util package com.ionspin.kotlin.crypto.util
//import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
//import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
//
//actual fun runTest(block: suspend (scope : CoroutineScope) -> Unit) = runBlocking { block(this) } actual fun runTest(block: suspend (scope : CoroutineScope) -> Unit) = runBlocking { block(this) }

View File

@ -1,6 +1,7 @@
package ext.libsodium.com.ionspin.kotlin.crypto package ext.libsodium.com.ionspin.kotlin.crypto
import com.ionspin.kotlin.crypto.box.BoxKeyPair
import org.khronos.webgl.Uint8Array import org.khronos.webgl.Uint8Array
/** /**
@ -10,13 +11,9 @@ import org.khronos.webgl.Uint8Array
*/ */
@JsModule("libsodium-wrappers-sumo") @JsModule("libsodium-wrappers-sumo")
external object JsSodium: JsAny { external object JsSodiumInterface {
@JsName("default")
val default : JsSodiumInterface
}
@JsModule("libsodium-wrappers-sumo")
external object JsSodiumInterface: JsAny {
@JsName("crypto_generichash") @JsName("crypto_generichash")
fun crypto_generichash(hashLength: Int, inputMessage: Uint8Array, key: Uint8Array): Uint8Array fun crypto_generichash(hashLength: Int, inputMessage: Uint8Array, key: Uint8Array): Uint8Array
@ -29,13 +26,13 @@ external object JsSodiumInterface: JsAny {
// ---- Generic hash ---- // Updateable // ---- Generic hash ---- // Updateable
@JsName("crypto_generichash_init") @JsName("crypto_generichash_init")
fun crypto_generichash_init(key : Uint8Array, hashLength: Int) : GenericHashStateInternalType fun crypto_generichash_init(key : Uint8Array, hashLength: Int) : JsAny
@JsName("crypto_generichash_update") @JsName("crypto_generichash_update")
fun crypto_generichash_update(state: GenericHashStateInternalType, inputMessage: Uint8Array) fun crypto_generichash_update(state: JsAny, inputMessage: Uint8Array)
@JsName("crypto_generichash_final") @JsName("crypto_generichash_final")
fun crypto_generichash_final(state: GenericHashStateInternalType, hashLength: Int) : Uint8Array fun crypto_generichash_final(state: JsAny, hashLength: Int) : Uint8Array
@JsName("crypto_generichash_keygen") @JsName("crypto_generichash_keygen")
fun crypto_generichash_keygen() : Uint8Array fun crypto_generichash_keygen() : Uint8Array
@ -43,18 +40,19 @@ external object JsSodiumInterface: JsAny {
// ---- Generic hash end ---- // Updateable // ---- Generic hash end ---- // Updateable
// ---- Blake2b ---- // ---- Blake2b ----
// I
@JsName("crypto_generichash_blake2b") @JsName("crypto_generichash_blake2b")
fun crypto_generichash_blake2b(hashLength: Int, inputMessage: Uint8Array, key: Uint8Array): Uint8Array fun crypto_generichash_blake2b(hashLength: Int, inputMessage: Uint8Array, key: Uint8Array): Uint8Array
@JsName("crypto_generichash_blake2b_init") @JsName("crypto_generichash_blake2b_init")
fun crypto_generichash_blake2b_init(key : Uint8Array, hashLength: Int) : Blake2bInternalStateType fun crypto_generichash_blake2b_init(key : Uint8Array, hashLength: Int) : JsAny
@JsName("crypto_generichash_blake2b_update") @JsName("crypto_generichash_blake2b_update")
fun crypto_generichash_blake2b_update(state: Blake2bInternalStateType, inputMessage: Uint8Array) fun crypto_generichash_blake2b_update(state: JsAny, inputMessage: Uint8Array)
// TODO: строка ниже просто висела без ничего, я ее закомментила
//crypto_secretstream_xchacha20poly1305_init_push
@JsName("crypto_generichash_blake2b_final") @JsName("crypto_generichash_blake2b_final")
fun crypto_generichash_blake2b_final(state: Blake2bInternalStateType, hashLength: Int) : Uint8Array fun crypto_generichash_blake2b_final(state: JsAny, hashLength: Int) : Uint8Array
@JsName("crypto_generichash_blake2b_keygen") @JsName("crypto_generichash_blake2b_keygen")
fun crypto_generichash_blake2b_keygen() : Uint8Array fun crypto_generichash_blake2b_keygen() : Uint8Array
@ -71,22 +69,22 @@ external object JsSodiumInterface: JsAny {
@JsName("crypto_hash_sha256_init") @JsName("crypto_hash_sha256_init")
fun crypto_hash_sha256_init() : Sha256StateType fun crypto_hash_sha256_init() : JsAny
@JsName("crypto_hash_sha256_update") @JsName("crypto_hash_sha256_update")
fun crypto_hash_sha256_update(state: Sha256StateType, message: Uint8Array) fun crypto_hash_sha256_update(state: JsAny, message: Uint8Array)
@JsName("crypto_hash_sha256_final") @JsName("crypto_hash_sha256_final")
fun crypto_hash_sha256_final(state: Sha256StateType): Uint8Array fun crypto_hash_sha256_final(state: JsAny): Uint8Array
@JsName("crypto_hash_sha512_init") @JsName("crypto_hash_sha512_init")
fun crypto_hash_sha512_init() : Sha512StateType fun crypto_hash_sha512_init() : JsAny
@JsName("crypto_hash_sha512_update") @JsName("crypto_hash_sha512_update")
fun crypto_hash_sha512_update(state: Sha512StateType, message: Uint8Array) fun crypto_hash_sha512_update(state: JsAny, message: Uint8Array)
@JsName("crypto_hash_sha512_final") @JsName("crypto_hash_sha512_final")
fun crypto_hash_sha512_final(state: Sha512StateType): Uint8Array fun crypto_hash_sha512_final(state: JsAny): Uint8Array
//XChaCha20Poly1305 - also in bindings //XChaCha20Poly1305 - also in bindings
//fun crypto_aead_xchacha20poly1305_ietf_encrypt(message: Uint8Array, associatedData: Uint8Array, secretNonce: Uint8Array, nonce: Uint8Array, key: Uint8Array) : Uint8Array //fun crypto_aead_xchacha20poly1305_ietf_encrypt(message: Uint8Array, associatedData: Uint8Array, secretNonce: Uint8Array, nonce: Uint8Array, key: Uint8Array) : Uint8Array
@ -95,33 +93,36 @@ external object JsSodiumInterface: JsAny {
//XChaCha20Poly1305 //XChaCha20Poly1305
//encrypt //encrypt
@JsName("crypto_secretstream_xchacha20poly1305_init_push") @JsName("crypto_secretstream_xchacha20poly1305_init_push")
fun crypto_secretstream_xchacha20poly1305_init_push(key: Uint8Array) : SecretStreamStateAndHeaderType fun crypto_secretstream_xchacha20poly1305_init_push(key: Uint8Array) : CryptoSecretstreamXchacha20poly1305InitPushResult
@JsName("crypto_secretstream_xchacha20poly1305_push") @JsName("crypto_secretstream_xchacha20poly1305_push")
fun crypto_secretstream_xchacha20poly1305_push(state: SecretStreamStateType, message: Uint8Array, associatedData: Uint8Array, tag: Byte) : Uint8Array // TODO: два варианта: \/
// 1. Меняем юбайт на байт и юинт на инт \/
// 2. Меняем юбайт на инт и юинт на лонг \/ и далее по списку
fun crypto_secretstream_xchacha20poly1305_push(state: JsAny, message: Uint8Array, associatedData: Uint8Array, tag: Byte) : Uint8Array
//decrypt //decrypt
@JsName("crypto_secretstream_xchacha20poly1305_init_pull") @JsName("crypto_secretstream_xchacha20poly1305_init_pull")
fun crypto_secretstream_xchacha20poly1305_init_pull(header: Uint8Array, key: Uint8Array) : SecretStreamStateType fun crypto_secretstream_xchacha20poly1305_init_pull(header: Uint8Array, key: Uint8Array) : JsAny
@JsName("crypto_secretstream_xchacha20poly1305_pull") @JsName("crypto_secretstream_xchacha20poly1305_pull")
fun crypto_secretstream_xchacha20poly1305_pull(state: SecretStreamStateType, ciphertext: Uint8Array, associatedData: Uint8Array) : DecryptedDataAndTagType fun crypto_secretstream_xchacha20poly1305_pull(state: JsAny, ciphertext: Uint8Array, associatedData: Uint8Array) : JsAny
//keygen and rekey //keygen and rekey
@JsName("crypto_secretstream_xchacha20poly1305_keygen") @JsName("crypto_secretstream_xchacha20poly1305_keygen")
fun crypto_secretstream_xchacha20poly1305_keygen() : Uint8Array fun crypto_secretstream_xchacha20poly1305_keygen() : Uint8Array
@JsName("crypto_secretstream_xchacha20poly1305_rekey") @JsName("crypto_secretstream_xchacha20poly1305_rekey")
fun crypto_secretstream_xchacha20poly1305_rekey(state: SecretStreamStateType) fun crypto_secretstream_xchacha20poly1305_rekey(state: JsAny)
// ---- SecretBox ---- // ---- SecretBox ----
@JsName("crypto_secretbox_detached") @JsName("crypto_secretbox_detached")
fun crypto_secretbox_detached(message: Uint8Array, nonce: Uint8Array, key: Uint8Array) : SecretBoxEncryptedType fun crypto_secretbox_detached(message: Uint8Array, nonce: Uint8Array, key: Uint8Array) : CryptoSecretboxDetachedResult
@JsName("crypto_secretbox_easy") @JsName("crypto_secretbox_easy")
fun crypto_secretbox_easy(message: Uint8Array, nonce: Uint8Array, key: Uint8Array) : Uint8Array fun crypto_secretbox_easy(message: Uint8Array, nonce: Uint8Array, key: Uint8Array) : Uint8Array
@JsName("crypto_secretbox_keygen") @JsName("crypto_secretbox_keygen")
fun crypto_secretbox_keygen() : Uint8Array fun crypto_secretbox_keygen() : Uint8Array
@JsName("crypto_secretbox_open_detached") @JsName("crypto_secretbox_open_detached")
fun crypto_secretbox_open_detached(ciphertext : Uint8Array, tag : Uint8Array, nonce: Uint8Array, key: Uint8Array) : Uint8Array fun crypto_secretbox_open_detached(ciphertext : Uint8Array, tag : Uint8Array, nonce: Uint8Array, key: Uint8Array) : JsAny
@JsName("crypto_secretbox_open_easy") @JsName("crypto_secretbox_open_easy")
fun crypto_secretbox_open_easy(ciphertext : Uint8Array, nonce: Uint8Array, key: Uint8Array) : Uint8Array fun crypto_secretbox_open_easy(ciphertext : Uint8Array, nonce: Uint8Array, key: Uint8Array) : JsAny
// ---- SecretBox End ---- // ---- SecretBox End ----
@ -135,7 +136,7 @@ external object JsSodiumInterface: JsAny {
@JsName("crypto_aead_chacha20poly1305_encrypt") @JsName("crypto_aead_chacha20poly1305_encrypt")
fun crypto_aead_chacha20poly1305_encrypt(message: Uint8Array, associatedData: Uint8Array, nsec: Uint8Array?, npub: Uint8Array, key: Uint8Array) : Uint8Array fun crypto_aead_chacha20poly1305_encrypt(message: Uint8Array, associatedData: Uint8Array, nsec: Uint8Array?, npub: Uint8Array, key: Uint8Array) : Uint8Array
@JsName("crypto_aead_chacha20poly1305_encrypt_detached") @JsName("crypto_aead_chacha20poly1305_encrypt_detached")
fun crypto_aead_chacha20poly1305_encrypt_detached(message: Uint8Array, associatedData: Uint8Array, nsec: Uint8Array?, npub: Uint8Array, key: Uint8Array) : AeadEncryptedType fun crypto_aead_chacha20poly1305_encrypt_detached(message: Uint8Array, associatedData: Uint8Array, nsec: Uint8Array?, npub: Uint8Array, key: Uint8Array) : Chacha20poly1305EncryptDetachedResult
@JsName("crypto_aead_chacha20poly1305_ietf_decrypt") @JsName("crypto_aead_chacha20poly1305_ietf_decrypt")
fun crypto_aead_chacha20poly1305_ietf_decrypt(nsec : Uint8Array?, ciphertext: Uint8Array, associatedData: Uint8Array, npub: Uint8Array, key: Uint8Array) : Uint8Array fun crypto_aead_chacha20poly1305_ietf_decrypt(nsec : Uint8Array?, ciphertext: Uint8Array, associatedData: Uint8Array, npub: Uint8Array, key: Uint8Array) : Uint8Array
@JsName("crypto_aead_chacha20poly1305_ietf_decrypt_detached") @JsName("crypto_aead_chacha20poly1305_ietf_decrypt_detached")
@ -143,7 +144,7 @@ external object JsSodiumInterface: JsAny {
@JsName("crypto_aead_chacha20poly1305_ietf_encrypt") @JsName("crypto_aead_chacha20poly1305_ietf_encrypt")
fun crypto_aead_chacha20poly1305_ietf_encrypt(message: Uint8Array, associatedData: Uint8Array, nsec: Uint8Array?, npub: Uint8Array, key: Uint8Array) : Uint8Array fun crypto_aead_chacha20poly1305_ietf_encrypt(message: Uint8Array, associatedData: Uint8Array, nsec: Uint8Array?, npub: Uint8Array, key: Uint8Array) : Uint8Array
@JsName("crypto_aead_chacha20poly1305_ietf_encrypt_detached") @JsName("crypto_aead_chacha20poly1305_ietf_encrypt_detached")
fun crypto_aead_chacha20poly1305_ietf_encrypt_detached(message: Uint8Array, associatedData: Uint8Array, nsec: Uint8Array?, npub: Uint8Array, key: Uint8Array) : AeadEncryptedType fun crypto_aead_chacha20poly1305_ietf_encrypt_detached(message: Uint8Array, associatedData: Uint8Array, nsec: Uint8Array?, npub: Uint8Array, key: Uint8Array) : Chacha20poly1305EncryptDetachedResult
@JsName("crypto_aead_chacha20poly1305_ietf_keygen") @JsName("crypto_aead_chacha20poly1305_ietf_keygen")
fun crypto_aead_chacha20poly1305_ietf_keygen() : Uint8Array fun crypto_aead_chacha20poly1305_ietf_keygen() : Uint8Array
@JsName("crypto_aead_chacha20poly1305_keygen") @JsName("crypto_aead_chacha20poly1305_keygen")
@ -155,7 +156,7 @@ external object JsSodiumInterface: JsAny {
@JsName("crypto_aead_xchacha20poly1305_ietf_encrypt") @JsName("crypto_aead_xchacha20poly1305_ietf_encrypt")
fun crypto_aead_xchacha20poly1305_ietf_encrypt(message: Uint8Array, associatedData: Uint8Array, nsec: Uint8Array?, npub: Uint8Array, key: Uint8Array) : Uint8Array fun crypto_aead_xchacha20poly1305_ietf_encrypt(message: Uint8Array, associatedData: Uint8Array, nsec: Uint8Array?, npub: Uint8Array, key: Uint8Array) : Uint8Array
@JsName("crypto_aead_xchacha20poly1305_ietf_encrypt_detached") @JsName("crypto_aead_xchacha20poly1305_ietf_encrypt_detached")
fun crypto_aead_xchacha20poly1305_ietf_encrypt_detached(message: Uint8Array, associatedData: Uint8Array, nsec: Uint8Array?, npub: Uint8Array, key: Uint8Array) : AeadEncryptedType fun crypto_aead_xchacha20poly1305_ietf_encrypt_detached(message: Uint8Array, associatedData: Uint8Array, nsec: Uint8Array?, npub: Uint8Array, key: Uint8Array) : Chacha20poly1305EncryptDetachedResult
@JsName("crypto_aead_xchacha20poly1305_ietf_keygen") @JsName("crypto_aead_xchacha20poly1305_ietf_keygen")
fun crypto_aead_xchacha20poly1305_ietf_keygen(): Uint8Array fun crypto_aead_xchacha20poly1305_ietf_keygen(): Uint8Array
@ -187,9 +188,9 @@ external object JsSodiumInterface: JsAny {
// ---- Box ---- // ---- Box ----
@JsName("crypto_box_keypair") @JsName("crypto_box_keypair")
fun crypto_box_keypair() : KeyExchangeKeyPairType fun crypto_box_keypair() : Keypair
@JsName("crypto_box_seed_keypair") @JsName("crypto_box_seed_keypair")
fun crypto_box_seed_keypair(seed : Uint8Array) : KeyExchangeKeyPairType fun crypto_box_seed_keypair(seed : Uint8Array) : Keypair
@JsName("crypto_box_easy") @JsName("crypto_box_easy")
fun crypto_box_easy(message: Uint8Array, fun crypto_box_easy(message: Uint8Array,
nonce: Uint8Array, nonce: Uint8Array,
@ -204,7 +205,7 @@ external object JsSodiumInterface: JsAny {
fun crypto_box_detached(message: Uint8Array, fun crypto_box_detached(message: Uint8Array,
nonce: Uint8Array, nonce: Uint8Array,
recipientsPublicKey: Uint8Array, recipientsPublicKey: Uint8Array,
sendersSecretKey: Uint8Array) : BoxEncryptedType sendersSecretKey: Uint8Array) : CryptoBoxDetachedResult
@JsName("crypto_box_open_detached") @JsName("crypto_box_open_detached")
fun crypto_box_open_detached(ciphertext: Uint8Array, fun crypto_box_open_detached(ciphertext: Uint8Array,
tag: Uint8Array, tag: Uint8Array,
@ -242,19 +243,19 @@ external object JsSodiumInterface: JsAny {
@JsName("crypto_sign_ed25519_sk_to_seed") @JsName("crypto_sign_ed25519_sk_to_seed")
fun crypto_sign_ed25519_sk_to_seed(ed25519SecretKey: Uint8Array) : Uint8Array fun crypto_sign_ed25519_sk_to_seed(ed25519SecretKey: Uint8Array) : Uint8Array
@JsName("crypto_sign_final_create") @JsName("crypto_sign_final_create")
fun crypto_sign_final_create(state: SignatureStateType, secretKey: Uint8Array) : Uint8Array fun crypto_sign_final_create(state: JsAny, secretKey: Uint8Array) : Uint8Array
@JsName("crypto_sign_final_verify") @JsName("crypto_sign_final_verify")
fun crypto_sign_final_verify(state: SignatureStateType, signature: Uint8Array, publicKey: Uint8Array) : Boolean fun crypto_sign_final_verify(state: JsAny, signature: Uint8Array, publicKey: Uint8Array) : Boolean
@JsName("crypto_sign_init") @JsName("crypto_sign_init")
fun crypto_sign_init() : SignatureStateType fun crypto_sign_init() : SignatureStateType
@JsName("crypto_sign_keypair") @JsName("crypto_sign_keypair")
fun crypto_sign_keypair() : KeyExchangeKeyPairType fun crypto_sign_keypair() : Keypair
@JsName("crypto_sign_open") @JsName("crypto_sign_open")
fun crypto_sign_open(signedMessage: Uint8Array, publicKey: Uint8Array) : Uint8Array fun crypto_sign_open(signedMessage: Uint8Array, publicKey: Uint8Array) : Uint8Array
@JsName("crypto_sign_seed_keypair") @JsName("crypto_sign_seed_keypair")
fun crypto_sign_seed_keypair(seed: Uint8Array) : KeyExchangeKeyPairType fun crypto_sign_seed_keypair(seed: Uint8Array) : Keypair
@JsName("crypto_sign_update") @JsName("crypto_sign_update")
fun crypto_sign_update(state: SignatureStateType, message: Uint8Array) fun crypto_sign_update(state: JsAny, message: Uint8Array)
@JsName("crypto_sign_verify_detached") @JsName("crypto_sign_verify_detached")
fun crypto_sign_verify_detached(signature: Uint8Array, message: Uint8Array, publicKey: Uint8Array) : Boolean fun crypto_sign_verify_detached(signature: Uint8Array, message: Uint8Array, publicKey: Uint8Array) : Boolean
@ -323,13 +324,13 @@ external object JsSodiumInterface: JsAny {
// ---- Key exchange ---- // ---- Key exchange ----
@JsName("crypto_kx_client_session_keys") @JsName("crypto_kx_client_session_keys")
fun crypto_kx_client_session_keys(clientPublicKey: Uint8Array, clientSecretKey: Uint8Array, serverPublicKey: Uint8Array) : KeyExchangeSessionKeyPairType fun crypto_kx_client_session_keys(clientPublicKey: Uint8Array, clientSecretKey: Uint8Array, serverPublicKey: Uint8Array) : CryptoKxClientSessionKeysResult
@JsName("crypto_kx_keypair") @JsName("crypto_kx_keypair")
fun crypto_kx_keypair() : KeyExchangeKeyPairType fun crypto_kx_keypair() : Keypair
@JsName("crypto_kx_seed_keypair") @JsName("crypto_kx_seed_keypair")
fun crypto_kx_seed_keypair(seed: Uint8Array) : KeyExchangeKeyPairType fun crypto_kx_seed_keypair(seed: Uint8Array) : Keypair
@JsName("crypto_kx_server_session_keys") @JsName("crypto_kx_server_session_keys")
fun crypto_kx_server_session_keys(serverPublicKey: Uint8Array, serverSecretKey: Uint8Array, clientPublicKey: Uint8Array) : KeyExchangeSessionKeyPairType fun crypto_kx_server_session_keys(serverPublicKey: Uint8Array, serverSecretKey: Uint8Array, clientPublicKey: Uint8Array) : CryptoKxServerSessionKeysResult
// ---- Key exchange end ---- // ---- Key exchange end ----
@ -366,112 +367,5 @@ external object JsSodiumInterface: JsAny {
// ---- Scalar multiplication end ---- // ---- Scalar multiplication end ----
//
// ---- Ristretto255 ----
@JsName("crypto_core_ristretto255_is_valid_point")
fun crypto_core_ristretto255_is_valid_point(p: Uint8Array): Boolean
@JsName("crypto_core_ristretto255_random")
fun crypto_core_ristretto255_random(): Uint8Array
@JsName("crypto_core_ristretto255_from_hash")
fun crypto_core_ristretto255_from_hash(r: Uint8Array): Uint8Array
@JsName("crypto_core_ristretto255_add")
fun crypto_core_ristretto255_add(p: Uint8Array, q: Uint8Array): Uint8Array
@JsName("crypto_core_ristretto255_sub")
fun crypto_core_ristretto255_sub(p: Uint8Array, q: Uint8Array): Uint8Array
@JsName("crypto_core_ristretto255_scalar_random")
fun crypto_core_ristretto255_scalar_random(): Uint8Array
@JsName("crypto_core_ristretto255_scalar_reduce")
fun crypto_core_ristretto255_scalar_reduce(s: Uint8Array): Uint8Array
@JsName("crypto_core_ristretto255_scalar_invert")
fun crypto_core_ristretto255_scalar_invert(s: Uint8Array): Uint8Array
@JsName("crypto_core_ristretto255_scalar_negate")
fun crypto_core_ristretto255_scalar_negate(s: Uint8Array): Uint8Array
@JsName("crypto_core_ristretto255_scalar_complement")
fun crypto_core_ristretto255_scalar_complement(s: Uint8Array): Uint8Array
@JsName("crypto_core_ristretto255_scalar_add")
fun crypto_core_ristretto255_scalar_add(x: Uint8Array, y: Uint8Array): Uint8Array
@JsName("crypto_core_ristretto255_scalar_sub")
fun crypto_core_ristretto255_scalar_sub(x: Uint8Array, y: Uint8Array): Uint8Array
@JsName("crypto_core_ristretto255_scalar_mul")
fun crypto_core_ristretto255_scalar_mul(x: Uint8Array, y: Uint8Array): Uint8Array
@JsName("crypto_scalarmult_ristretto255")
fun crypto_scalarmult_ristretto255(n: Uint8Array, p: Uint8Array): Uint8Array
@JsName("crypto_scalarmult_ristretto255_base")
fun crypto_scalarmult_ristretto255_base(n: Uint8Array): Uint8Array
//
// ---- Ristretto255 end ----
//
// ---- Ed25519 ----
@JsName("crypto_core_ed25519_is_valid_point")
fun crypto_core_ed25519_is_valid_point(p: Uint8Array): Boolean
@JsName("crypto_core_ed25519_random")
fun crypto_core_ed25519_random(): Uint8Array
@JsName("crypto_core_ed25519_from_uniform")
fun crypto_core_ed25519_from_uniform(r: Uint8Array): Uint8Array
@JsName("crypto_core_ed25519_add")
fun crypto_core_ed25519_add(p: Uint8Array, q: Uint8Array): Uint8Array
@JsName("crypto_core_ed25519_sub")
fun crypto_core_ed25519_sub(p: Uint8Array, q: Uint8Array): Uint8Array
@JsName("crypto_core_ed25519_scalar_random")
fun crypto_core_ed25519_scalar_random(): Uint8Array
@JsName("crypto_core_ed25519_scalar_reduce")
fun crypto_core_ed25519_scalar_reduce(s: Uint8Array): Uint8Array
@JsName("crypto_core_ed25519_scalar_invert")
fun crypto_core_ed25519_scalar_invert(s: Uint8Array): Uint8Array
@JsName("crypto_core_ed25519_scalar_negate")
fun crypto_core_ed25519_scalar_negate(s: Uint8Array): Uint8Array
@JsName("crypto_core_ed25519_scalar_complement")
fun crypto_core_ed25519_scalar_complement(s: Uint8Array): Uint8Array
@JsName("crypto_core_ed25519_scalar_add")
fun crypto_core_ed25519_scalar_add(x: Uint8Array, y: Uint8Array): Uint8Array
@JsName("crypto_core_ed25519_scalar_sub")
fun crypto_core_ed25519_scalar_sub(x: Uint8Array, y: Uint8Array): Uint8Array
@JsName("crypto_core_ed25519_scalar_mul")
fun crypto_core_ed25519_scalar_mul(x: Uint8Array, y: Uint8Array): Uint8Array
@JsName("crypto_scalarmult_ed25519")
fun crypto_scalarmult_ed25519(n: Uint8Array, p: Uint8Array): Uint8Array
@JsName("crypto_scalarmult_ed25519_noclamp")
fun crypto_scalarmult_ed25519_noclamp(n: Uint8Array, p: Uint8Array): Uint8Array
@JsName("crypto_scalarmult_ed25519_base")
fun crypto_scalarmult_ed25519_base(n: Uint8Array): Uint8Array
@JsName("crypto_scalarmult_ed25519_base_noclamp")
fun crypto_scalarmult_ed25519_base_noclamp(n: Uint8Array): Uint8Array
//
// ---- Ed25519 end ----
} }

View File

@ -2,7 +2,12 @@ package ext.libsodium.com.ionspin.kotlin.crypto
import com.ionspin.kotlin.crypto.getSodiumLoaded import com.ionspin.kotlin.crypto.getSodiumLoaded
import com.ionspin.kotlin.crypto.sodiumLoaded import com.ionspin.kotlin.crypto.sodiumLoaded
import ext.libsodium.* import ext.libsodium._libsodiumPromise
import ext.libsodium.crypto_generichash
import ext.libsodium.crypto_hash_sha256
import ext.libsodium.crypto_hash_sha256_init
import ext.libsodium.crypto_hash_sha512
import ext.libsodium.sodium_init
import kotlin.coroutines.suspendCoroutine import kotlin.coroutines.suspendCoroutine
/** /**
@ -22,19 +27,21 @@ object JsSodiumLoader {
} }
// TODO: попробовать сделать из этого suspend вместо continuation
suspend fun load(): Unit = suspendCoroutine { continuation -> suspend fun load(): Unit = suspendCoroutine { continuation ->
if (!getSodiumLoaded()) { if (!getSodiumLoaded()) {
_libsodiumPromise.then<JsAny?> { _libsodiumPromise.then<JsAny?> {
sodium_init() sodium_init()
sodiumLoaded = true sodiumLoaded = true
//Dynamic может быть Юнит, но Unit не может быть JsAny?
continuation.resumeWith(Result.success(Unit)) continuation.resumeWith(Result.success(Unit))
null null
}.catch { e -> }.catch { e ->
val throwable = e.toThrowableOrNull() val throwable = e as? Throwable
if (throwable != null) { if (throwable != null) {
continuation.resumeWith(Result.failure(throwable)) continuation.resumeWith(Result.failure(throwable))
} else { } else {
continuation.resumeWith(Result.failure(Throwable("Unknown error", throwable))) continuation.resumeWith(Result.failure(Exception("Error: $e")))
} }
null null
} }
@ -43,13 +50,12 @@ object JsSodiumLoader {
} }
} }
fun loadWithCallback(doneCallback: () -> (Unit)) { fun loadWithCallback(doneCallback: () -> (JsAny)) {
if (!getSodiumLoaded()) { if (!getSodiumLoaded()) {
_libsodiumPromise.then<JsAny?> { _libsodiumPromise.then<JsAny> {
sodium_init() sodium_init()
sodiumLoaded = true sodiumLoaded = true
doneCallback.invoke() doneCallback.invoke()
null
} }
} else { } else {
doneCallback.invoke() doneCallback.invoke()

View File

@ -1,12 +1,11 @@
package com.ionspin.kotlin.crypto package com.ionspin.kotlin.crypto
import ext.libsodium.com.ionspin.kotlin.crypto.JsSodium
import ext.libsodium.com.ionspin.kotlin.crypto.JsSodiumInterface import ext.libsodium.com.ionspin.kotlin.crypto.JsSodiumInterface
import ext.libsodium.com.ionspin.kotlin.crypto.JsSodiumLoader import ext.libsodium.com.ionspin.kotlin.crypto.JsSodiumLoader
var sodiumLoaded: Boolean = false var sodiumLoaded: Boolean = false
fun getSodium() : JsSodiumInterface = JsSodium.default fun getSodium() : JsSodiumInterface = JsSodiumInterface
fun getSodiumLoaded() : Boolean = sodiumLoaded fun getSodiumLoaded() : Boolean = sodiumLoaded
@ -27,6 +26,8 @@ actual object LibsodiumInitializer {
JsSodiumLoader.loadWithCallback { JsSodiumLoader.loadWithCallback {
isPlatformInitialized = true isPlatformInitialized = true
done() done()
// TODO: there's no return needed!!!
"null".toJsString()
} }
} }

View File

@ -1,50 +1,58 @@
package ext.libsodium.com.ionspin.kotlin.crypto package ext.libsodium.com.ionspin.kotlin.crypto
import org.khronos.webgl.Uint8Array import org.khronos.webgl.Uint8Array
//TODO: может быть стоит поудалять ненужное
external object Sha256StateType: JsAny
external object Sha512StateType: JsAny
external object SignatureStateType: JsAny external object SignatureStateType: JsAny
external object AeadEncryptedType : JsAny { external object Chacha20poly1305EncryptDetachedResult : JsAny {
val ciphertext: Uint8Array val ciphertext: Uint8Array
var mac: Uint8Array var mac: Uint8Array
} }
external object BoxEncryptedType : JsAny { external object CryptoBoxDetachedResult : JsAny {
val ciphertext: Uint8Array val ciphertext: Uint8Array
var mac: Uint8Array var mac: Uint8Array
} }
external object KeyExchangeSessionKeyPairType: JsAny { //external object CryptoBoxKeypairResult: JsAny {
// val publicKey: Uint8Array
// val privateKey: Uint8Array
//}
external object CryptoKxClientSessionKeysResult: JsAny {
val sharedRx: Uint8Array val sharedRx: Uint8Array
val sharedTx: Uint8Array val sharedTx: Uint8Array
} }
external object KeyExchangeKeyPairType: JsAny { //external object CryptoKxKeypairResult: JsAny {
// val publicKey: Uint8Array
// val privateKey: Uint8Array
//}
external object Keypair: JsAny {
val publicKey: Uint8Array val publicKey: Uint8Array
val privateKey: Uint8Array val privateKey: Uint8Array
} }
//
external object CryptoKxServerSessionKeysResult: JsAny {
val sharedRx: Uint8Array
val sharedTx: Uint8Array
}
external object SecretBoxEncryptedType: JsAny { external object CryptoSecretboxDetachedResult: JsAny {
val cipher: Uint8Array val cipher: Uint8Array
val mac: Uint8Array val mac: Uint8Array
} }
external object SecretStreamStateType: JsAny external object CryptoSecretstreamXchacha20poly1305InitPushResult: JsAny {
val state: Uint8Array
external object SecretStreamStateAndHeaderType: JsAny {
val state: SecretStreamStateType
val header: Uint8Array val header: Uint8Array
} }
external object DecryptedDataAndTagType: JsAny { external object CryptoSecretstreamXchacha20poly1305PullResult: JsAny {
val message: Uint8Array val message: Uint8Array
val tag: Byte val tag: Byte
} }
external object GenericHashStateInternalType: JsAny
external object Blake2bInternalStateType: JsAny

View File

@ -3,6 +3,7 @@ package com.ionspin.kotlin.crypto.aead
import com.ionspin.kotlin.crypto.getSodium import com.ionspin.kotlin.crypto.getSodium
import ext.libsodium.com.ionspin.kotlin.crypto.toUByteArray import ext.libsodium.com.ionspin.kotlin.crypto.toUByteArray
import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array
import org.khronos.webgl.Uint8Array
actual object AuthenticatedEncryptionWithAssociatedData { actual object AuthenticatedEncryptionWithAssociatedData {
@ -130,8 +131,8 @@ actual object AuthenticatedEncryptionWithAssociatedData {
key.toUInt8Array(), key.toUInt8Array(),
) )
return AeadEncryptedDataAndTag( return AeadEncryptedDataAndTag(
result.ciphertext.toUByteArray(), (result.ciphertext as Uint8Array).toUByteArray(),
result.mac.toUByteArray() (result.mac as Uint8Array).toUByteArray()
) )
} }
@ -204,8 +205,8 @@ actual object AuthenticatedEncryptionWithAssociatedData {
key.toUInt8Array(), key.toUInt8Array(),
) )
return AeadEncryptedDataAndTag( return AeadEncryptedDataAndTag(
result.ciphertext.toUByteArray(), (result.ciphertext as Uint8Array).toUByteArray(),
result.mac.toUByteArray() (result.mac as Uint8Array).toUByteArray()
) )
} }

View File

@ -1,106 +0,0 @@
package com.ionspin.kotlin.crypto.ed25519
import com.ionspin.kotlin.crypto.getSodium
import ext.libsodium.com.ionspin.kotlin.crypto.toUByteArray
import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array
actual object Ed25519LowLevel {
actual fun isValidPoint(encoded: UByteArray): Boolean =
getSodium().crypto_core_ed25519_is_valid_point(encoded.toUInt8Array())
actual fun addPoints(p: UByteArray, q: UByteArray): UByteArray {
val result = getSodium().crypto_core_ed25519_add(p.toUInt8Array(), q.toUInt8Array())
return result.toUByteArray()
}
actual fun subtractPoints(p: UByteArray, q: UByteArray): UByteArray {
val result = getSodium().crypto_core_ed25519_sub(p.toUInt8Array(), q.toUInt8Array())
return result.toUByteArray()
}
actual fun pointFromUniform(uniform: UByteArray): UByteArray {
val result = getSodium().crypto_core_ed25519_from_uniform(uniform.toUInt8Array())
return result.toUByteArray()
}
actual fun randomPoint(): UByteArray {
val result = getSodium().crypto_core_ed25519_random()
return result.toUByteArray()
}
actual fun randomScalar(): UByteArray {
val result = getSodium().crypto_core_ed25519_scalar_random()
return result.toUByteArray()
}
actual fun invertScalar(scalar: UByteArray): UByteArray {
val result = getSodium().crypto_core_ed25519_scalar_invert(scalar.toUInt8Array())
return result.toUByteArray()
}
actual fun negateScalar(scalar: UByteArray): UByteArray {
val result = getSodium().crypto_core_ed25519_scalar_negate(scalar.toUInt8Array())
return result.toUByteArray()
}
actual fun complementScalar(scalar: UByteArray): UByteArray {
val result = getSodium().crypto_core_ed25519_scalar_complement(scalar.toUInt8Array())
return result.toUByteArray()
}
actual fun addScalars(x: UByteArray, y: UByteArray): UByteArray {
val result = getSodium().crypto_core_ed25519_scalar_add(x.toUInt8Array(), y.toUInt8Array())
return result.toUByteArray()
}
actual fun subtractScalars(x: UByteArray, y: UByteArray): UByteArray {
val result = getSodium().crypto_core_ed25519_scalar_sub(x.toUInt8Array(), y.toUInt8Array())
return result.toUByteArray()
}
actual fun multiplyScalars(x: UByteArray, y: UByteArray): UByteArray {
val result = getSodium().crypto_core_ed25519_scalar_mul(x.toUInt8Array(), y.toUInt8Array())
return result.toUByteArray()
}
actual fun reduceScalar(scalar: UByteArray): UByteArray {
val result = getSodium().crypto_core_ed25519_scalar_reduce(scalar.toUInt8Array())
return result.toUByteArray()
}
actual fun scalarMultiplication(n: UByteArray, p: UByteArray): UByteArray {
val result = getSodium().crypto_scalarmult_ed25519(n.toUInt8Array(), p.toUInt8Array())
return result.toUByteArray()
}
actual fun scalarMultiplicationNoClamp(n: UByteArray, p: UByteArray): UByteArray {
val result = getSodium().crypto_scalarmult_ed25519_noclamp(n.toUInt8Array(), p.toUInt8Array())
return result.toUByteArray()
}
actual fun scalarMultiplicationBase(n: UByteArray): UByteArray {
val result = getSodium().crypto_scalarmult_ed25519_base(n.toUInt8Array())
return result.toUByteArray()
}
actual fun scalarMultiplicationBaseNoClamp(n: UByteArray): UByteArray {
val result = getSodium().crypto_scalarmult_ed25519_base_noclamp(n.toUInt8Array())
return result.toUByteArray()
}
}

View File

@ -1,7 +1,6 @@
package com.ionspin.kotlin.crypto.generichash package com.ionspin.kotlin.crypto.generichash
import com.ionspin.kotlin.crypto.getSodium import com.ionspin.kotlin.crypto.getSodium
import ext.libsodium.com.ionspin.kotlin.crypto.GenericHashStateInternalType
import ext.libsodium.com.ionspin.kotlin.crypto.toUByteArray import ext.libsodium.com.ionspin.kotlin.crypto.toUByteArray
import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array
import org.khronos.webgl.Uint8Array import org.khronos.webgl.Uint8Array
@ -12,8 +11,14 @@ import org.khronos.webgl.Uint8Array
* on 21-Aug-2020 * on 21-Aug-2020
*/ */
// TODO: посмотреть, как оно используется
external object GenericHashStateInternalType: JsAny
//Раз используется как жсЭни, то можно написать = ЖсЭни
//actual typealias GenericHashStateInternal = Any
actual typealias GenericHashStateInternal = GenericHashStateInternalType actual typealias GenericHashStateInternal = GenericHashStateInternalType
actual object GenericHash { actual object GenericHash {
actual fun genericHash( actual fun genericHash(
message: UByteArray, message: UByteArray,
@ -32,7 +37,7 @@ actual object GenericHash {
key: UByteArray? key: UByteArray?
): GenericHashState { ): GenericHashState {
val state = getSodium().crypto_generichash_init(key?.toUInt8Array() ?: Uint8Array(0), requestedHashLength) val state = getSodium().crypto_generichash_init(key?.toUInt8Array() ?: Uint8Array(0), requestedHashLength)
return GenericHashState(requestedHashLength, state) return GenericHashState(requestedHashLength, state as GenericHashStateInternal)
} }
actual fun genericHashUpdate( actual fun genericHashUpdate(

View File

@ -1,14 +1,19 @@
package com.ionspin.kotlin.crypto.hash package com.ionspin.kotlin.crypto.hash
import com.ionspin.kotlin.crypto.getSodium import com.ionspin.kotlin.crypto.getSodium
import ext.libsodium.com.ionspin.kotlin.crypto.Sha256StateType
import ext.libsodium.com.ionspin.kotlin.crypto.Sha512StateType
import ext.libsodium.com.ionspin.kotlin.crypto.toUByteArray import ext.libsodium.com.ionspin.kotlin.crypto.toUByteArray
import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array
// TODO: проверить, что эти штуки юзаются как жсЭни
//typealias Sha256State = JsAny
//typealias Sha512State = JsAny
//actual typealias Sha256State = Any
//actual typealias Sha512State = Any
external object Sha256StateType: JsAny
actual typealias Sha256State = Sha256StateType actual typealias Sha256State = Sha256StateType
actual typealias Sha512State = Sha512StateType actual typealias Sha512State = Sha256StateType
actual object Hash { actual object Hash {
@ -17,7 +22,7 @@ actual object Hash {
} }
actual fun sha256Init(): Sha256State { actual fun sha256Init(): Sha256State {
return getSodium().crypto_hash_sha256_init() return getSodium().crypto_hash_sha256_init() as Sha256State
} }
actual fun sha256Update(state: Sha256State, data: UByteArray) { actual fun sha256Update(state: Sha256State, data: UByteArray) {
@ -33,7 +38,7 @@ actual object Hash {
} }
actual fun sha512Init(): Sha512State { actual fun sha512Init(): Sha512State {
return getSodium().crypto_hash_sha512_init() return getSodium().crypto_hash_sha512_init() as Sha512State
} }
actual fun sha512Update(state: Sha512State, data: UByteArray) { actual fun sha512Update(state: Sha512State, data: UByteArray) {

View File

@ -1,94 +0,0 @@
package com.ionspin.kotlin.crypto.ristretto255
import com.ionspin.kotlin.crypto.getSodium
import ext.libsodium.com.ionspin.kotlin.crypto.toUByteArray
import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array
actual object Ristretto255LowLevel {
actual fun isValidPoint(encoded: UByteArray): Boolean =
getSodium().crypto_core_ristretto255_is_valid_point(encoded.toUInt8Array())
actual fun addPoints(p: UByteArray, q: UByteArray): UByteArray {
val result = getSodium().crypto_core_ristretto255_add(p.toUInt8Array(), q.toUInt8Array())
return result.toUByteArray()
}
actual fun subtractPoints(p: UByteArray, q: UByteArray): UByteArray {
val result = getSodium().crypto_core_ristretto255_sub(p.toUInt8Array(), q.toUInt8Array())
return result.toUByteArray()
}
actual fun pointFromHash(hash: UByteArray): UByteArray {
val result = getSodium().crypto_core_ristretto255_from_hash(hash.toUInt8Array())
return result.toUByteArray()
}
actual fun randomPoint(): UByteArray {
val result = getSodium().crypto_core_ristretto255_random()
return result.toUByteArray()
}
actual fun randomScalar(): UByteArray {
val result = getSodium().crypto_core_ristretto255_scalar_random()
return result.toUByteArray()
}
actual fun invertScalar(scalar: UByteArray): UByteArray {
val result = getSodium().crypto_core_ristretto255_scalar_invert(scalar.toUInt8Array())
return result.toUByteArray()
}
actual fun negateScalar(scalar: UByteArray): UByteArray {
val result = getSodium().crypto_core_ristretto255_scalar_negate(scalar.toUInt8Array())
return result.toUByteArray()
}
actual fun complementScalar(scalar: UByteArray): UByteArray {
val result = getSodium().crypto_core_ristretto255_scalar_complement(scalar.toUInt8Array())
return result.toUByteArray()
}
actual fun addScalars(x: UByteArray, y: UByteArray): UByteArray {
val result = getSodium().crypto_core_ristretto255_scalar_add(x.toUInt8Array(), y.toUInt8Array())
return result.toUByteArray()
}
actual fun subtractScalars(x: UByteArray, y: UByteArray): UByteArray {
val result = getSodium().crypto_core_ristretto255_scalar_sub(x.toUInt8Array(), y.toUInt8Array())
return result.toUByteArray()
}
actual fun multiplyScalars(x: UByteArray, y: UByteArray): UByteArray {
val result = getSodium().crypto_core_ristretto255_scalar_mul(x.toUInt8Array(), y.toUInt8Array())
return result.toUByteArray()
}
actual fun reduceScalar(scalar: UByteArray): UByteArray {
val result = getSodium().crypto_core_ristretto255_scalar_reduce(scalar.toUInt8Array())
return result.toUByteArray()
}
actual fun scalarMultiplication(n: UByteArray, p: UByteArray): UByteArray {
val result = getSodium().crypto_scalarmult_ristretto255(n.toUInt8Array(), p.toUInt8Array())
return result.toUByteArray()
}
actual fun scalarMultiplicationBase(n: UByteArray): UByteArray {
val result = getSodium().crypto_scalarmult_ristretto255_base(n.toUInt8Array())
return result.toUByteArray()
}
}

View File

@ -3,6 +3,7 @@ package com.ionspin.kotlin.crypto.secretbox
import com.ionspin.kotlin.crypto.getSodium import com.ionspin.kotlin.crypto.getSodium
import ext.libsodium.com.ionspin.kotlin.crypto.toUByteArray import ext.libsodium.com.ionspin.kotlin.crypto.toUByteArray
import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array
import org.khronos.webgl.Uint8Array
actual object SecretBox { actual object SecretBox {
actual fun easy(message: UByteArray, nonce: UByteArray, key: UByteArray): UByteArray { actual fun easy(message: UByteArray, nonce: UByteArray, key: UByteArray): UByteArray {
@ -24,7 +25,7 @@ actual object SecretBox {
nonce.toUInt8Array(), nonce.toUInt8Array(),
key.toUInt8Array() key.toUInt8Array()
) )
return decryptionResult.toUByteArray() return (decryptionResult as Uint8Array).toUByteArray()
} catch (error: Throwable) { } catch (error: Throwable) {
throw SecretBoxCorruptedOrTamperedDataExceptionOrInvalidKey() throw SecretBoxCorruptedOrTamperedDataExceptionOrInvalidKey()
} }
@ -59,7 +60,7 @@ actual object SecretBox {
nonce.toUInt8Array(), nonce.toUInt8Array(),
key.toUInt8Array() key.toUInt8Array()
) )
return decryptionResult.toUByteArray() return (decryptionResult as Uint8Array).toUByteArray()
} catch (error: Throwable) { } catch (error: Throwable) {
throw SecretBoxCorruptedOrTamperedDataExceptionOrInvalidKey() throw SecretBoxCorruptedOrTamperedDataExceptionOrInvalidKey()
} }

View File

@ -1,17 +1,20 @@
package com.ionspin.kotlin.crypto.secretstream package com.ionspin.kotlin.crypto.secretstream
import com.ionspin.kotlin.crypto.getSodium import com.ionspin.kotlin.crypto.getSodium
import ext.libsodium.com.ionspin.kotlin.crypto.SecretStreamStateType import ext.libsodium.com.ionspin.kotlin.crypto.CryptoSecretstreamXchacha20poly1305PullResult
import ext.libsodium.com.ionspin.kotlin.crypto.toUByteArray import ext.libsodium.com.ionspin.kotlin.crypto.toUByteArray
import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array
import org.khronos.webgl.Uint8Array
external object SecretStreamStateType: JsAny
actual typealias SecretStreamState = SecretStreamStateType actual typealias SecretStreamState = SecretStreamStateType
actual object SecretStream { actual object SecretStream {
actual fun xChaCha20Poly1305InitPush(key: UByteArray): SecretStreamStateAndHeader { actual fun xChaCha20Poly1305InitPush(key: UByteArray): SecretStreamStateAndHeader {
val state = getSodium().crypto_secretstream_xchacha20poly1305_init_push(key.toUInt8Array()) val state = getSodium().crypto_secretstream_xchacha20poly1305_init_push(key.toUInt8Array())
return SecretStreamStateAndHeader(state.state, state.header.toUByteArray()) return SecretStreamStateAndHeader(state.state as SecretStreamState, state.header.toUByteArray())
} }
actual fun xChaCha20Poly1305Push( actual fun xChaCha20Poly1305Push(
@ -30,7 +33,7 @@ actual object SecretStream {
header: UByteArray header: UByteArray
): SecretStreamStateAndHeader { ): SecretStreamStateAndHeader {
val state = getSodium().crypto_secretstream_xchacha20poly1305_init_pull(header.toUInt8Array(), key.toUInt8Array()) val state = getSodium().crypto_secretstream_xchacha20poly1305_init_pull(header.toUInt8Array(), key.toUInt8Array())
return SecretStreamStateAndHeader(state, header) return SecretStreamStateAndHeader(state as SecretStreamState, header)
} }
actual fun xChaCha20Poly1305Pull( actual fun xChaCha20Poly1305Pull(
@ -38,14 +41,14 @@ actual object SecretStream {
ciphertext: UByteArray, ciphertext: UByteArray,
associatedData: UByteArray associatedData: UByteArray
): DecryptedDataAndTag { ): DecryptedDataAndTag {
try { val dataAndTag = getSodium().crypto_secretstream_xchacha20poly1305_pull(
val dataAndTag = getSodium().crypto_secretstream_xchacha20poly1305_pull( state, ciphertext.toUInt8Array(), associatedData.toUInt8Array()
state, ciphertext.toUInt8Array(), associatedData.toUInt8Array() )
) if (dataAndTag as? JsBoolean == false.toJsBoolean()) {
return DecryptedDataAndTag(dataAndTag.message.toUByteArray(), dataAndTag.tag.toUByte())
} catch (error: Throwable) {
throw SecretStreamCorruptedOrTamperedDataException() throw SecretStreamCorruptedOrTamperedDataException()
} }
return DecryptedDataAndTag((dataAndTag as CryptoSecretstreamXchacha20poly1305PullResult).message.toUByteArray(), dataAndTag.tag.toUByte())
} }
actual fun xChaCha20Poly1305Keygen(): UByteArray { actual fun xChaCha20Poly1305Keygen(): UByteArray {

View File

@ -1,4 +1,5 @@
@file:JsModule("libsodium-sumo") @file:JsModule("libsodium-sumo")
//@file:JsNonModule
package ext.libsodium package ext.libsodium
import org.khronos.webgl.Uint8Array import org.khronos.webgl.Uint8Array
@ -12,7 +13,7 @@ import kotlin.js.Promise
*/ */
@JsName("ready") @JsName("ready")
external val _libsodiumPromise : Promise<JsAny?> external val _libsodiumPromise : Promise<JsAny>
@JsName("_sodium_init") @JsName("_sodium_init")
external fun sodium_init() : Int external fun sodium_init() : Int

View File

@ -1,10 +1,10 @@
package com.ionspin.kotlin.crypto.util package com.ionspin.kotlin.crypto.util
//import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
//actual fun runTest(block: suspend (scope : CoroutineScope) -> Unit) { actual fun runTest(block: suspend (scope : CoroutineScope) -> Unit) {
// kotlinx.coroutines.test.runTest { kotlinx.coroutines.test.runTest {
// block(this) block(this)
// } }
//} }

View File

@ -1,4 +0,0 @@
config.resolve.alias = {
"crypto": false,
// "path": false,
}

View File

@ -18,7 +18,7 @@
@file:Suppress("UnstableApiUsage") @file:Suppress("UnstableApiUsage")
import org.gradle.api.tasks.testing.logging.TestExceptionFormat import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.targets.native.tasks.KotlinNativeTest import org.jetbrains.kotlin.gradle.targets.native.tasks.KotlinNativeTest
import org.jetbrains.kotlin.gradle.tasks.FatFrameworkTask import org.jetbrains.kotlin.gradle.tasks.FatFrameworkTask
@ -81,6 +81,7 @@ kotlin {
binaries.executable() binaries.executable()
} }
@OptIn(ExperimentalWasmDsl::class) @OptIn(ExperimentalWasmDsl::class)
wasmJs { wasmJs {
browser { browser {
@ -94,7 +95,24 @@ kotlin {
} }
} }
binaries.executable() binaries.executable()
// browser {
// val rootDirPath = project.rootDir.path
// val projectDirPath = project.projectDir.path
// commonWebpackConfig {
// outputFileName = "composeApp.js"
// devServer = (devServer ?: KotlinWebpackConfig.DevServer()).apply {
//// static = (static ?: mutableListOf()).apply {
//// // Serve sources to debug inside browser
//// add(rootDirPath)
//// add(projectDirPath)
//// }
// }
// }
// }
// binaries.executable()
} }
linuxX64("linux") { linuxX64("linux") {
binaries { binaries {
executable { executable {
@ -195,8 +213,6 @@ kotlin {
dependencies { dependencies {
implementation(kotlin(Deps.Common.test)) implementation(kotlin(Deps.Common.test))
implementation(kotlin(Deps.Common.testAnnotation)) implementation(kotlin(Deps.Common.testAnnotation))
implementation(Deps.Common.coroutinesTest)
implementation(kotlin("test"))
} }
} }
@ -221,6 +237,24 @@ kotlin {
} }
} }
val wasmJsMain by getting {
dependencies {
// implementation(kotlin(Deps.wasmJs.stdLib))
implementation(npm(Deps.wasmJs.Npm.libsodiumWrappers.first, Deps.wasmJs.Npm.libsodiumWrappers.second))
}
}
val wasmJsTest by getting {
dependencies {
dependsOn(commonTest)
implementation(npm(Deps.wasmJs.Npm.libsodiumWrappers.first, Deps.wasmJs.Npm.libsodiumWrappers.second))
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.9.0")
implementation(kotlin("test"))
}
}
// val nativeMain by creating { // val nativeMain by creating {
// dependsOn(commonMain) // dependsOn(commonMain)
// dependencies { // dependencies {
@ -294,16 +328,7 @@ kotlin {
implementation(kotlin(Deps.Js.test)) implementation(kotlin(Deps.Js.test))
} }
} }
val wasmJsMain by getting {
dependencies {
implementation(npm(Deps.wasmJs.Npm.libsodiumWrappers.first, Deps.wasmJs.Npm.libsodiumWrappers.second))
}
}
val wasmJsTest by getting {
dependencies {
implementation(npm(Deps.wasmJs.Npm.libsodiumWrappers.first, Deps.wasmJs.Npm.libsodiumWrappers.second))
}
}
val linuxMain by getting { val linuxMain by getting {
dependsOn(nativeMain) dependsOn(nativeMain)
} }

View File

@ -1,15 +1,12 @@
import com.ionspin.kotlin.crypto.LibsodiumInitializer import com.ionspin.kotlin.crypto.LibsodiumInitializer
import com.ionspin.kotlin.crypto.getSodium
import com.ionspin.kotlin.crypto.hash.Hash
import com.ionspin.kotlin.crypto.util.encodeToUByteArray
import ext.libsodium.com.ionspin.kotlin.crypto.JsSodiumInterface
fun main() { fun main() {
LibsodiumInitializer.initializeWithCallback { LibsodiumInitializer.initializeWithCallback {
val hash = Hash.sha512("123".encodeToUByteArray()) // val hash = Hash.sha512("123".encodeToUByteArray())
println("Hash (SHA512) of 123: ${hash.toHexString()}") // println("Hash (SHA512) of 123: ${hash.toHexString()}")
} println("Hello")
}
} }