refactor(logger,crypto): standardize logging and clean up crypto

This commit is contained in:
zarazaex69
2026-04-20 05:23:07 +03:00
parent f903bc15d2
commit 41b51e275e
2 changed files with 58 additions and 18 deletions

View File

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

View File

@@ -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...)
}