Compare commits

..
24 Commits
Author SHA1 Message Date
sergeych e71ff35e14 Fix libsodium initialization on Kotlin JS
Build project and push to maven snapshot / Build-And-Push-Linux (push) Has been cancelled
Build project and push to maven snapshot / Build-And-Push-Mac (push) Has been cancelled
Build project and push to maven snapshot / Build-And-Push-Windows (push) Has been cancelled
2026-07-28 10:07:29 +04:00
sergeych eb9158d3e1 Publish bindings alongside crypto2
Build project and push to maven snapshot / Build-And-Push-Linux (push) Waiting to run
Build project and push to maven snapshot / Build-And-Push-Mac (push) Waiting to run
Build project and push to maven snapshot / Build-And-Push-Windows (push) Waiting to run
2026-07-28 02:48:31 +04:00
sergeych 12ff5ed3cb Publish Wasm bindings from Linux CI
Build project and push to maven snapshot / Build-And-Push-Linux (push) Waiting to run
Build project and push to maven snapshot / Build-And-Push-Mac (push) Waiting to run
Build project and push to maven snapshot / Build-And-Push-Windows (push) Waiting to run
2026-07-28 02:37:32 +04:00
sergeych e17f5a8dd5 Fix libsodium initialization on Kotlin Wasm
Build project and push to maven snapshot / Build-And-Push-Linux (push) Waiting to run
Build project and push to maven snapshot / Build-And-Push-Mac (push) Waiting to run
Build project and push to maven snapshot / Build-And-Push-Windows (push) Waiting to run
2026-07-28 02:36:16 +04:00
chmaria b4b08f2e17 Version updated to 0.9.6
Build project and push to maven snapshot / Build-And-Push-Linux (push) Has been cancelled
Build project and push to maven snapshot / Build-And-Push-Mac (push) Has been cancelled
Build project and push to maven snapshot / Build-And-Push-Windows (push) Has been cancelled
2025-03-24 00:52:49 +03:00
kildishevps aeda210bc5 Version that was successfully published
Build project and push to maven snapshot / Build-And-Push-Linux (push) Has been cancelled
Build project and push to maven snapshot / Build-And-Push-Mac (push) Has been cancelled
Build project and push to maven snapshot / Build-And-Push-Windows (push) Has been cancelled
2025-03-03 12:55:13 +03:00
kildishevps 09622f8226 Gradle configuration that publishes linuxArm64 publication from terminal
Build project and push to maven snapshot / Build-And-Push-Linux (push) Has been cancelled
Build project and push to maven snapshot / Build-And-Push-Mac (push) Has been cancelled
Build project and push to maven snapshot / Build-And-Push-Windows (push) Has been cancelled
2025-02-22 22:11:40 +03:00
kildishevps c39c9ec71e Adding comment in hardly understandable part
Build project and push to maven snapshot / Build-And-Push-Linux (push) Has been cancelled
Build project and push to maven snapshot / Build-And-Push-Mac (push) Has been cancelled
Build project and push to maven snapshot / Build-And-Push-Windows (push) Has been cancelled
2025-02-05 00:37:27 +03:00
kildishevps 2acf7218b4 Changing README
Build project and push to maven snapshot / Build-And-Push-Linux (push) Waiting to run
Build project and push to maven snapshot / Build-And-Push-Mac (push) Waiting to run
Build project and push to maven snapshot / Build-And-Push-Windows (push) Waiting to run
2025-02-04 21:40:56 +03:00
kildishevps f1b8488707 Changing publishing paths back to YoungBlood organization
Build project and push to maven snapshot / Build-And-Push-Linux (push) Has been cancelled
Build project and push to maven snapshot / Build-And-Push-Windows (push) Has been cancelled
Build project and push to maven snapshot / Build-And-Push-Mac (push) Has been cancelled
2025-02-02 19:33:01 +03:00
kildishevps 8a2bbb3e33 Fixig publication again
Build project and push to maven snapshot / Build-And-Push-Mac (push) Waiting to run
Build project and push to maven snapshot / Build-And-Push-Windows (push) Waiting to run
Build project and push to maven snapshot / Build-And-Push-Linux (push) Waiting to run
2025-02-02 18:58:02 +03:00
kildishevps c87a0cf24c Fixig publication
Build project and push to maven snapshot / Build-And-Push-Linux (push) Has been cancelled
Build project and push to maven snapshot / Build-And-Push-Mac (push) Has been cancelled
Build project and push to maven snapshot / Build-And-Push-Windows (push) Has been cancelled
2025-02-01 05:49:07 +03:00
PlaceboAddict c8020e9200 Small fix to previous update
Build project and push to maven snapshot / Build-And-Push-Linux (push) Waiting to run
Build project and push to maven snapshot / Build-And-Push-Mac (push) Waiting to run
Build project and push to maven snapshot / Build-And-Push-Windows (push) Waiting to run
2025-01-31 21:33:37 +03:00
PlaceboAddict 3334a967b1 GroupID and some other things changed from "iospin" to "sergeych"
Build project and push to maven snapshot / Build-And-Push-Linux (push) Waiting to run
Build project and push to maven snapshot / Build-And-Push-Mac (push) Waiting to run
Build project and push to maven snapshot / Build-And-Push-Windows (push) Waiting to run
2025-01-31 20:51:54 +03:00
Kildishev Petr 186db7478a Merge pull request #1 from sergeych/develop2
Build project and push to maven snapshot / Build-And-Push-Linux (push) Has been cancelled
Build project and push to maven snapshot / Build-And-Push-Mac (push) Has been cancelled
Build project and push to maven snapshot / Build-And-Push-Windows (push) Has been cancelled
Develop changes
2025-01-23 00:59:17 +03:00
kildishevps 90feb762db Rebase and adapting new functions to wasmJs 2025-01-23 00:35:54 +03:00
kildishevps bd64211bab Changed lib version to 0.9.4-SNAPSHOT 2025-01-22 23:29:50 +03:00
kildishevps c5e91fc8ee Moved all wasmJs to runningOnLinuxx in gradle, deleted unneded TODOS and dependencies 2025-01-22 23:29:47 +03:00
kildishevps f548a162e7 Attempt on fixing tests 2025-01-22 23:25:20 +03:00
kildishevps db219a351f Fixing all functions that didn't work and renaming types 2025-01-22 23:24:23 +03:00
kildishevps 083df8a73c Sample (and maybe something else too, idk) works now!! 2025-01-22 23:24:23 +03:00
kildishevps e2a079f0d2 Fixed libsodiumPromise.then TypeCastError error 2025-01-22 23:24:23 +03:00
kildishevps c6946c90b4 Fixed Throable cast error and changed gradle version in one more place 2025-01-22 23:24:23 +03:00
kildishevps 504e8db46b Changed gradle and kotlin versions and made LibsodiumInitializer.initializeWithCallback a bit prettier 2025-01-22 23:24:23 +03:00
33 changed files with 956 additions and 822 deletions

