Last active
November 6, 2018 23:30
-
-
Save worldofprasanna/5e92ff1ca863d17b8b5f55941dec959d to your computer and use it in GitHub Desktop.
AES GCM Encryption in golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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