79 lines
1.7 KiB
Go
79 lines
1.7 KiB
Go
package db
|
|
|
|
import (
|
|
"crypto/aes"
|
|
"crypto/cipher"
|
|
"crypto/rand"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"io"
|
|
)
|
|
|
|
var encryptionKey []byte
|
|
|
|
func SetEncryptionKey(hexKey string) error {
|
|
key, err := hex.DecodeString(hexKey)
|
|
if err != nil {
|
|
return fmt.Errorf("decode encryption key: %w", err)
|
|
}
|
|
if len(key) != 32 {
|
|
return fmt.Errorf("encryption key must be 32 bytes (64 hex chars), got %d bytes", len(key))
|
|
}
|
|
encryptionKey = key
|
|
return nil
|
|
}
|
|
|
|
func Encrypt(plaintext string) (string, error) {
|
|
if len(encryptionKey) == 0 {
|
|
return plaintext, nil
|
|
}
|
|
block, err := aes.NewCipher(encryptionKey)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
aesGCM, err := cipher.NewGCM(block)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
nonce := make([]byte, aesGCM.NonceSize())
|
|
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
|
|
return "", err
|
|
}
|
|
ciphertext := aesGCM.Seal(nonce, nonce, []byte(plaintext), nil)
|
|
return hex.EncodeToString(ciphertext), nil
|
|
}
|
|
|
|
func Decrypt(ciphertextHex string) (string, error) {
|
|
if len(encryptionKey) == 0 {
|
|
return ciphertextHex, nil
|
|
}
|
|
ciphertext, err := hex.DecodeString(ciphertextHex)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
block, err := aes.NewCipher(encryptionKey)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
aesGCM, err := cipher.NewGCM(block)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
nonceSize := aesGCM.NonceSize()
|
|
if len(ciphertext) < nonceSize {
|
|
return "", fmt.Errorf("ciphertext too short")
|
|
}
|
|
nonce, ciphertext := ciphertext[:nonceSize], ciphertext[nonceSize:]
|
|
plaintext, err := aesGCM.Open(nil, nonce, ciphertext, nil)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return string(plaintext), nil
|
|
}
|
|
|
|
func HashSHA256(data string) string {
|
|
h := sha256.Sum256([]byte(data))
|
|
return hex.EncodeToString(h[:])
|
|
}
|