From db219a351fdf20ed7eaab45821cf1f29f90d81d6 Mon Sep 17 00:00:00 2001 From: kildishevps Date: Tue, 31 Dec 2024 05:36:26 +0300 Subject: [PATCH] Fixing all functions that didn't work and renaming types --- .../kotlin/crypto/JsSodiumInterface.kt | 75 +++++++++---------- .../ionspin/kotlin/crypto/JsSodiumLoader.kt | 1 - .../kotlin/com/ionspin/kotlin/crypto/Types.kt | 42 +++++------ ...thenticatedEncryptionWithAssociatedData.kt | 9 +-- .../kotlin/crypto/generichash/GenericHash.kt | 9 +-- .../com/ionspin/kotlin/crypto/hash/Hash.kt | 15 ++-- .../kotlin/crypto/secretbox/SecretBox.kt | 5 +- .../crypto/secretstream/SecretStream.kt | 21 +++--- sample/build.gradle.kts | 1 - 9 files changed, 76 insertions(+), 102 deletions(-) diff --git a/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumInterface.kt b/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumInterface.kt index ee79ddd..37af828 100644 --- a/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumInterface.kt +++ b/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumInterface.kt @@ -29,13 +29,13 @@ external object JsSodiumInterface: JsAny { // ---- 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 @@ -43,19 +43,18 @@ external object JsSodiumInterface: JsAny { // ---- 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 @@ -72,22 +71,22 @@ external object JsSodiumInterface: JsAny { @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 @@ -96,36 +95,36 @@ external object JsSodiumInterface: JsAny { //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 ---- @@ -139,7 +138,7 @@ external object JsSodiumInterface: JsAny { @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") @@ -147,7 +146,7 @@ external object JsSodiumInterface: JsAny { @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") @@ -159,7 +158,7 @@ external object JsSodiumInterface: JsAny { @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 @@ -191,9 +190,9 @@ external object JsSodiumInterface: JsAny { // ---- 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, @@ -208,7 +207,7 @@ external object JsSodiumInterface: JsAny { 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, @@ -246,19 +245,19 @@ external object JsSodiumInterface: JsAny { @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 @@ -327,13 +326,13 @@ external object JsSodiumInterface: JsAny { // ---- 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 ---- diff --git a/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumLoader.kt b/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumLoader.kt index a96f2d4..ec7306b 100644 --- a/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumLoader.kt +++ b/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumLoader.kt @@ -28,7 +28,6 @@ object JsSodiumLoader { _libsodiumPromise.then { sodium_init() sodiumLoaded = true - //Dynamic может быть Юнит, но Unit не может быть JsAny? continuation.resumeWith(Result.success(Unit)) null }.catch { e -> diff --git a/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/Types.kt b/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/Types.kt index 1ca6f46..753003a 100644 --- a/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/Types.kt +++ b/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/Types.kt @@ -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 diff --git a/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/aead/AuthenticatedEncryptionWithAssociatedData.kt b/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/aead/AuthenticatedEncryptionWithAssociatedData.kt index 0c5ee80..d4fbda6 100644 --- a/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/aead/AuthenticatedEncryptionWithAssociatedData.kt +++ b/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/aead/AuthenticatedEncryptionWithAssociatedData.kt @@ -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() ) } diff --git a/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/generichash/GenericHash.kt b/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/generichash/GenericHash.kt index 86a9f16..5fd9f0f 100644 --- a/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/generichash/GenericHash.kt +++ b/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/generichash/GenericHash.kt @@ -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( diff --git a/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/hash/Hash.kt b/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/hash/Hash.kt index ce3d7ab..9287f80 100644 --- a/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/hash/Hash.kt +++ b/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/hash/Hash.kt @@ -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) { diff --git a/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/secretbox/SecretBox.kt b/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/secretbox/SecretBox.kt index e61d14f..7b609b2 100644 --- a/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/secretbox/SecretBox.kt +++ b/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/secretbox/SecretBox.kt @@ -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() } diff --git a/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/secretstream/SecretStream.kt b/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/secretstream/SecretStream.kt index 652f3c8..db56a66 100644 --- a/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/secretstream/SecretStream.kt +++ b/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/secretstream/SecretStream.kt @@ -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,14 @@ 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()) { + 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 { diff --git a/sample/build.gradle.kts b/sample/build.gradle.kts index 1eefd25..92311bc 100644 --- a/sample/build.gradle.kts +++ b/sample/build.gradle.kts @@ -245,7 +245,6 @@ kotlin { } 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")