| 50 | } |
| 51 | |
| 52 | func (c *EncryptedCache) Put(ctx context.Context, key string, data []byte) error { |
| 53 | // Select a random nonce, and leave capacity for the ciphertext. |
| 54 | nonce := make([]byte, c.aead.NonceSize(), c.aead.NonceSize()+len(data)+c.aead.Overhead()) |
| 55 | if _, err := cryptorand.Read(nonce); err != nil { |
| 56 | return err |
| 57 | } |
| 58 | |
| 59 | // Encrypt the message and append the ciphertext to the nonce. |
| 60 | encryptedData := c.aead.Seal(nonce, nonce, data, []byte(key)) |
| 61 | |
| 62 | return c.next.Put(ctx, key, encryptedData) |
| 63 | } |
| 64 | |
| 65 | func (c *EncryptedCache) Delete(ctx context.Context, key string) error { |
| 66 | return c.next.Delete(ctx, key) |