package utils import ( "crypto/aes" "crypto/cipher" "crypto/rand" "encoding/hex" "errors" "fmt" "io" ) func AESEncrypt(plaintext string, key *[32]byte) (ciphertext string, err error) { block, err := aes.NewCipher(key[:]) if err != nil { return "", err } gcm, err := cipher.NewGCM(block) if err != nil { return "", err } nonce := make([]byte, gcm.NonceSize()) _, err = io.ReadFull(rand.Reader, nonce) if err != nil { return "", err } cypher := gcm.Seal(nonce, nonce, []byte(plaintext), nil) cyp := fmt.Sprintf("%x", cypher) return cyp, nil } func AESDecrypt(text string, key *[32]byte) (plaintext string, err error) { ciphertext, _ := hex.DecodeString(text) block, err := aes.NewCipher(key[:]) if err != nil { return "", err } gcm, err := cipher.NewGCM(block) if err != nil { return "", err } if len(ciphertext) < gcm.NonceSize() { return "", errors.New("malformed ciphertext") } decodedtext, err := gcm.Open(nil, ciphertext[:gcm.NonceSize()], ciphertext[gcm.NonceSize():], nil, ) if err != nil { return "", err } return string(decodedtext), err }