Перевел на multi-tenant

Добавил поставщиков
Накладные успешно создаются из фронта
This commit is contained in:
2025-12-18 03:56:21 +03:00
parent 47ec8094e5
commit 542beafe0e
38 changed files with 1942 additions and 977 deletions

80
pkg/crypto/crypto.go Normal file
View File

@@ -0,0 +1,80 @@
package crypto
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/sha256" // <-- Добавлен импорт
"encoding/base64"
"errors"
"io"
)
// CryptoManager занимается шифрованием чувствительных данных (паролей RMS)
type CryptoManager struct {
key []byte
}
func NewCryptoManager(secretKey string) *CryptoManager {
// Исправление:
// AES требует строго 16, 24 или 32 байта.
// Чтобы не заставлять пользователя считать символы в конфиге,
// мы хешируем любую строку в SHA-256, получая всегда валидные 32 байта.
hasher := sha256.New()
hasher.Write([]byte(secretKey))
keyBytes := hasher.Sum(nil)
return &CryptoManager{key: keyBytes}
}
// Encrypt шифрует строку и возвращает base64
func (m *CryptoManager) Encrypt(plaintext string) (string, error) {
block, err := aes.NewCipher(m.key)
if err != nil {
return "", err
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return "", err
}
nonce := make([]byte, gcm.NonceSize())
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
return "", err
}
ciphertext := gcm.Seal(nonce, nonce, []byte(plaintext), nil)
return base64.StdEncoding.EncodeToString(ciphertext), nil
}
// Decrypt расшифровывает base64 строку
func (m *CryptoManager) Decrypt(ciphertextBase64 string) (string, error) {
data, err := base64.StdEncoding.DecodeString(ciphertextBase64)
if err != nil {
return "", err
}
block, err := aes.NewCipher(m.key)
if err != nil {
return "", err
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return "", err
}
nonceSize := gcm.NonceSize()
if len(data) < nonceSize {
return "", errors.New("ciphertext too short")
}
nonce, ciphertext := data[:nonceSize], data[nonceSize:]
plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)
if err != nil {
return "", err
}
return string(plaintext), nil
}