crypto / org.apache.tuweni.crypto.sodium / XChaCha20Poly1305

XChaCha20Poly1305

class XChaCha20Poly1305 (source)

Authenticated Encryption with Additional Data using XChaCha20-Poly1305.

The XChaCha20-Poly1305 construction can safely encrypt a practically unlimited number of messages with the same key, without any practical limit to the size of a message (up to ~ 2^64 bytes).

As an alternative to counters, its large nonce size (192-bit) allows random nonces to be safely used.

For this reason, and if interoperability with other libraries is not a concern, this is the recommended AEAD construction.

This class depends upon the JNR-FFI library being available on the classpath, along with its dependencies. See https://github.com/jnr/jnr-ffi. JNR-FFI can be included using the gradle dependency 'com.github.jnr:jnr-ffi'.

Types

Key

class Key : Destroyable

A XChaCha20-Poly1305 key.

Nonce

class Nonce

A XChaCha20-Poly1305 nonce.

Functions

decrypt

static fun decrypt(cipherText: Bytes, key: Key, nonce: Nonce): Bytes?
static fun decrypt(cipherText: ByteArray, key: Key, nonce: Nonce): ByteArray?
static fun decrypt(cipherText: Bytes, data: Bytes, key: Key, nonce: Nonce): Bytes?
static fun decrypt(cipherText: ByteArray, data: ByteArray, key: Key, nonce: Nonce): ByteArray?

Decrypt a message using a given key.

decryptDetached

static fun decryptDetached(cipherText: Bytes, mac: Bytes, key: Key, nonce: Nonce): Bytes
static fun decryptDetached(cipherText: ByteArray, mac: ByteArray, key: Key, nonce: Nonce): ByteArray?
static fun decryptDetached(cipherText: Bytes, mac: Bytes, data: Bytes, key: Key, nonce: Nonce): Bytes?
static fun decryptDetached(cipherText: ByteArray, mac: ByteArray, data: ByteArray, key: Key, nonce: Nonce): ByteArray?

Decrypt a message using a given key and a detached message authentication code.

encrypt

static fun encrypt(message: Bytes, key: Key, nonce: Nonce): Bytes
static fun encrypt(message: ByteArray, key: Key, nonce: Nonce): ByteArray
static fun encrypt(message: Bytes, data: Bytes, key: Key, nonce: Nonce): Bytes
static fun encrypt(message: ByteArray, data: ByteArray, key: Key, nonce: Nonce): ByteArray

Encrypt a message for a given key.

encryptDetached

static fun encryptDetached(message: Bytes, key: Key, nonce: Nonce): DetachedEncryptionResult
static fun encryptDetached(message: ByteArray, key: Key, nonce: Nonce): DetachedEncryptionResult
static fun encryptDetached(message: Bytes, data: Bytes, key: Key, nonce: Nonce): DetachedEncryptionResult
static fun encryptDetached(message: ByteArray, data: ByteArray, key: Key, nonce: Nonce): DetachedEncryptionResult

Encrypt a message for a given key, generating a detached message authentication code.

isAvailable

static fun isAvailable(): Boolean

Check if Sodium and the XChaCha20Poly1305 algorithm is available.

XChaCha20Poly1305 is supported in sodium native library version >= 10.0.12.

isSecretStreamAvailable

static fun isSecretStreamAvailable(): Boolean

Check if Sodium and the XChaCha20Poly1305 secret stream algorithm is available.

XChaCha20Poly1305 secret stream is supported in sodium native library version >= 10.0.14.

openDecryptionStream

static fun openDecryptionStream(key: Key, header: ByteArray): SecretDecryptionStream

Open an decryption stream.

openEncryptionStream

static fun openEncryptionStream(key: Key): SecretEncryptionStream

Open an encryption stream.