You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
628 B
Go

package aes
import (
// Standard
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
)
// Encrypt
func (Algorithm) Encrypt(data []byte, key []byte) (string, error) {
// Block
block, err := aes.NewCipher(key)
// Handle Error
if err != nil {
return "", err
}
// GCM
gcm, err := cipher.NewGCM(block)
// Handle Error
if err != nil {
return "", err
}
// Nonce
nonce := make([]byte, gcm.NonceSize())
_, err = rand.Read(nonce)
// Handle Error
if err != nil {
return "", err
}
// Return Encrypted Data
return base64.RawStdEncoding.EncodeToString(gcm.Seal(nonce, nonce, []byte(data), nil)), err
}