add Utility for enc/dec - #93
Conversation
WalkthroughAdds in-memory byte-slice helpers EncryptBytes and DecryptBytes that delegate to existing streaming APIs. Introduces an internal nopCloser to adapt bytes.Buffer for EncryptStream/DecryptStream usage. Updates tests to cover byte-slice encryption/decryption across simple, empty, and large inputs; removes prior nopCloser test helper. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
actor Caller
participant Crypto as crypto package
participant Buffer as bytes.Buffer
participant StreamEnc as EncryptStream
participant StreamDec as DecryptStream
rect rgb(230,240,255)
note over Caller,Crypto: EncryptBytes flow
Caller->>Crypto: EncryptBytes(data, key)
Crypto->>Buffer: create bytes.Buffer
Crypto->>StreamEnc: EncryptStream(bytes.NewReader(data), nopCloser{&Buffer}, key)
StreamEnc-->>Crypto: write ciphertext to Buffer
Crypto-->>Caller: Buffer.Bytes()
end
rect rgb(235,255,235)
note over Caller,Crypto: DecryptBytes flow
Caller->>Crypto: DecryptBytes(encryptedData, key)
Crypto->>Buffer: create bytes.Buffer
Crypto->>StreamDec: DecryptStream(bytes.NewReader(encryptedData), nopCloser{&Buffer}, key)
StreamDec-->>Crypto: write plaintext to Buffer
Crypto-->>Caller: Buffer.Bytes()
end
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Suggested reviewers
Pre-merge checks (2 warnings, 1 inconclusive)❌ Failed checks (2 warnings, 1 inconclusive)
Poem
Tip 👮 Agentic pre-merge checks are now available in preview!Pro plan users can now enable pre-merge checks in their settings to enforce checklists before merging PRs.
Example: reviews:
pre_merge_checks:
custom_checks:
- name: "Undocumented Breaking Changes"
mode: "warning"
instructions: |
Pass/fail criteria: All breaking changes to public APIs, CLI flags, environment variables, configuration keys, database schemas, or HTTP/GraphQL endpoints must be documented in the "Breaking Change" section of the PR description and in CHANGELOG.md. Exclude purely internal or private changes (e.g., code not exported from package entry points or explicitly marked as internal).Please share your feedback with us on this Discord post. ✨ Finishing Touches
🧪 Generate unit tests
Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (4)
crypto/crypto.go (3)
176-189: Wrap error with context in EncryptBytes.Helps trace failures to the wrapper when bubbling up.
- if err := EncryptStream(reader, writer, key); err != nil { - return nil, err - } + if err := EncryptStream(reader, writer, key); err != nil { + return nil, fmt.Errorf("EncryptBytes: %w", err) + }
191-204: Wrap error with context in DecryptBytes.Same reasoning as above.
- if err := DecryptStream(reader, writer, key); err != nil { - return nil, err - } + if err := DecryptStream(reader, writer, key); err != nil { + return nil, fmt.Errorf("DecryptBytes: %w", err) + }
206-213: Minor: clarify nopCloser usage or provide helper.To avoid confusion with io.NopCloser (reader), consider a tiny helper and use it at call sites.
// keep type as-is for tests, add helper: func nopWriteCloser(w io.Writer) io.WriteCloser { return nopCloser{w} }Then:
- writer := &nopCloser{&buf} + writer := nopWriteCloser(&buf)crypto/crypto_test.go (1)
682-725: Add a multi-chunk case to exercise the byte-slice wrappers across chunk boundaries.Current cases don’t exceed 64KiB; include one >64KiB to validate multi-chunk path via EncryptBytes/DecryptBytes.
{ name: "large data", data: bytes.Repeat([]byte("test"), 1000), key: "test-key", }, + { + name: "multi-chunk data (>64KiB)", + data: bytes.Repeat([]byte("x"), 200*1024), + key: "test-key", + },
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
crypto/crypto.go(2 hunks)crypto/crypto_test.go(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
crypto/crypto_test.go (1)
crypto/crypto.go (2)
EncryptBytes(179-189)DecryptBytes(194-204)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: Analyze (go)
🔇 Additional comments (1)
crypto/crypto.go (1)
4-4: LGTM on bytes import.
Summary by CodeRabbit
New Features
Tests