shared

package
v1.0.0-echo Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

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 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 Admin key is only sent if admin is true Username is always sent (case-insensitive match on server)

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"`
	// 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 (add more types as needed)

const (
	TextMessage     MessageType = "text"
	FileMessageType MessageType = "file"
)

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 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