encrypt

fun encrypt(plaintext: String, conversationKey: ByteArray, nonce: ByteArray = generateNonce()): Nip44CipherText

Encrypts a plaintext message using NIP-44.

Encrypts the message with the following steps:

  1. Pads the plaintext using NIP-44 padding scheme

  2. Derives message keys from conversation key and nonce

  3. Encrypts padded plaintext with ChaCha20

  4. Computes HMAC-SHA256 over nonce and ciphertext

  5. Securely clears derived keys from memory

Return

Nip44CipherText containing version, nonce, ciphertext, and MAC

Parameters

plaintext

The message to encrypt (1-65535 bytes UTF-8)

conversationKey

The 32-byte conversation key

nonce

The 32-byte random nonce (auto-generated if not provided)

Throws

if plaintext length is invalid or keys are wrong size