shared

package
v0.11.0-beta.1 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ClientVersion is the version of the MarChat client
	ClientVersion = "dev"

	// ServerVersion is the version of the MarChat server
	ServerVersion = "dev"

	// BuildTime is the time when the binary was built
	BuildTime = "unknown"

	// GitCommit is the git commit hash
	GitCommit = "unknown"
)

Version variables that can be set at build time using ldflags

Functions

func CompareVersions

func CompareVersions(a, b string) int

CompareVersions compares two semver-style version strings (e.g. "1.2.3"). Returns -1 if a < b, 0 if a == b, 1 if a > b. Non-numeric or "dev" versions are treated as 0.0.0 so they satisfy any constraint.

func DecryptMessage

func DecryptMessage(sessionKey *SessionKey, encrypted *EncryptedMessage) ([]byte, error)

DecryptMessage decrypts a message using ChaCha20-Poly1305

func GetKeyID

func GetKeyID(publicKey []byte) string

GetKeyID returns the key ID for a public key

func GetServerVersionInfo

func GetServerVersionInfo() string

GetServerVersionInfo returns a formatted server version string

func GetVersionInfo

func GetVersionInfo() string

GetVersionInfo returns a formatted version string

func ValidateKeyPair

func ValidateKeyPair(keypair *KeyPair) error

ValidateKeyPair validates a keypair

Types

type EncryptedMessage

type EncryptedMessage struct {
	Type        MessageType `json:"type"`
	Sender      string      `json:"sender"`
	CreatedAt   time.Time   `json:"created_at"`
	Content     string      `json:"content,omitempty"`      // Plaintext for system messages
	Encrypted   []byte      `json:"encrypted,omitempty"`    // Encrypted payload
	Nonce       []byte      `json:"nonce,omitempty"`        // For encrypted messages
	Recipient   string      `json:"recipient,omitempty"`    // For direct messages
	IsEncrypted bool        `json:"is_encrypted,omitempty"` // Flag for encrypted messages
	File        *FileMeta   `json:"file,omitempty"`         // For file messages
}

EncryptedMessage represents an E2E encrypted message

func EncryptMessage

func EncryptMessage(sessionKey *SessionKey, plaintext []byte) (*EncryptedMessage, error)

EncryptMessage encrypts a message using ChaCha20-Poly1305

func EncryptTextMessage

func EncryptTextMessage(sessionKey *SessionKey, sender, content string) (*EncryptedMessage, error)

EncryptTextMessage encrypts a text message

type FileMeta

type FileMeta struct {
	Filename string `json:"filename"`
	Size     int64  `json:"size"`
	Data     []byte `json:"data"` // raw bytes (base64-encoded in JSON)
}

type Handshake

type Handshake struct {
	Username string `json:"username"`
	Admin    bool   `json:"admin"`
	AdminKey string `json:"admin_key,omitempty"`
}

Handshake is sent by the client on WebSocket connect for authentication

type KeyPair

type KeyPair struct {
	PublicKey  []byte    `json:"public_key"`
	PrivateKey []byte    `json:"private_key"`
	CreatedAt  time.Time `json:"created_at"`
}

KeyPair represents a user's cryptographic identity

func GenerateKeyPair

func GenerateKeyPair() (*KeyPair, error)

GenerateKeyPair creates a new X25519 keypair

type Message

type Message struct {
	Sender    string      `json:"sender"`
	Content   string      `json:"content"`
	CreatedAt time.Time   `json:"created_at"`
	Type      MessageType `json:"type,omitempty"`
	Encrypted bool        `json:"encrypted,omitempty"`

	// MessageID uniquely identifies a message for edits, deletes, reactions, pins
	MessageID int64 `json:"message_id,omitempty"`

	// Recipient for direct messages (empty = broadcast to all)
	Recipient string `json:"recipient,omitempty"`

	// Edited indicates the message has been modified
	Edited bool `json:"edited,omitempty"`

	Channel string `json:"channel,omitempty"`

	// Reaction metadata
	Reaction *ReactionMeta `json:"reaction,omitempty"`

	// For file messages, Content is empty and File is set
	File *FileMeta `json:"file,omitempty"`
}

func DecryptTextMessage

func DecryptTextMessage(sessionKey *SessionKey, encrypted *EncryptedMessage) (*Message, error)

DecryptTextMessage decrypts a text message and returns the original Message

type MessageType

type MessageType string

MessageType distinguishes between text and file messages

const (
	TextMessage      MessageType = "text"
	FileMessageType  MessageType = "file"
	AdminCommandType MessageType = "admin_command"
	EditMessageType  MessageType = "edit"
	DeleteMessage    MessageType = "delete"
	TypingMessage    MessageType = "typing"
	ReactionMessage  MessageType = "reaction"
	DirectMessage    MessageType = "dm"
	SearchMessage    MessageType = "search"
	PinMessage       MessageType = "pin"
	ReadReceiptType  MessageType = "read_receipt"
	JoinChannelType  MessageType = "join_channel"
	LeaveChannelType MessageType = "leave_channel"
	ListChannelsType MessageType = "list_channels"
)

type PublicKeyInfo

type PublicKeyInfo struct {
	Username  string    `json:"username"`
	PublicKey []byte    `json:"public_key"`
	CreatedAt time.Time `json:"created_at"`
	KeyID     string    `json:"key_id"` // SHA256 hash of public key
}

PublicKeyInfo represents a user's public key for distribution

type ReactionMeta

type ReactionMeta struct {
	Emoji     string `json:"emoji"`
	TargetID  int64  `json:"target_id"`
	IsRemoval bool   `json:"is_removal,omitempty"`
}

ReactionMeta contains reaction data for a message

type SessionKey

type SessionKey struct {
	Key       []byte    `json:"key"`
	CreatedAt time.Time `json:"created_at"`
	KeyID     string    `json:"key_id"`
}

SessionKey represents a derived session key for a conversation

func DeriveSessionKey

func DeriveSessionKey(myPrivateKey, theirPublicKey []byte, conversationID string) (*SessionKey, error)

DeriveSessionKey creates a shared secret between two users

Jump to

Keyboard shortcuts

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