Added secretbox functions and constants
This commit is contained in:
1 parent
555b69f15c
commit
99b9ee5e9d
14 files changed
+464
-32
No files matched your search
+10
@@ -61,6 +61,16 @@ interface JsSodiumInterface {
|
||||
fun crypto_secretstream_xchacha20poly1305_keygen() : Uint8Array
|
||||
fun crypto_secretstream_xchacha20poly1305_rekey(state: dynamic)
|
||||
|
||||
// ---- SecretBox ----
|
||||
fun crypto_secretbox_detached(message: Uint8Array, nonce: Uint8Array, key: Uint8Array) : dynamic
|
||||
fun crypto_secretbox_easy(message: Uint8Array, nonce: Uint8Array, key: Uint8Array) : Uint8Array
|
||||
fun crypto_secretbox_keygen() : Uint8Array
|
||||
fun crypto_secretbox_open_detached(ciphertext : Uint8Array, tag : Uint8Array, nonce: Uint8Array, key: Uint8Array) : dynamic
|
||||
fun crypto_secretbox_open_easy(ciphertext : Uint8Array, nonce: Uint8Array, key: Uint8Array) : dynamic
|
||||
|
||||
|
||||
// ---- SecretBox End ----
|
||||
|
||||
//util
|
||||
fun memzero(array: Uint8Array)
|
||||
|
||||
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
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 {
|
||||
return getSodium().crypto_secretbox_easy(
|
||||
message.toUInt8Array(),
|
||||
nonce.toUInt8Array(),
|
||||
key.toUInt8Array()
|
||||
).toUByteArray()
|
||||
}
|
||||
|
||||
actual fun openEasy(
|
||||
ciphertext: UByteArray,
|
||||
nonce: UByteArray,
|
||||
key: UByteArray
|
||||
): UByteArray {
|
||||
try {
|
||||
val decryptionResult = getSodium().crypto_secretbox_open_easy(
|
||||
ciphertext.toUInt8Array(),
|
||||
nonce.toUInt8Array(),
|
||||
key.toUInt8Array()
|
||||
)
|
||||
return (decryptionResult as Uint8Array).toUByteArray()
|
||||
} catch (error: Throwable) {
|
||||
throw SecretBoxCorruptedOrTamperedDataExceptionOrInvalidKey()
|
||||
}
|
||||
}
|
||||
|
||||
actual fun detached(
|
||||
message: UByteArray,
|
||||
nonce: UByteArray,
|
||||
key: UByteArray
|
||||
): SecretBoxEncryptedDataAndTag {
|
||||
val result = getSodium().crypto_secretbox_detached(
|
||||
message.toUInt8Array(),
|
||||
nonce.toUInt8Array(),
|
||||
key.toUInt8Array()
|
||||
)
|
||||
return SecretBoxEncryptedDataAndTag(
|
||||
(result.cipher as Uint8Array).toUByteArray(),
|
||||
(result.mac as Uint8Array).toUByteArray()
|
||||
)
|
||||
}
|
||||
|
||||
actual fun openDetached(
|
||||
ciphertext: UByteArray,
|
||||
tag: UByteArray,
|
||||
nonce: UByteArray,
|
||||
key: UByteArray
|
||||
): UByteArray {
|
||||
try {
|
||||
val decryptionResult = getSodium().crypto_secretbox_open_detached(
|
||||
ciphertext.toUInt8Array(),
|
||||
tag.toUInt8Array(),
|
||||
nonce.toUInt8Array(),
|
||||
key.toUInt8Array()
|
||||
)
|
||||
return (decryptionResult as Uint8Array).toUByteArray()
|
||||
} catch (error: Throwable) {
|
||||
throw SecretBoxCorruptedOrTamperedDataExceptionOrInvalidKey()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
actual fun keygen(): UByteArray {
|
||||
return getSodium().crypto_secretbox_keygen().toUByteArray()
|
||||
}
|
||||
|
||||
}
|
||||
+3
@@ -40,6 +40,9 @@ actual object SecretStream {
|
||||
val dataAndTag = getSodium().crypto_secretstream_xchacha20poly1305_pull(
|
||||
state, ciphertext.toUInt8Array(), additionalData.toUInt8Array()
|
||||
)
|
||||
if (dataAndTag == false) {
|
||||
throw SecretStreamCorrupedOrTamperedDataException()
|
||||
}
|
||||
return DecryptedDataAndTag((dataAndTag.message as Uint8Array).toUByteArray(), dataAndTag.tag)
|
||||
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user