Encrypt encrypts a plain text message. Uses AES128 keys in GCM (Galois/Counter Mode). Since GCM uses a nonce, the encrypted message will be different each time the operation is run for the same set of inputs. The returned cipher is in the format |nonce(12)|tag(16)|encrypted(plaintextLen)|.
(key types.AES128Key, plaintext []byte)
| 31 | // Since GCM uses a nonce, the encrypted message will be different each time the operation is run for the same set of inputs. |
| 32 | // The returned cipher is in the format |nonce(12)|tag(16)|encrypted(plaintextLen)|. |
| 33 | func Encrypt(key types.AES128Key, plaintext []byte) ([]byte, error) { |
| 34 | cipherBlock, err := aes.NewCipher(key[:]) |
| 35 | if err != nil { |
| 36 | return nil, err |
| 37 | } |
| 38 | gcm, err := cipher.NewGCM(cipherBlock) |
| 39 | if err != nil { |
| 40 | return nil, err |
| 41 | } |
| 42 | nonce := make([]byte, gcm.NonceSize()) |
| 43 | _, err = io.ReadFull(rand.Reader, nonce) |
| 44 | if err != nil { |
| 45 | return nil, err |
| 46 | } |
| 47 | return gcm.Seal(nonce, nonce, plaintext, nil), nil |
| 48 | } |
| 49 | |
| 50 | // Decrypt decrypts an encrypted message. |
| 51 | // Uses AES128 keys in GCM (Galois/Counter Mode). |
no outgoing calls