encrypts plaintext using AES-GCM with the given key and returns the base64 encoded ciphertext
(plaintext, key []byte)
| 4028 | |
| 4029 | // encrypts plaintext using AES-GCM with the given key and returns the base64 encoded ciphertext |
| 4030 | func encrypt(plaintext, key []byte) ([]byte, error) { |
| 4031 | block, err := aes.NewCipher(key) |
| 4032 | if err != nil { |
| 4033 | return nil, err |
| 4034 | } |
| 4035 | |
| 4036 | aesGCM, err := cipher.NewGCM(block) |
| 4037 | if err != nil { |
| 4038 | return nil, err |
| 4039 | } |
| 4040 | |
| 4041 | nonce := make([]byte, aesGCM.NonceSize()) |
| 4042 | if _, err := io.ReadFull(rand.Reader, nonce); err != nil { |
| 4043 | return nil, err |
| 4044 | } |
| 4045 | |
| 4046 | // nonce is prepended to the ciphertext, so it can be used for decryption |
| 4047 | ciphertext := aesGCM.Seal(nonce, nonce, plaintext, nil) |
| 4048 | return ciphertext, nil |
| 4049 | } |
| 4050 | |
| 4051 | // decrypts the ciphertext using AES-GCM with the given key and returns the plaintext |
| 4052 | func decrypt(ciphertext, key []byte) ([]byte, error) { |