Skip to content

test(crypto): add unit tests for crypto helpers - #2

Open
EduardF1 wants to merge 3 commits into
marlinjai:mainfrom
EduardF1:test/crypto-unit-tests
Open

test(crypto): add unit tests for crypto helpers#2
EduardF1 wants to merge 3 commits into
marlinjai:mainfrom
EduardF1:test/crypto-unit-tests

Conversation

@EduardF1

Copy link
Copy Markdown

Summary

src/crypto.ts holds the security-critical primitives (API-key generation,
SHA-256 hashing, key verification, and constant-time comparison) but had no
test coverage
auth.spec.ts was the only spec. This adds crypto.spec.ts
(18 tests) to lock the contract and guard against regressions.

Coverage

  • generateApiKeysk_live_ prefix, base64url body (no + / / / =),
    expected 32-char length (24 random bytes), and uniqueness across 1000 keys.
  • hashApiKey — 64-char lowercase hex, a known SHA-256 vector
    (SHA-256("abc")), determinism, and input sensitivity. The known-answer test
    pins the hex encoding so any byte-order/padding regression is caught.
  • verifyApiKey — round-trip success, mismatch rejection, and graceful
    false on a malformed stored hash.
  • timingSafeEqual — identical, empty, equal-length-but-different,
    different-length, and last-character-only-differs cases.
  • generateId — RFC 4122 v4 shape and uniqueness.

Validation

  • pnpm test -> 42 passed (24 existing + 18 new)
  • pnpm typecheck -> clean
  • Test-only change; no source modifications, no new dependencies.

Add coverage for API key generation, SHA-256 hashing, key verification,
timing-safe comparison, and UUID generation.

The crypto spec checks live API key prefixes, base64url formatting, key length,
uniqueness, known hash vectors, malformed-hash rejection, constant-time
comparison outcomes, and UUID v4 shape.
@EduardF1
EduardF1 force-pushed the test/crypto-unit-tests branch from 84914cb to e4cb526 Compare June 30, 2026 12:53
EduardF1 added 2 commits July 13, 2026 10:19
This repository uses pnpm; the npm lockfile was committed by mistake and dominated the diff.
@EduardF1

EduardF1 commented Aug 3, 2026

Copy link
Copy Markdown
Author

Removed the package-lock.json that was committed by mistake (this repo uses pnpm) in da5105d. The diff is now just src/crypto.spec.ts.

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.

1 participant