Skip to content

[BUG] fix(benchmark): BenchmarkEngine.generateKey produces half the requested byte length #236

Description

@faizahmad-khan

Bug Description

BenchmarkEngine.generateKey(lengthInBytes) in lib/utils/benchmark.ts produces a hex string with lengthInBytes characters (nibbles), which is only lengthInBytes / 2 actual bytes. The function name and parameter clearly document the intent as byte count, but the implementation generates half as many bytes as requested.

Steps to Reproduce

  1. Call BenchmarkEngine.generateKey(16) — intended to produce a 16-byte key for AES-128.
  2. Observe: the returned string has 16 hex characters = 8 bytes, not 16.
  3. Pass this key to an AES encrypt benchmark — it throws INVALID_KEY_LENGTH because AES requires exactly 16, 24, or 32 bytes.

Expected Behavior

BenchmarkEngine.generateKey(16) should return a string representing exactly 16 bytes (i.e. 32 hex characters).

Actual Behavior

The function loops lengthInBytes times, appending one hex character per iteration:

// lib/utils/benchmark.ts line 27
for (let i = 0; i < lengthInBytes; i++) {       // loops 16 times
  result += hex.charAt(...)                       // appends 1 nibble each time
}
// result = 16 chars = 8 bytes ← wrong

This means every cipher that relies on the benchmark engine for key generation receives a key that is half the intended size, causing silent failures or thrown errors during benchmark runs.

Screenshots

N/A — reproducible via unit test:

const key = BenchmarkEngine.generateKey(16)
console.log(key.length) // prints 16 — should be 32 for 16 bytes

Environment

  • OS: Any
  • Browser: Any
  • Version: Latest (main branch)

Additional Context

Affected file: lib/utils/benchmark.ts lines 24–31
Fix: loop lengthInBytes * 2 times so that each byte is represented by two hex characters:

  static generateKey(lengthInBytes: number): string {
    const hex = '0123456789abcdef'
    let result = ''
-   for (let i = 0; i < lengthInBytes; i++) {
+   for (let i = 0; i < lengthInBytes * 2; i++) {
      result += hex.charAt(Math.floor(Math.random() * hex.length))
    }
    return result
  }

Metadata

Metadata

Labels

ECSoC26Elite Coders Summer of Code 2026bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions