Encrypt encrypts the given plaintext using AES-GCM with the provided base64 encoded key and returns the ciphertext as a base64 encoded string
(plaintext, base64Key string)
| 25 | |
| 26 | // Encrypt encrypts the given plaintext using AES-GCM with the provided base64 encoded key and returns the ciphertext as a base64 encoded string |
| 27 | func Encrypt(plaintext, base64Key string) (string, error) { |
| 28 | key, err := base64.StdEncoding.DecodeString(base64Key) |
| 29 | if err != nil { |
| 30 | return "", err |
| 31 | } |
| 32 | |
| 33 | block, err := aes.NewCipher(key) |
| 34 | if err != nil { |
| 35 | return "", err |
| 36 | } |
| 37 | |
| 38 | gcm, err := cipher.NewGCM(block) |
| 39 | if err != nil { |
| 40 | return "", err |
| 41 | } |
| 42 | |
| 43 | nonce := make([]byte, gcm.NonceSize()) |
| 44 | if _, err := io.ReadFull(rand.Reader, nonce); err != nil { |
| 45 | return "", err |
| 46 | } |
| 47 | |
| 48 | ciphertext := gcm.Seal(nonce, nonce, []byte(plaintext), nil) |
| 49 | return base64.StdEncoding.EncodeToString(ciphertext), nil |
| 50 | } |
| 51 | |
| 52 | // Decrypt decrypts the given base64 encoded ciphertext using AES-GCM with the provided base64 encoded key and returns the plaintext |
| 53 | func Decrypt(base64Ciphertext, base64Key string) (string, error) { |
nothing calls this directly
no outgoing calls
no test coverage detected