Fixed some wonky encryption BS

This commit is contained in:
Atridad Lahiji 2024-09-01 18:52:23 -06:00
parent e8e14c988d
commit 32374cfbb4
Signed by: atridad
SSH key fingerprint: SHA256:LGomp8Opq0jz+7kbwNcdfTcuaLRb5Nh0k5AchDDb438
2 changed files with 13 additions and 5 deletions

View file

@ -4,9 +4,9 @@ POSTGRES_PASSWORD=password
POSTGRES_USER=username
# Security
ENCRYPTION_KEY="super-secret"
SIGNING_KEY="super-secret"
AUTH_SECRET="super-secret"
ENCRYPTION_KEY="hOzXzSwDSuU41PMtMHm9O/nqf1X+jTB3MOgVDSPXC5o="
SIGNING_KEY="hOzXzSwDSuU41PMtMHm9O/nqf1X+jTB3MOgVDSPXC5o="
AUTH_SECRET="hOzXzSwDSuU41PMtMHm9O/nqf1X+jTB3MOgVDSPXC5o="
# Feature Flags
DEVMODE=true

View file

@ -4,6 +4,7 @@ import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
@ -36,9 +37,16 @@ func InitSessionMiddleware() echo.MiddlewareFunc {
return session.Middleware(store)
}
// Returns the first 32 bytes of the SHA-256 hash of the ENCRYPTION_KEY environment variable
func getEncryptionKey() []byte {
key := []byte(os.Getenv("ENCRYPTION_KEY"))
hash := sha256.Sum256(key)
return hash[:32] // Use the first 32 bytes for AES-256
}
// Encrypt data using AES
func encrypt(data []byte) (string, error) {
encryptionKey := []byte(os.Getenv("ENCRYPTION_KEY"))
encryptionKey := getEncryptionKey()
fmt.Printf("Encryption Key Length: %d\n", len(encryptionKey))
block, err := aes.NewCipher(encryptionKey)
@ -60,7 +68,7 @@ func encrypt(data []byte) (string, error) {
// decrypt decrypts the data using AES-GCM.
func decrypt(encryptedString string) (string, error) {
encryptionKey := []byte(os.Getenv("ENCRYPTION_KEY"))
encryptionKey := getEncryptionKey()
data, err := base64.StdEncoding.DecodeString(encryptedString)
if err != nil {