No files matched your search

+4 -12
View File
@@ -13,20 +13,12 @@ attack surfaces, bugs and other issues and you shouldn't use it in production un
#### Gradle
```kotlin
implementation("com.ionspin.kotlin:multiplatform-crypto-libsodium-bindings:0.9.2")
```
#### Snapshot builds
```kotlin
repositories {
maven("https://oss.sonatype.org/content/repositories/snapshots")
maven("https://gitea.sergeych.net/api/packages/YoungBlood/maven")
}
implementation("com.ionspin.kotlin:multiplatform-crypto-libsodium-bindings:0.9.3-SNAPSHOT")
implementation("net.sergeych:multiplatform-crypto-libsodium-bindings:0.9.6")
```
## Usage
Before using the wrapper you need to initialize the underlying libsodium library. You can use either a callback or coroutines approach
@@ -146,7 +138,7 @@ Currently supported native platforms (Apple Silicon is supported since 0.8.5) :
Android .so files come from running dist-build scripts in libsodium which you can find in the libsodium submodule
Java Linux Arm/X86_64 and Mac so and dylib are the same as produced by multiplatform builds, also based on the same submodule commit
Java Windows dll is from https://download.libsodium.org/libsodium/releases/libsodium-1.0.19-stable-msvc.zip
Javascript is from libsodium-wrappers-sumo npm package version 0.7.13 (https://github.com/jedisct1/libsodium.js)
Javascript and WebAssembly is from libsodium-wrappers-sumo npm package version 0.7.13 (https://github.com/jedisct1/libsodium.js)
### TODO:
- Improve documentation
@@ -156,7 +148,7 @@ Javascript is from libsodium-wrappers-sumo npm package version 0.7.13 (https://g
- Find a better way of fetching Konan dependencies than having a dummy project.
### Building
Clone the git, init the submodule and run `./gradlew build`. Note that current build settings are such that only linux builds `js` target.
Clone the git, init the submodule and run `./gradlew build`. Note that current build settings are such that only linux builds `js` and `wasmJs` targets.
#### Notes for Github runners:
- At the moment all runners need to have android sdk present even though not all are building Android build
+3 -5
View File
@@ -18,7 +18,7 @@
buildscript {
dependencies {
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.23")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:2.0.21")
classpath("com.android.tools.build:gradle:7.2.2")
classpath("org.jetbrains.dokka:dokka-gradle-plugin:1.9.20")
}
@@ -36,13 +36,11 @@ allprojects {
repositories {
mavenCentral()
google()
maven {
url = uri("https://oss.sonatype.org/content/repositories/snapshots")
}
maven("https://gitea.sergeych.net/api/packages/YoungBlood/maven")
}
}
group = "com.ionspin.kotlin"
group = ReleaseInfo.group
apply {
+1 -1
View File
@@ -28,7 +28,7 @@ repositories {
}
dependencies {
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.23")
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:2.0.21")
implementation("com.android.tools.build:gradle:7.2.2")
implementation ("org.jetbrains.dokka:dokka-gradle-plugin:1.9.20")
}
+6 -12
View File
@@ -16,7 +16,8 @@
object Versions {
val kotlinCoroutines = "1.8.0"
val kotlin = "1.9.23"
val kotlinCoroutinesTest = "1.9.0"
val kotlin = "2.0.21"
val kotlinSerialization = "1.6.3"
val kotlinSerializationPlugin = kotlin
val taskTreePlugin = "1.5"
@@ -35,8 +36,8 @@ object Versions {
}
object ReleaseInfo {
val group = "com.ionspin.kotlin"
val bindingsVersion = "0.9.3-SNAPSHOT"
val group = "net.sergeych"
val bindingsVersion = "0.9.8-SNAPSHOT"
}
object Deps {
@@ -46,13 +47,14 @@ object Deps {
val test = "test-common"
val testAnnotation = "test-annotations-common"
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 kotlinBigNum = "com.ionspin.kotlin:bignum:${Versions.kotlinBigNumVersion}"
val apiProject = ":multiplatform-crypto-api"
val libsodiumBindings = "com.ionspin.kotlin:multiplatform-crypto-libsodium-bindings:${Versions.sampleLibsodiumBindings}"
val libsodiumBindings = "net.sergeych:multiplatform-crypto-libsodium-bindings:${Versions.sampleLibsodiumBindings}"
val kodein = "org.kodein.di:kodein-di:${Versions.kodeinVersion}"
}
@@ -73,13 +75,6 @@ object Deps {
}
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 {
val libsodiumWrappers = Pair("libsodium-wrappers-sumo", "0.7.13")
@@ -147,4 +142,3 @@ object PluginsDeps {
val kotlinAndroid = "kotlin-android"
val kapt = "kotlin-kapt"
}
+412 -373
View File
File diff suppressed because it is too large. Load diff
+22 -29
View File
@@ -17,7 +17,7 @@
@file:Suppress("UnstableApiUsage")
import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.targets.native.tasks.KotlinNativeTest
plugins {
@@ -41,18 +41,6 @@ kotlin {
runningOnLinuxx86_64 {
jvm()
// TODO: wasm тут копирует не апишный, если поменялся тот, то поменять и тут
@OptIn(ExperimentalWasmDsl::class)
wasmJs {
browser {
testTask {
useKarma {
useChrome()
}
}
}
}
js(IR) {
browser {
testTask {
@@ -71,6 +59,16 @@ kotlin {
}
}
@OptIn(ExperimentalWasmDsl::class)
wasmJs {
browser {
testTask {
useKarma {
useChrome()
}
}
}
}
linuxX64("linux") {
binaries {
staticLib {
@@ -126,6 +124,8 @@ kotlin {
dependencies {
implementation(kotlin(Deps.Common.test))
implementation(kotlin(Deps.Common.testAnnotation))
implementation(Deps.Common.coroutinesTest)
implementation(kotlin("test"))
}
}
@@ -154,24 +154,17 @@ kotlin {
implementation(kotlin(Deps.Js.test))
}
}
}
// TODO: может сунуть обратно в ранинг онг линукс, как и то, что выше
val wasmJsMain by getting {
dependencies {
// implementation(kotlin(Deps.wasmJs.stdLib))
implementation(npm(Deps.wasmJs.Npm.libsodiumWrappers.first, Deps.wasmJs.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))
}
}
}
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 {
val tvosX64Main by getting {
dependsOn(commonMain)
@@ -18,10 +18,9 @@
@file:Suppress("UnstableApiUsage")
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.targets.js.dsl.ExperimentalWasmDsl
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
plugins {
@@ -34,22 +33,11 @@ plugins {
}
val sonatypeStaging = "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
val sonatypeSnapshots = "https://oss.sonatype.org/content/repositories/snapshots/"
val sonatypePassword: String? by project
val sonatypeUsername: String? by project
val sonatypePasswordEnv: String? = System.getenv()["SONATYPE_PASSWORD"]
val sonatypeUsernameEnv: String? = System.getenv()["SONATYPE_USERNAME"]
repositories {
mavenCentral()
mavenLocal()
maven {
url = uri("https://oss.sonatype.org/content/repositories/snapshots")
}
maven("https://gitea.sergeych.net/api/packages/YoungBlood/maven")
}
group = ReleaseInfo.group
@@ -90,20 +78,11 @@ kotlin {
}
jvm()
val projectRef = project
runningOnLinuxx86_64 {
println("Configuring Linux X86-64 targets")
wasmJs {
browser {
testTask {
useKarma {
useChromeHeadless()
}
}
}
}
js {
browser {
testTask {
@@ -121,6 +100,18 @@ kotlin {
}
}
@OptIn(ExperimentalWasmDsl::class)
wasmJs {
browser {
testTask {
useKarma {
useChrome()
}
}
}
}
linuxX64() {
compilations.getByName("main") {
val libsodiumCinterop by cinterops.creating {
@@ -137,14 +128,14 @@ kotlin {
}
}
if (ideaActive.not()) {
// if (ideaActive.not()) {
linuxArm64() {
binaries {
staticLib {
}
}
}
}
// }
}
@@ -304,10 +295,8 @@ kotlin {
implementation(kotlin(Deps.Common.test))
implementation(kotlin(Deps.Common.testAnnotation))
implementation(Deps.Common.coroutines)
// implementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.9.0")
implementation(Deps.Common.coroutinesTest)
implementation(kotlin("test"))
// implementation(kotlin("test-junit"))
}
}
@@ -333,35 +322,17 @@ 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
//linux, linuxArm32Hfp, linuxArm64
val linux64Bit = setOf(
"linuxX64"
)
val linuxArm64Bit = setOf(
if (ideaActive.not()) {
// if (ideaActive.not()) {
"linuxArm64"
} else {
""
}
// } else {
// ""
// }
)
//iosArm64, iosX64, macosX64, metadata, tvosArm64, tvosX64, watchosArm32, watchosArm64
@@ -400,22 +371,15 @@ kotlin {
defaultSourceSet.dependsOn(nativeMain)
}
if (linuxArm64Bit.contains(this@withType.name)) {
defaultSourceSet.dependsOn(
createWorkaroundNativeMainSourceSet(
this@withType.name,
nativeDependencies
)
)
defaultSourceSet.dependsOn(nativeMain)
compilations.getByName("main") {
val libsodiumCinterop by cinterops.creating {
defFile(projectRef.file("src/nativeInterop/cinterop/libsodium.def"))
compilerOpts.add("-I${projectRef.rootDir}/sodiumWrapper/static-arm64/include/")
}
kotlinOptions.freeCompilerArgs = listOf(
"-include-binary", "${projectRef.rootDir}/sodiumWrapper/static-arm64/lib/libsodium.a"
)
val libsodiumCinterop by cinterops.creating {
defFile(projectRef.file("src/nativeInterop/cinterop/libsodium.def"))
compilerOpts.add("-I${projectRef.rootDir}/sodiumWrapper/static-arm64/include/")
}
kotlinOptions.freeCompilerArgs = listOf(
"-include-binary", "${projectRef.rootDir}/sodiumWrapper/static-arm64/lib/libsodium.a"
)
}
if (macos64Bit.contains(this@withType.name)) {
@@ -602,6 +566,17 @@ kotlin {
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 {
isRunningInIdea {
kotlin.srcDir("src/nativeMain/kotlin")
@@ -740,13 +715,12 @@ tasks {
// }
// }
// TODO: ваще не жс тест, помогите
val wasmJsBrowserTest by getting(KotlinJsTest::class) {
testLogging {
events("PASSED", "FAILED", "SKIPPED")
showStandardStreams = true
}
}
// val wasmJsBrowserTest by getting(KotlinJsTest::class) {
// testLogging {
// events("PASSED", "FAILED", "SKIPPED")
// showStandardStreams = true
// }
// }
val jsBrowserTest by getting(KotlinJsTest::class) {
testLogging {
@@ -825,26 +799,21 @@ publishing {
}
}
val mavenToken by lazy {
File("${System.getProperty("user.home")}/.gitea_token").readText()
}
repositories {
maven {
url = uri(sonatypeStaging)
credentials {
username = sonatypeUsername ?: sonatypeUsernameEnv ?: ""
password = sonatypePassword ?: sonatypePasswordEnv ?: ""
credentials(HttpHeaderCredentials::class) {
name = "Authorization"
value = mavenToken
}
}
maven {
name = "snapshot"
url = uri(sonatypeSnapshots)
credentials {
username = sonatypeUsername ?: sonatypeUsernameEnv ?: ""
password = sonatypePassword ?: sonatypePasswordEnv ?: ""
url = uri("https://gitea.sergeych.net/api/packages/SergeychWorks/maven")
authentication {
create("Authorization", HttpHeaderAuthentication::class)
}
}
}
}
@@ -4,7 +4,6 @@ import com.ionspin.kotlin.crypto.generichash.GenericHash
import com.ionspin.kotlin.crypto.util.encodeToUByteArray
import com.ionspin.kotlin.crypto.util.testBlocking
import com.ionspin.kotlin.crypto.util.toHexString
import com.ionspin.kotlin.crypto.util.runTest
import kotlin.test.Test
import kotlin.test.assertTrue
@@ -17,7 +16,6 @@ class SmokeTest {
//TODO Browser ignores our testBlocking, node works fine though
@Test
fun testIfLibraryIsNotOnFire() {
throw Exception("aoaoao")
testBlocking {
LibsodiumInitializer.initialize()
val hashResult = GenericHash.genericHash("Hello".encodeToUByteArray(), 64)
@@ -36,6 +36,6 @@ fun testBlocking(block : suspend () -> Unit) {
block.startCoroutine(continuation)
}
expect fun runTest(block: suspend (scope : CoroutineScope) -> Unit)
fun runTest(block: suspend (scope : CoroutineScope) -> Unit) = kotlinx.coroutines.test.runTest { block(this) }
@@ -3,6 +3,7 @@ package ext.libsodium.com.ionspin.kotlin.crypto
import com.ionspin.kotlin.crypto.box.BoxKeyPair
import org.khronos.webgl.Uint8Array
import kotlin.js.Promise
/**
* Created by Ugljesa Jovanovic
@@ -13,6 +14,8 @@ import org.khronos.webgl.Uint8Array
@JsNonModule
external object JsSodiumInterface {
val ready: Promise<dynamic>
@JsName("crypto_generichash")
fun crypto_generichash(hashLength: Int, inputMessage: Uint8Array, key: Uint8Array): Uint8Array
@@ -2,12 +2,6 @@ package ext.libsodium.com.ionspin.kotlin.crypto
import com.ionspin.kotlin.crypto.getSodiumLoaded
import com.ionspin.kotlin.crypto.sodiumLoaded
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
/**
@@ -17,20 +11,9 @@ import kotlin.coroutines.suspendCoroutine
*/
object JsSodiumLoader {
class _EmitJsSodiumFunction {
init {
println(::crypto_generichash)
println(::crypto_hash_sha256)
println(::crypto_hash_sha512)
println(::crypto_hash_sha256_init)
}
}
suspend fun load() = suspendCoroutine { continuation ->
if (!getSodiumLoaded()) {
_libsodiumPromise.then<dynamic> {
sodium_init()
JsSodiumInterface.ready.then<dynamic> {
sodiumLoaded = true
continuation.resumeWith(Result.success(Unit))
}.catch { e ->
@@ -43,8 +26,7 @@ object JsSodiumLoader {
fun loadWithCallback(doneCallback: () -> (Unit)) {
if (!getSodiumLoaded()) {
_libsodiumPromise.then<dynamic> {
sodium_init()
JsSodiumInterface.ready.then<dynamic> {
sodiumLoaded = true
doneCallback.invoke()
}
@@ -1,31 +0,0 @@
@file:JsModule("libsodium-sumo")
@file:JsNonModule
package ext.libsodium
import org.khronos.webgl.Uint8Array
import kotlin.js.Promise
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 25-May-2020
*/
@JsName("ready")
external val _libsodiumPromise : Promise<dynamic>
@JsName("_sodium_init")
external fun sodium_init() : Int
external fun crypto_generichash(hashLength: Int, inputMessage: Uint8Array) : Uint8Array
external fun crypto_hash_sha256(message: Uint8Array) : Uint8Array
external fun crypto_hash_sha512(message: Uint8Array) : Uint8Array
external fun crypto_hash_sha256_init(): dynamic
@@ -1,7 +1,7 @@
package com.ionspin.kotlin.crypto.util
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.promise
actual fun runTest(block: suspend (scope : CoroutineScope) -> Unit): dynamic = GlobalScope.promise { block(this) }
//import kotlinx.coroutines.CoroutineScope
//import kotlinx.coroutines.GlobalScope
//import kotlinx.coroutines.promise
//
//actual fun runTest(block: suspend (scope : CoroutineScope) -> Unit): dynamic = GlobalScope.promise { block(this) }
@@ -1,7 +1,7 @@
package com.ionspin.kotlin.crypto.util
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.runBlocking
actual fun runTest(block: suspend (scope : CoroutineScope) -> Unit) = runBlocking { block(this) }
//import kotlinx.coroutines.CoroutineScope
//import kotlinx.coroutines.runBlocking
//
//actual fun runTest(block: suspend (scope : CoroutineScope) -> Unit) = runBlocking { block(this) }
@@ -1,7 +1,7 @@
package com.ionspin.kotlin.crypto.util
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.runBlocking
actual fun runTest(block: suspend (scope : CoroutineScope) -> Unit) = runBlocking { block(this) }
//import kotlinx.coroutines.CoroutineScope
//import kotlinx.coroutines.runBlocking
//
//actual fun runTest(block: suspend (scope : CoroutineScope) -> Unit) = runBlocking { block(this) }
@@ -1,8 +1,8 @@
package ext.libsodium.com.ionspin.kotlin.crypto
import com.ionspin.kotlin.crypto.box.BoxKeyPair
import org.khronos.webgl.Uint8Array
import kotlin.js.Promise
/**
* Created by Ugljesa Jovanovic
@@ -11,8 +11,14 @@ import org.khronos.webgl.Uint8Array
*/
@JsModule("libsodium-wrappers-sumo")
external object JsSodiumInterface {
external object JsSodium: JsAny {
@JsName("default")
val default : JsSodiumInterface
}
@JsModule("libsodium-wrappers-sumo")
external interface JsSodiumInterface: JsAny {
val ready: Promise<JsAny?>
@JsName("crypto_generichash")
fun crypto_generichash(hashLength: Int, inputMessage: Uint8Array, key: Uint8Array): Uint8Array
@@ -26,13 +32,13 @@ external object JsSodiumInterface {
// ---- Generic hash ---- // Updateable
@JsName("crypto_generichash_init")
fun crypto_generichash_init(key : Uint8Array, hashLength: Int) : JsAny
fun crypto_generichash_init(key : Uint8Array, hashLength: Int) : GenericHashStateInternalType
@JsName("crypto_generichash_update")
fun crypto_generichash_update(state: JsAny, inputMessage: Uint8Array)
fun crypto_generichash_update(state: GenericHashStateInternalType, inputMessage: Uint8Array)
@JsName("crypto_generichash_final")
fun crypto_generichash_final(state: JsAny, hashLength: Int) : Uint8Array
fun crypto_generichash_final(state: GenericHashStateInternalType, hashLength: Int) : Uint8Array
@JsName("crypto_generichash_keygen")
fun crypto_generichash_keygen() : Uint8Array
@@ -40,19 +46,18 @@ external object JsSodiumInterface {
// ---- Generic hash end ---- // Updateable
// ---- Blake2b ----
// I
@JsName("crypto_generichash_blake2b")
fun crypto_generichash_blake2b(hashLength: Int, inputMessage: Uint8Array, key: Uint8Array): Uint8Array
@JsName("crypto_generichash_blake2b_init")
fun crypto_generichash_blake2b_init(key : Uint8Array, hashLength: Int) : JsAny
fun crypto_generichash_blake2b_init(key : Uint8Array, hashLength: Int) : Blake2bInternalStateType
@JsName("crypto_generichash_blake2b_update")
fun crypto_generichash_blake2b_update(state: JsAny, inputMessage: Uint8Array)
// TODO: строка ниже просто висела без ничего, я ее закомментила
//crypto_secretstream_xchacha20poly1305_init_push
fun crypto_generichash_blake2b_update(state: Blake2bInternalStateType, inputMessage: Uint8Array)
@JsName("crypto_generichash_blake2b_final")
fun crypto_generichash_blake2b_final(state: JsAny, hashLength: Int) : Uint8Array
fun crypto_generichash_blake2b_final(state: Blake2bInternalStateType, hashLength: Int) : Uint8Array
@JsName("crypto_generichash_blake2b_keygen")
fun crypto_generichash_blake2b_keygen() : Uint8Array
@@ -69,22 +74,22 @@ external object JsSodiumInterface {
@JsName("crypto_hash_sha256_init")
fun crypto_hash_sha256_init() : JsAny
fun crypto_hash_sha256_init() : Sha256StateType
@JsName("crypto_hash_sha256_update")
fun crypto_hash_sha256_update(state: JsAny, message: Uint8Array)
fun crypto_hash_sha256_update(state: Sha256StateType, message: Uint8Array)
@JsName("crypto_hash_sha256_final")
fun crypto_hash_sha256_final(state: JsAny): Uint8Array
fun crypto_hash_sha256_final(state: Sha256StateType): Uint8Array
@JsName("crypto_hash_sha512_init")
fun crypto_hash_sha512_init() : JsAny
fun crypto_hash_sha512_init() : Sha512StateType
@JsName("crypto_hash_sha512_update")
fun crypto_hash_sha512_update(state: JsAny, message: Uint8Array)
fun crypto_hash_sha512_update(state: Sha512StateType, message: Uint8Array)
@JsName("crypto_hash_sha512_final")
fun crypto_hash_sha512_final(state: JsAny): Uint8Array
fun crypto_hash_sha512_final(state: Sha512StateType): Uint8Array
//XChaCha20Poly1305 - also in bindings
//fun crypto_aead_xchacha20poly1305_ietf_encrypt(message: Uint8Array, associatedData: Uint8Array, secretNonce: Uint8Array, nonce: Uint8Array, key: Uint8Array) : Uint8Array
@@ -93,36 +98,33 @@ external object JsSodiumInterface {
//XChaCha20Poly1305
//encrypt
@JsName("crypto_secretstream_xchacha20poly1305_init_push")
fun crypto_secretstream_xchacha20poly1305_init_push(key: Uint8Array) : CryptoSecretstreamXchacha20poly1305InitPushResult
fun crypto_secretstream_xchacha20poly1305_init_push(key: Uint8Array) : SecretStreamStateAndHeaderType
@JsName("crypto_secretstream_xchacha20poly1305_push")
// TODO: два варианта: \/
// 1. Меняем юбайт на байт и юинт на инт \/
// 2. Меняем юбайт на инт и юинт на лонг \/ и далее по списку
fun crypto_secretstream_xchacha20poly1305_push(state: JsAny, message: Uint8Array, associatedData: Uint8Array, tag: Byte) : Uint8Array
fun crypto_secretstream_xchacha20poly1305_push(state: SecretStreamStateType, message: Uint8Array, associatedData: Uint8Array, tag: Byte) : Uint8Array
//decrypt
@JsName("crypto_secretstream_xchacha20poly1305_init_pull")
fun crypto_secretstream_xchacha20poly1305_init_pull(header: Uint8Array, key: Uint8Array) : JsAny
fun crypto_secretstream_xchacha20poly1305_init_pull(header: Uint8Array, key: Uint8Array) : SecretStreamStateType
@JsName("crypto_secretstream_xchacha20poly1305_pull")
fun crypto_secretstream_xchacha20poly1305_pull(state: JsAny, ciphertext: Uint8Array, associatedData: Uint8Array) : JsAny
fun crypto_secretstream_xchacha20poly1305_pull(state: SecretStreamStateType, ciphertext: Uint8Array, associatedData: Uint8Array) : DecryptedDataAndTagType
//keygen and rekey
@JsName("crypto_secretstream_xchacha20poly1305_keygen")
fun crypto_secretstream_xchacha20poly1305_keygen() : Uint8Array
@JsName("crypto_secretstream_xchacha20poly1305_rekey")
fun crypto_secretstream_xchacha20poly1305_rekey(state: JsAny)
fun crypto_secretstream_xchacha20poly1305_rekey(state: SecretStreamStateType)
// ---- SecretBox ----
@JsName("crypto_secretbox_detached")
fun crypto_secretbox_detached(message: Uint8Array, nonce: Uint8Array, key: Uint8Array) : CryptoSecretboxDetachedResult
fun crypto_secretbox_detached(message: Uint8Array, nonce: Uint8Array, key: Uint8Array) : SecretBoxEncryptedType
@JsName("crypto_secretbox_easy")
fun crypto_secretbox_easy(message: Uint8Array, nonce: Uint8Array, key: Uint8Array) : Uint8Array
@JsName("crypto_secretbox_keygen")
fun crypto_secretbox_keygen() : Uint8Array
@JsName("crypto_secretbox_open_detached")
fun crypto_secretbox_open_detached(ciphertext : Uint8Array, tag : Uint8Array, nonce: Uint8Array, key: Uint8Array) : JsAny
fun crypto_secretbox_open_detached(ciphertext : Uint8Array, tag : Uint8Array, nonce: Uint8Array, key: Uint8Array) : Uint8Array
@JsName("crypto_secretbox_open_easy")
fun crypto_secretbox_open_easy(ciphertext : Uint8Array, nonce: Uint8Array, key: Uint8Array) : JsAny
fun crypto_secretbox_open_easy(ciphertext : Uint8Array, nonce: Uint8Array, key: Uint8Array) : Uint8Array
// ---- SecretBox End ----
@@ -136,7 +138,7 @@ external object JsSodiumInterface {
@JsName("crypto_aead_chacha20poly1305_encrypt")
fun crypto_aead_chacha20poly1305_encrypt(message: Uint8Array, associatedData: Uint8Array, nsec: Uint8Array?, npub: Uint8Array, key: Uint8Array) : Uint8Array
@JsName("crypto_aead_chacha20poly1305_encrypt_detached")
fun crypto_aead_chacha20poly1305_encrypt_detached(message: Uint8Array, associatedData: Uint8Array, nsec: Uint8Array?, npub: Uint8Array, key: Uint8Array) : Chacha20poly1305EncryptDetachedResult
fun crypto_aead_chacha20poly1305_encrypt_detached(message: Uint8Array, associatedData: Uint8Array, nsec: Uint8Array?, npub: Uint8Array, key: Uint8Array) : AeadEncryptedType
@JsName("crypto_aead_chacha20poly1305_ietf_decrypt")
fun crypto_aead_chacha20poly1305_ietf_decrypt(nsec : Uint8Array?, ciphertext: Uint8Array, associatedData: Uint8Array, npub: Uint8Array, key: Uint8Array) : Uint8Array
@JsName("crypto_aead_chacha20poly1305_ietf_decrypt_detached")
@@ -144,7 +146,7 @@ external object JsSodiumInterface {
@JsName("crypto_aead_chacha20poly1305_ietf_encrypt")
fun crypto_aead_chacha20poly1305_ietf_encrypt(message: Uint8Array, associatedData: Uint8Array, nsec: Uint8Array?, npub: Uint8Array, key: Uint8Array) : Uint8Array
@JsName("crypto_aead_chacha20poly1305_ietf_encrypt_detached")
fun crypto_aead_chacha20poly1305_ietf_encrypt_detached(message: Uint8Array, associatedData: Uint8Array, nsec: Uint8Array?, npub: Uint8Array, key: Uint8Array) : Chacha20poly1305EncryptDetachedResult
fun crypto_aead_chacha20poly1305_ietf_encrypt_detached(message: Uint8Array, associatedData: Uint8Array, nsec: Uint8Array?, npub: Uint8Array, key: Uint8Array) : AeadEncryptedType
@JsName("crypto_aead_chacha20poly1305_ietf_keygen")
fun crypto_aead_chacha20poly1305_ietf_keygen() : Uint8Array
@JsName("crypto_aead_chacha20poly1305_keygen")
@@ -156,7 +158,7 @@ external object JsSodiumInterface {
@JsName("crypto_aead_xchacha20poly1305_ietf_encrypt")
fun crypto_aead_xchacha20poly1305_ietf_encrypt(message: Uint8Array, associatedData: Uint8Array, nsec: Uint8Array?, npub: Uint8Array, key: Uint8Array) : Uint8Array
@JsName("crypto_aead_xchacha20poly1305_ietf_encrypt_detached")
fun crypto_aead_xchacha20poly1305_ietf_encrypt_detached(message: Uint8Array, associatedData: Uint8Array, nsec: Uint8Array?, npub: Uint8Array, key: Uint8Array) : Chacha20poly1305EncryptDetachedResult
fun crypto_aead_xchacha20poly1305_ietf_encrypt_detached(message: Uint8Array, associatedData: Uint8Array, nsec: Uint8Array?, npub: Uint8Array, key: Uint8Array) : AeadEncryptedType
@JsName("crypto_aead_xchacha20poly1305_ietf_keygen")
fun crypto_aead_xchacha20poly1305_ietf_keygen(): Uint8Array
@@ -188,9 +190,9 @@ external object JsSodiumInterface {
// ---- Box ----
@JsName("crypto_box_keypair")
fun crypto_box_keypair() : Keypair
fun crypto_box_keypair() : KeyExchangeKeyPairType
@JsName("crypto_box_seed_keypair")
fun crypto_box_seed_keypair(seed : Uint8Array) : Keypair
fun crypto_box_seed_keypair(seed : Uint8Array) : KeyExchangeKeyPairType
@JsName("crypto_box_easy")
fun crypto_box_easy(message: Uint8Array,
nonce: Uint8Array,
@@ -205,7 +207,7 @@ external object JsSodiumInterface {
fun crypto_box_detached(message: Uint8Array,
nonce: Uint8Array,
recipientsPublicKey: Uint8Array,
sendersSecretKey: Uint8Array) : CryptoBoxDetachedResult
sendersSecretKey: Uint8Array) : BoxEncryptedType
@JsName("crypto_box_open_detached")
fun crypto_box_open_detached(ciphertext: Uint8Array,
tag: Uint8Array,
@@ -243,19 +245,19 @@ external object JsSodiumInterface {
@JsName("crypto_sign_ed25519_sk_to_seed")
fun crypto_sign_ed25519_sk_to_seed(ed25519SecretKey: Uint8Array) : Uint8Array
@JsName("crypto_sign_final_create")
fun crypto_sign_final_create(state: JsAny, secretKey: Uint8Array) : Uint8Array
fun crypto_sign_final_create(state: SignatureStateType, secretKey: Uint8Array) : Uint8Array
@JsName("crypto_sign_final_verify")
fun crypto_sign_final_verify(state: JsAny, signature: Uint8Array, publicKey: Uint8Array) : Boolean
fun crypto_sign_final_verify(state: SignatureStateType, signature: Uint8Array, publicKey: Uint8Array) : Boolean
@JsName("crypto_sign_init")
fun crypto_sign_init() : SignatureStateType
@JsName("crypto_sign_keypair")
fun crypto_sign_keypair() : Keypair
fun crypto_sign_keypair() : KeyExchangeKeyPairType
@JsName("crypto_sign_open")
fun crypto_sign_open(signedMessage: Uint8Array, publicKey: Uint8Array) : Uint8Array
@JsName("crypto_sign_seed_keypair")
fun crypto_sign_seed_keypair(seed: Uint8Array) : Keypair
fun crypto_sign_seed_keypair(seed: Uint8Array) : KeyExchangeKeyPairType
@JsName("crypto_sign_update")
fun crypto_sign_update(state: JsAny, message: Uint8Array)
fun crypto_sign_update(state: SignatureStateType, message: Uint8Array)
@JsName("crypto_sign_verify_detached")
fun crypto_sign_verify_detached(signature: Uint8Array, message: Uint8Array, publicKey: Uint8Array) : Boolean
@@ -324,13 +326,13 @@ external object JsSodiumInterface {
// ---- Key exchange ----
@JsName("crypto_kx_client_session_keys")
fun crypto_kx_client_session_keys(clientPublicKey: Uint8Array, clientSecretKey: Uint8Array, serverPublicKey: Uint8Array) : CryptoKxClientSessionKeysResult
fun crypto_kx_client_session_keys(clientPublicKey: Uint8Array, clientSecretKey: Uint8Array, serverPublicKey: Uint8Array) : KeyExchangeSessionKeyPairType
@JsName("crypto_kx_keypair")
fun crypto_kx_keypair() : Keypair
fun crypto_kx_keypair() : KeyExchangeKeyPairType
@JsName("crypto_kx_seed_keypair")
fun crypto_kx_seed_keypair(seed: Uint8Array) : Keypair
fun crypto_kx_seed_keypair(seed: Uint8Array) : KeyExchangeKeyPairType
@JsName("crypto_kx_server_session_keys")
fun crypto_kx_server_session_keys(serverPublicKey: Uint8Array, serverSecretKey: Uint8Array, clientPublicKey: Uint8Array) : CryptoKxServerSessionKeysResult
fun crypto_kx_server_session_keys(serverPublicKey: Uint8Array, serverSecretKey: Uint8Array, clientPublicKey: Uint8Array) : KeyExchangeSessionKeyPairType
// ---- Key exchange end ----
@@ -367,5 +369,112 @@ external object JsSodiumInterface {
// ---- 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 ----
}
@@ -2,12 +2,6 @@ package ext.libsodium.com.ionspin.kotlin.crypto
import com.ionspin.kotlin.crypto.getSodiumLoaded
import com.ionspin.kotlin.crypto.sodiumLoaded
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
/**
@@ -17,31 +11,18 @@ import kotlin.coroutines.suspendCoroutine
*/
object JsSodiumLoader {
class _EmitJsSodiumFunction {
init {
println(::crypto_generichash)
println(::crypto_hash_sha256)
println(::crypto_hash_sha512)
println(::crypto_hash_sha256_init)
}
}
// TODO: попробовать сделать из этого suspend вместо continuation
suspend fun load(): Unit = suspendCoroutine { continuation ->
if (!getSodiumLoaded()) {
_libsodiumPromise.then<JsAny?> {
sodium_init()
JsSodium.default.ready.then<JsAny?> {
sodiumLoaded = true
//Dynamic может быть Юнит, но Unit не может быть JsAny?
continuation.resumeWith(Result.success(Unit))
null
}.catch { e ->
val throwable = e as? Throwable
val throwable = e.toThrowableOrNull()
if (throwable != null) {
continuation.resumeWith(Result.failure(throwable))
} else {
continuation.resumeWith(Result.failure(Exception("Error: $e")))
continuation.resumeWith(Result.failure(Throwable("Unknown error", throwable)))
}
null
}
@@ -50,12 +31,12 @@ object JsSodiumLoader {
}
}
fun loadWithCallback(doneCallback: () -> (JsAny)) {
fun loadWithCallback(doneCallback: () -> (Unit)) {
if (!getSodiumLoaded()) {
_libsodiumPromise.then<JsAny> {
sodium_init()
JsSodium.default.ready.then<JsAny?> {
sodiumLoaded = true
doneCallback.invoke()
null
}
} else {
doneCallback.invoke()
@@ -1,11 +1,12 @@
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.JsSodiumLoader
var sodiumLoaded: Boolean = false
fun getSodium() : JsSodiumInterface = JsSodiumInterface
fun getSodium() : JsSodiumInterface = JsSodium.default
fun getSodiumLoaded() : Boolean = sodiumLoaded
@@ -26,8 +27,6 @@ actual object LibsodiumInitializer {
JsSodiumLoader.loadWithCallback {
isPlatformInitialized = true
done()
// TODO: there's no return needed!!!
"null".toJsString()
}
}
@@ -1,58 +1,50 @@
package ext.libsodium.com.ionspin.kotlin.crypto
import org.khronos.webgl.Uint8Array
//TODO: может быть стоит поудалять ненужное
external object Sha256StateType: JsAny
external object Sha512StateType: JsAny
external object SignatureStateType: JsAny
external object Chacha20poly1305EncryptDetachedResult : JsAny {
external object AeadEncryptedType : JsAny {
val ciphertext: Uint8Array
var mac: Uint8Array
}
external object CryptoBoxDetachedResult : JsAny {
external object BoxEncryptedType : JsAny {
val ciphertext: Uint8Array
var mac: Uint8Array
}
//external object CryptoBoxKeypairResult: JsAny {
// val publicKey: Uint8Array
// val privateKey: Uint8Array
//}
external object CryptoKxClientSessionKeysResult: JsAny {
external object KeyExchangeSessionKeyPairType: JsAny {
val sharedRx: Uint8Array
val sharedTx: Uint8Array
}
//external object CryptoKxKeypairResult: JsAny {
// val publicKey: Uint8Array
// val privateKey: Uint8Array
//}
external object Keypair: JsAny {
external object KeyExchangeKeyPairType: JsAny {
val publicKey: Uint8Array
val privateKey: Uint8Array
}
//
external object CryptoKxServerSessionKeysResult: JsAny {
val sharedRx: Uint8Array
val sharedTx: Uint8Array
}
external object CryptoSecretboxDetachedResult: JsAny {
external object SecretBoxEncryptedType: JsAny {
val cipher: Uint8Array
val mac: Uint8Array
}
external object CryptoSecretstreamXchacha20poly1305InitPushResult: JsAny {
val state: Uint8Array
external object SecretStreamStateType: JsAny
external object SecretStreamStateAndHeaderType: JsAny {
val state: SecretStreamStateType
val header: Uint8Array
}
external object CryptoSecretstreamXchacha20poly1305PullResult: JsAny {
external object DecryptedDataAndTagType: JsAny {
val message: Uint8Array
val tag: Byte
}
external object GenericHashStateInternalType: JsAny
external object Blake2bInternalStateType: JsAny
@@ -3,7 +3,6 @@ package com.ionspin.kotlin.crypto.aead
import com.ionspin.kotlin.crypto.getSodium
import ext.libsodium.com.ionspin.kotlin.crypto.toUByteArray
import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array
import org.khronos.webgl.Uint8Array
actual object AuthenticatedEncryptionWithAssociatedData {
@@ -131,8 +130,8 @@ actual object AuthenticatedEncryptionWithAssociatedData {
key.toUInt8Array(),
)
return AeadEncryptedDataAndTag(
(result.ciphertext as Uint8Array).toUByteArray(),
(result.mac as Uint8Array).toUByteArray()
result.ciphertext.toUByteArray(),
result.mac.toUByteArray()
)
}
@@ -205,8 +204,8 @@ actual object AuthenticatedEncryptionWithAssociatedData {
key.toUInt8Array(),
)
return AeadEncryptedDataAndTag(
(result.ciphertext as Uint8Array).toUByteArray(),
(result.mac as Uint8Array).toUByteArray()
result.ciphertext.toUByteArray(),
result.mac.toUByteArray()
)
}
@@ -0,0 +1,106 @@
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()
}
}
@@ -1,6 +1,7 @@
package com.ionspin.kotlin.crypto.generichash
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.toUInt8Array
import org.khronos.webgl.Uint8Array
@@ -11,14 +12,8 @@ import org.khronos.webgl.Uint8Array
* on 21-Aug-2020
*/
// TODO: посмотреть, как оно используется
external object GenericHashStateInternalType: JsAny
//Раз используется как жсЭни, то можно написать = ЖсЭни
//actual typealias GenericHashStateInternal = Any
actual typealias GenericHashStateInternal = GenericHashStateInternalType
actual object GenericHash {
actual fun genericHash(
message: UByteArray,
@@ -37,7 +32,7 @@ actual object GenericHash {
key: UByteArray?
): GenericHashState {
val state = getSodium().crypto_generichash_init(key?.toUInt8Array() ?: Uint8Array(0), requestedHashLength)
return GenericHashState(requestedHashLength, state as GenericHashStateInternal)
return GenericHashState(requestedHashLength, state)
}
actual fun genericHashUpdate(
@@ -1,19 +1,14 @@
package com.ionspin.kotlin.crypto.hash
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.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 Sha512State = Sha256StateType
actual typealias Sha512State = Sha512StateType
actual object Hash {
@@ -22,7 +17,7 @@ actual object Hash {
}
actual fun sha256Init(): Sha256State {
return getSodium().crypto_hash_sha256_init() as Sha256State
return getSodium().crypto_hash_sha256_init()
}
actual fun sha256Update(state: Sha256State, data: UByteArray) {
@@ -38,7 +33,7 @@ actual object Hash {
}
actual fun sha512Init(): Sha512State {
return getSodium().crypto_hash_sha512_init() as Sha512State
return getSodium().crypto_hash_sha512_init()
}
actual fun sha512Update(state: Sha512State, data: UByteArray) {
@@ -0,0 +1,94 @@
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()
}
}
@@ -3,7 +3,6 @@ package com.ionspin.kotlin.crypto.secretbox
import com.ionspin.kotlin.crypto.getSodium
import ext.libsodium.com.ionspin.kotlin.crypto.toUByteArray
import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array
import org.khronos.webgl.Uint8Array
actual object SecretBox {
actual fun easy(message: UByteArray, nonce: UByteArray, key: UByteArray): UByteArray {
@@ -25,7 +24,7 @@ actual object SecretBox {
nonce.toUInt8Array(),
key.toUInt8Array()
)
return (decryptionResult as Uint8Array).toUByteArray()
return decryptionResult.toUByteArray()
} catch (error: Throwable) {
throw SecretBoxCorruptedOrTamperedDataExceptionOrInvalidKey()
}
@@ -60,7 +59,7 @@ actual object SecretBox {
nonce.toUInt8Array(),
key.toUInt8Array()
)
return (decryptionResult as Uint8Array).toUByteArray()
return decryptionResult.toUByteArray()
} catch (error: Throwable) {
throw SecretBoxCorruptedOrTamperedDataExceptionOrInvalidKey()
}
@@ -1,20 +1,17 @@
package com.ionspin.kotlin.crypto.secretstream
import com.ionspin.kotlin.crypto.getSodium
import ext.libsodium.com.ionspin.kotlin.crypto.CryptoSecretstreamXchacha20poly1305PullResult
import ext.libsodium.com.ionspin.kotlin.crypto.SecretStreamStateType
import ext.libsodium.com.ionspin.kotlin.crypto.toUByteArray
import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array
import org.khronos.webgl.Uint8Array
external object SecretStreamStateType: JsAny
actual typealias SecretStreamState = SecretStreamStateType
actual object SecretStream {
actual fun xChaCha20Poly1305InitPush(key: UByteArray): SecretStreamStateAndHeader {
val state = getSodium().crypto_secretstream_xchacha20poly1305_init_push(key.toUInt8Array())
return SecretStreamStateAndHeader(state.state as SecretStreamState, state.header.toUByteArray())
return SecretStreamStateAndHeader(state.state, state.header.toUByteArray())
}
actual fun xChaCha20Poly1305Push(
@@ -33,7 +30,7 @@ actual object SecretStream {
header: UByteArray
): SecretStreamStateAndHeader {
val state = getSodium().crypto_secretstream_xchacha20poly1305_init_pull(header.toUInt8Array(), key.toUInt8Array())
return SecretStreamStateAndHeader(state as SecretStreamState, header)
return SecretStreamStateAndHeader(state, header)
}
actual fun xChaCha20Poly1305Pull(
@@ -41,14 +38,16 @@ actual object SecretStream {
ciphertext: UByteArray,
associatedData: UByteArray
): DecryptedDataAndTag {
val dataAndTag = getSodium().crypto_secretstream_xchacha20poly1305_pull(
state, ciphertext.toUInt8Array(), associatedData.toUInt8Array()
)
if (dataAndTag as? JsBoolean == false.toJsBoolean()) {
// crypto_secretstream_xchacha20poly1305_pull JavaScript realization returns either 0
// or DecryptedDataAndTagType object, therefore if 0 is returned an identifying exception is thrown
try {
val dataAndTag = getSodium().crypto_secretstream_xchacha20poly1305_pull(
state, ciphertext.toUInt8Array(), associatedData.toUInt8Array()
)
return DecryptedDataAndTag(dataAndTag.message.toUByteArray(), dataAndTag.tag.toUByte())
} catch (error: Throwable) {
throw SecretStreamCorruptedOrTamperedDataException()
}
return DecryptedDataAndTag((dataAndTag as CryptoSecretstreamXchacha20poly1305PullResult).message.toUByteArray(), dataAndTag.tag.toUByte())
}
actual fun xChaCha20Poly1305Keygen(): UByteArray {
@@ -1,31 +0,0 @@
@file:JsModule("libsodium-sumo")
//@file:JsNonModule
package ext.libsodium
import org.khronos.webgl.Uint8Array
import kotlin.js.Promise
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 25-May-2020
*/
@JsName("ready")
external val _libsodiumPromise : Promise<JsAny>
@JsName("_sodium_init")
external fun sodium_init() : Int
external fun crypto_generichash(hashLength: Int, inputMessage: Uint8Array) : Uint8Array
external fun crypto_hash_sha256(message: Uint8Array) : Uint8Array
external fun crypto_hash_sha512(message: Uint8Array) : Uint8Array
external fun crypto_hash_sha256_init(): JsAny
@@ -1,11 +0,0 @@
//package com.ionspin.kotlin.crypto.debug
import kotlin.test.Test
import kotlin.test.assertEquals
class TestTest {
@Test
fun wasmSymbolsTest() {
assertEquals(42, 42)
}
}
@@ -1,11 +0,0 @@
package com.ionspin.kotlin.crypto.debug
import kotlin.test.Test
import kotlin.test.assertEquals
class WasmSymbolsTest {
@Test
fun wasmSymbolsTest() {
assertEquals(42, 42)
}
}
@@ -1,10 +1,10 @@
package com.ionspin.kotlin.crypto.util
import kotlinx.coroutines.CoroutineScope
//import kotlinx.coroutines.CoroutineScope
actual fun runTest(block: suspend (scope : CoroutineScope) -> Unit) {
kotlinx.coroutines.test.runTest {
block(this)
}
}
//actual fun runTest(block: suspend (scope : CoroutineScope) -> Unit) {
// kotlinx.coroutines.test.runTest {
// block(this)
// }
//}
@@ -0,0 +1,4 @@
config.resolve.alias = {
"crypto": false,
// "path": false,
}
+15 -40
View File
@@ -18,7 +18,7 @@
@file:Suppress("UnstableApiUsage")
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.targets.native.tasks.KotlinNativeTest
import org.jetbrains.kotlin.gradle.tasks.FatFrameworkTask
@@ -39,10 +39,10 @@ val sonatypeSnapshots = "https://oss.sonatype.org/content/repositories/snapshots
repositories {
mavenCentral()
mavenLocal()
maven("https://oss.sonatype.org/content/repositories/snapshots/")
maven("https://gitea.sergeych.net/api/packages/YoungBlood/maven")
}
group = "com.ionspin.kotlin"
group = "net.sergeych"
version = "0.1.0-SNAPSHOT"
val ideaActive = System.getProperty("idea.active") == "true"
@@ -81,7 +81,6 @@ kotlin {
binaries.executable()
}
@OptIn(ExperimentalWasmDsl::class)
wasmJs {
browser {
@@ -95,24 +94,7 @@ kotlin {
}
}
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") {
binaries {
executable {
@@ -213,6 +195,8 @@ kotlin {
dependencies {
implementation(kotlin(Deps.Common.test))
implementation(kotlin(Deps.Common.testAnnotation))
implementation(Deps.Common.coroutinesTest)
implementation(kotlin("test"))
}
}
@@ -237,24 +221,6 @@ 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 {
// dependsOn(commonMain)
// dependencies {
@@ -328,7 +294,16 @@ kotlin {
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 {
dependsOn(nativeMain)
}
@@ -1,12 +1,15 @@
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() {
LibsodiumInitializer.initializeWithCallback {
// val hash = Hash.sha512("123".encodeToUByteArray())
// println("Hash (SHA512) of 123: ${hash.toHexString()}")
println("Hello")
}
LibsodiumInitializer.initializeWithCallback {
val hash = Hash.sha512("123".encodeToUByteArray())
println("Hash (SHA512) of 123: ${hash.toHexString()}")
}
}