test(crypto): add unit tests for crypto helpers - #2
Open
EduardF1 wants to merge 3 commits into
Open
Conversation
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
force-pushed
the
test/crypto-unit-tests
branch
from
June 30, 2026 12:53
84914cb to
e4cb526
Compare
This repository uses pnpm; the npm lockfile was committed by mistake and dominated the diff.
Author
|
Removed the |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
src/crypto.tsholds the security-critical primitives (API-key generation,SHA-256 hashing, key verification, and constant-time comparison) but had no
test coverage —
auth.spec.tswas the only spec. This addscrypto.spec.ts(18 tests) to lock the contract and guard against regressions.
Coverage
generateApiKey—sk_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 testpins the hex encoding so any byte-order/padding regression is caught.
verifyApiKey— round-trip success, mismatch rejection, and gracefulfalseon 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