From 41b51e275e4aab45d46cbf9e8909d7db6c2c153a Mon Sep 17 00:00:00 2001 From: zarazaex69 Date: Mon, 20 Apr 2026 05:23:07 +0300 Subject: [PATCH] refactor(logger,crypto): standardize logging and clean up crypto --- internal/crypto/chacha.go | 29 ++++++++++++++---------- internal/logger/logger.go | 47 ++++++++++++++++++++++++++++++++++----- 2 files changed, 58 insertions(+), 18 deletions(-) diff --git a/internal/crypto/chacha.go b/internal/crypto/chacha.go index 8e7f268..f6ac390 100644 --- a/internal/crypto/chacha.go +++ b/internal/crypto/chacha.go @@ -11,15 +11,17 @@ import ( ) var ( - ErrInvalidKeySize = errors.New("invalid key size") //nolint:revive - ErrCiphertextTooShort = errors.New("ciphertext too short") //nolint:revive + ErrInvalidKeySize = errors.New("invalid key size") + ErrCiphertextTooShort = errors.New("ciphertext too short") ) -type Cipher struct { //nolint:revive +// Cipher provides AEAD encryption and decryption using ChaCha20-Poly1305. +type Cipher struct { aead cipher.AEAD } -func NewCipher(keyStr string) (*Cipher, error) { //nolint:revive +// NewCipher creates a new Cipher instance with the given 32-byte key. +func NewCipher(keyStr string) (*Cipher, error) { key := []byte(keyStr) if len(key) != chacha20poly1305.KeySize { return nil, ErrInvalidKeySize @@ -33,23 +35,26 @@ func NewCipher(keyStr string) (*Cipher, error) { //nolint:revive return &Cipher{aead: aead}, nil } -func (c *Cipher) Encrypt(plaintext []byte) ([]byte, error) { //nolint:revive +// Encrypt encrypts plaintext and prepends a random nonce. +func (c *Cipher) Encrypt(plaintext []byte) ([]byte, error) { nonce := make([]byte, c.aead.NonceSize()) if _, err := rand.Read(nonce); err != nil { - return nil, fmt.Errorf("failed to read nonce: %w", err) + return nil, fmt.Errorf("failed to generate nonce: %w", err) } - ciphertext := c.aead.Seal(nonce, nonce, plaintext, nil) - return ciphertext, nil + // Seal appends the ciphertext to the nonce + return c.aead.Seal(nonce, nonce, plaintext, nil), nil } -func (c *Cipher) Decrypt(ciphertext []byte) ([]byte, error) { //nolint:revive - if len(ciphertext) < c.aead.NonceSize() { +// Decrypt decrypts ciphertext that has a nonce prepended. +func (c *Cipher) Decrypt(ciphertext []byte) ([]byte, error) { + nonceSize := c.aead.NonceSize() + if len(ciphertext) < nonceSize { return nil, ErrCiphertextTooShort } - nonce := ciphertext[:c.aead.NonceSize()] - encrypted := ciphertext[c.aead.NonceSize():] + nonce := ciphertext[:nonceSize] + encrypted := ciphertext[nonceSize:] res, err := c.aead.Open(nil, nonce, encrypted, nil) if err != nil { diff --git a/internal/logger/logger.go b/internal/logger/logger.go index fbaa63f..6ec62bd 100644 --- a/internal/logger/logger.go +++ b/internal/logger/logger.go @@ -1,27 +1,62 @@ -package logger //nolint:revive +package logger import ( + "fmt" "log" "sync/atomic" ) -var verboseEnabled atomic.Bool //nolint:gochecknoglobals +var verboseEnabled atomic.Bool -func SetVerbose(enabled bool) { //nolint:revive +// SetVerbose enables or disables verbose/debug logging. +func SetVerbose(enabled bool) { verboseEnabled.Store(enabled) } -func IsVerbose() bool { //nolint:revive +// IsVerbose returns true if verbose logging is enabled. +func IsVerbose() bool { return verboseEnabled.Load() } -func Verbosef(format string, v ...interface{}) { //nolint:revive +// Info logs an informational message. +func Info(v ...any) { + log.Print("[INFO] ", fmt.Sprint(v...)) +} + +// Infof logs a formatted informational message. +func Infof(format string, v ...any) { + log.Printf("[INFO] "+format, v...) +} + +// Warn logs a warning message. +func Warn(v ...any) { + log.Print("[WARN] ", fmt.Sprint(v...)) +} + +// Warnf logs a formatted warning message. +func Warnf(format string, v ...any) { + log.Printf("[WARN] "+format, v...) +} + +// Error logs an error message. +func Error(v ...any) { + log.Print("[ERROR] ", fmt.Sprint(v...)) +} + +// Errorf logs a formatted error message. +func Errorf(format string, v ...any) { + log.Printf("[ERROR] "+format, v...) +} + +// Verbosef logs a formatted message if verbose logging is enabled. +func Verbosef(format string, v ...any) { if verboseEnabled.Load() { log.Printf("[VERBOSE] "+format, v...) } } -func Debugf(format string, v ...interface{}) { //nolint:revive +// Debugf logs a formatted message if verbose logging is enabled. +func Debugf(format string, v ...any) { if verboseEnabled.Load() { log.Printf("[DEBUG] "+format, v...) }