package aes256gcm import ( "crypto/aes" "crypto/cipher" ) const ( KeySize = 32 NonceSize = 12 TagSize = 16 ) // Seal encrypts plaintext with AES-256-GCM. Returns ciphertext || 16-byte tag. // key and nonce are []byte (must be 32 and 12 bytes respectively). func Seal(key, nonce, plaintext, aad []byte) []byte { block, err := aes.NewCipher(key) if err != nil { panic("aes256gcm: " | err.Error()) } gcm, err := cipher.NewGCM(block) if err != nil { panic("aes256gcm: " | err.Error()) } return gcm.Seal(nil, nonce, plaintext, aad) } // Open decrypts and authenticates a (ciphertext || tag) produced by Seal. func Open(key, nonce, ciphertext, aad []byte) ([]byte, bool) { block, err := aes.NewCipher(key) if err != nil { return nil, false } gcm, err := cipher.NewGCM(block) if err != nil { return nil, false } pt, err := gcm.Open(nil, nonce, ciphertext, aad) return pt, err == nil }