encryption

package
v0.8.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 18, 2025 License: Unlicense Imports: 18 Imported by: 0

Documentation

Overview

Package encryption contains the message encryption schemes defined in NIP-04 and NIP-44, used for encrypting the content of nostr messages.

Index

Constants

View Source
const (
	MinPlaintextSize = 0x0001 // 1b msg => padded to 32b
	MaxPlaintextSize = 0xffff // 65535 (64kb-1) => padded to 64kb
)

Variables

View Source
var WithCustomSalt = WithCustomNonce

Deprecated: use WithCustomNonce instead of WithCustomSalt, so the naming is less confusing

Functions

func CalcPadding

func CalcPadding(sLen int) (l int)

CalcPadding creates padding for the message payload that is precisely a power of two in order to reduce the chances of plaintext attack. This is plainly retarded because it could blow out the message size a lot when just a random few dozen bytes and a length prefix would achieve the same result.

func Decrypt

func Decrypt(b64ciphertextWrapped, conversationKey []byte) (
	plaintext []byte,
	err error,
)

Decrypt data that has been encoded using a provided symmetric conversation key using NIP-44 encryption (chacha20 cipher stream and sha256 HMAC).

func DecryptNip4

func DecryptNip4(content, key []byte) (msg []byte, err error)

DecryptNip4 decrypts a content string using the shared secret key. The inverse operation to message -> EncryptNip4(message, key).

func Encrypt

func Encrypt(
	plaintext, conversationKey []byte, applyOptions ...func(opts *Opts),
) (
	cipherString []byte, err error,
)

Encrypt data using a provided symmetric conversation key using NIP-44 encryption (chacha20 cipher stream and sha256 HMAC).

func EncryptNip4

func EncryptNip4(msg, key []byte) (ct []byte, err error)

EncryptNip4 encrypts message with key using aes-256-cbc. key should be the shared secret generated by ComputeSharedSecret.

Returns: base64(encrypted_bytes) + "?iv=" + base64(initialization_vector).

func GenerateConversationKeyFromHex added in v0.4.15

func GenerateConversationKeyFromHex(pkh, skh string) (ck []byte, err error)

GenerateConversationKeyFromHex performs an ECDH key generation hashed with the nip-44-v2 using hkdf.

func GenerateConversationKeyWithSigner added in v0.4.15

func GenerateConversationKeyWithSigner(sign signer.I, pk []byte) (
	ck []byte, err error,
)

func WithCustomNonce

func WithCustomNonce(salt []byte) func(opts *Opts)

WithCustomNonce enables using a custom nonce (salt) instead of using the system crypto/rand entropy source.

Types

type Opts

type Opts struct {
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL