Nip44

object Nip44

NIP-44 encrypted direct messages implementation.

Provides versioned, authenticated encryption for Nostr direct messages using ChaCha20-Poly1305-like construction with HKDF key derivation and HMAC-SHA256 authentication.

Usage

// Derive conversation key from ECDH shared secret
val sharedSecret = secKey1.sharedSecretWith(pubKey2)
val conversationKey = Nip44.getConversationKey(sharedSecret)

// Encrypt a message
val encrypted = Nip44.encrypt("Hello, Nostr!", conversationKey)
val base64Payload = encrypted.toBase64()

// Decrypt a message
val received = Nip44CipherText.parse(base64Payload)
val plaintext = Nip44.decrypt(received, conversationKey)

See also

Types

Link copied to clipboard
data class MessageKeys(val chachaKey: ByteArray, val chachaNonce: ByteArray, val hmacKey: ByteArray)

Container for per-message cryptographic keys.

Functions

Link copied to clipboard
fun decrypt(payload: Nip44CipherText, conversationKey: ByteArray): String

Decrypts a NIP-44 encrypted message.

Link copied to clipboard
fun encrypt(plaintext: String, conversationKey: ByteArray, nonce: ByteArray = generateNonce()): Nip44CipherText

Encrypts a plaintext message using NIP-44.

Link copied to clipboard

Generates a cryptographically secure random nonce.

Link copied to clipboard

Derives a conversation key from an ECDH shared secret.

Link copied to clipboard
fun getMessageKeys(conversationKey: ByteArray, nonce: ByteArray): Nip44.MessageKeys

Derives per-message encryption keys from a conversation key and nonce.