Skip to content

Instantly share code, notes, and snippets.

@worldofprasanna
Last active November 6, 2018 23:30
Show Gist options
  • Save worldofprasanna/5e92ff1ca863d17b8b5f55941dec959d to your computer and use it in GitHub Desktop.
Save worldofprasanna/5e92ff1ca863d17b8b5f55941dec959d to your computer and use it in GitHub Desktop.
AES GCM Encryption in golang
key := "Symmetric Key" //
func getGCM() {
if block, err := aes.NewCipher(key); err != nil {
return nil, err
} else if gcm, err := cipher.NewGCM(block); err != nil {
return nil, err
} else {
return gcm, nil
}
}
func Encrypt() {
gcm := getGCM()
nonce := make([]byte, gcm.NonceSize())
plainText := []byte(message)
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
logrus.Error("Error occurred in io.ReadFull in CryptoService", err.Error())
return "", nil
}
return base64.StdEncoding.EncodeToString(gcm.Seal(nonce, nonce, plainText, nil)), nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment