Skip to content

add Utility for enc/dec - #93

Merged
robindiddams merged 1 commit into
mainfrom
encryptbytes
Sep 9, 2025
Merged

robindiddams merged 1 commit into
mainfrom
encryptbytes

Conversation

@robindiddams

@robindiddams robindiddams commented Sep 9, 2025 •

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

  • New Features

    • Introduced in-memory encryption/decryption helpers for byte data, enabling direct byte-in/byte-out operations without managing streams. This reduces boilerplate and improves ergonomics while supporting both small and large payloads. Existing streaming options remain unchanged.
  • Tests

    • Added comprehensive tests validating in-memory encryption/decryption across typical, empty, and large inputs to ensure reliability and correctness.

@coderabbitai

coderabbitai Bot commented Sep 9, 2025 •

Copy link
Copy Markdown
Contributor

Walkthrough

Adds 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

Cohort / File(s) Summary
In-memory byte APIs
crypto/crypto.go
Added EncryptBytes([]byte, string) ([]byte, error) and DecryptBytes([]byte, string) ([]byte, error) wrappers around EncryptStream/DecryptStream. Introduced internal nopCloser to adapt bytes.Buffer to io.WriteCloser. Imported bytes.
Tests for byte APIs
crypto/crypto_test.go
Removed test-local nopCloser helper. Added TestBytesEncryptionDecryption covering simple, empty, and large data using EncryptBytes/DecryptBytes with key "test-key".

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
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested reviewers

  • jhaynie

Pre-merge checks (2 warnings, 1 inconclusive)

❌ Failed checks (2 warnings, 1 inconclusive)
Check name Status Explanation Resolution
Description Check ⚠️ Warning The pull request currently has no description, so it provides no context or details about the new in-memory encryption/decryption wrappers, the nopCloser adapter, or the accompanying tests. Add a description summarizing the addition of EncryptBytes and DecryptBytes functions, the nopCloser type adaptation, and the new tests validating encryption and decryption of byte slices.
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
Title Check ❓ Inconclusive The title “add Utility for enc/dec” is related to the change but uses vague shorthand and does not clearly identify the specific new functions or the crypto package context, so it does not concisely convey the primary change. Rename the title to explicitly summarize the main change, for example “Add EncryptBytes and DecryptBytes convenience wrappers to crypto package.”

Poem

I nibbled bytes like clover leaves,
Wrapped them snug in secret sleeves;
A hop, a skip—decrypt, delight!
Plaintext dawn from cipher night.
With buffers warm and streams just right,
My whiskers twitch—it works! Alright! 🐇✨

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.

  • Built-in checks – Quickly apply ready-made checks to enforce title conventions, require pull request descriptions that follow templates, validate linked issues for compliance, and more.
  • Custom agentic checks – Define your own rules using CodeRabbit’s advanced agentic capabilities to enforce organization-specific policies and workflows. For example, you can instruct CodeRabbit’s agent to verify that API documentation is updated whenever API schema files are modified in a PR. Note: Upto 5 custom checks are currently allowed during the preview period. Pricing for this feature will be announced in a few weeks.

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 Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch encryptbytes

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai
coderabbitai Bot requested a review from jhaynie September 9, 2025 14:12

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between bc93f2e and af4466c.

📒 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.

@robindiddams
robindiddams merged commit d4e687e into main Sep 9, 2025
5 checks passed
@robindiddams
robindiddams deleted the encryptbytes branch September 9, 2025 14:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants