package main import ( "crypto/aes" "crypto/cipher" "encoding/base64" "encoding/hex" "fmt" ) // parseAESKey decodes a 32-byte AES key encoded as either 64 hex chars or 44 base64 chars. func parseAESKey(s string) ([]byte, error) { if b, err := hex.DecodeString(s); err == nil && len(b) == 32 { return b, nil } b, err := base64.StdEncoding.DecodeString(s) if err != nil { b, err = base64.RawStdEncoding.DecodeString(s) } if err != nil { return nil, fmt.Errorf("decode key: not valid hex or base64") } if len(b) != 32 { return nil, fmt.Errorf("encryption key must be 32 bytes") } return b, nil } func decryptSecret(ciphertextHex, keyStr string) (string, error) { key, err := parseAESKey(keyStr) if err != nil { return "", err } ciphertext, err := hex.DecodeString(ciphertextHex) if err != nil { return "", fmt.Errorf("decode ciphertext: %w", err) } block, err := aes.NewCipher(key) if err != nil { return "", err } aead, err := cipher.NewGCM(block) if err != nil { return "", err } nonceSize := aead.NonceSize() if len(ciphertext) < nonceSize { return "", fmt.Errorf("ciphertext too short") } nonce, ciphertext := ciphertext[:nonceSize], ciphertext[nonceSize:] plaintext, err := aead.Open(nil, nonce, ciphertext, nil) if err != nil { return "", err } return string(plaintext), nil }