Seal encrypts plaintext and returns nonce||ciphertext (tag appended by GCM).
(plaintext []byte)
| 53 | |
| 54 | // Seal encrypts plaintext and returns nonce||ciphertext (tag appended by GCM). |
| 55 | func (c *Crypto) Seal(plaintext []byte) ([]byte, error) { |
| 56 | nonce := make([]byte, c.aead.NonceSize()) |
| 57 | if _, err := rand.Read(nonce); err != nil { |
| 58 | return nil, fmt.Errorf("crypto: nonce read: %w", err) |
| 59 | } |
| 60 | ct := c.aead.Seal(nil, nonce, plaintext, nil) |
| 61 | out := make([]byte, 0, len(nonce)+len(ct)) |
| 62 | out = append(out, nonce...) |
| 63 | out = append(out, ct...) |
| 64 | return out, nil |
| 65 | } |
| 66 | |
| 67 | // Open inverts Seal. Returns an error on auth-tag failure (tampered ciphertext, |
| 68 | // nonce, or tag, or wrong key). |