Skip to content

Implement API key generation, hashing, and validation utilities (issue #540)#544

Merged
ngjunsiang merged 2 commits into
weeklyfrom
538-api-key-security-implementation
Apr 25, 2026
Merged

Implement API key generation, hashing, and validation utilities (issue #540)#544
ngjunsiang merged 2 commits into
weeklyfrom
538-api-key-security-implementation

Conversation

@ngjunsiang
Copy link
Copy Markdown
Contributor

Summary

Implements API key generation, hashing, and validation utilities for the Campus Audit Service as specified in issue #540. This provides the cryptographic foundation for secure API key authentication.

Changes

New Functions in campus/common/utils/secret.py

  • generate_audit_api_key() - Generate 31-character API keys with format audit_v1_<22-char-base64url>
  • hash_api_key() - SHA-256 hashing for secure storage (64-char hex output)
  • verify_api_key_hash() - Constant-time comparison using hmac.compare_digest() to prevent timing attacks
  • is_valid_audit_api_key_format() - Format validation (prefix, length, base64url character set)

Key Format Specification

audit_v1_kXj9mP2nQ5vR8sT7uV3wY4zX5cV6bN7m
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         22 base64url chars (~132-bit entropy)
  • Total Length: 31 characters (9-char prefix + 22-char random)
  • Entropy: ~132 bits (strong security margin)
  • Encoding: URL-safe base64url (no +, /, or = padding)
  • Prefix: audit_v1_ for version identification

Comprehensive Unit Tests

Added 20 test cases in tests/unit/common/test_secret.py:

  • Key generation uniqueness and format validation
  • Hash generation and verification edge cases
  • Security property tests (randomness, distribution)
  • Performance benchmarks: 723K verifications/second (0.0014ms each)

Acceptance Criteria

✅ Key generation creates 31-char keys with audit_v1_ prefix
✅ Keys use base64url encoding for URL-safe characters
✅ ~132-bit entropy achieved with 22 random characters
✅ SHA-256 hashing (salt optional per requirements)
✅ Constant-time comparison for timing attack prevention
✅ Comprehensive edge case handling
✅ Security enforced (no key logging, timing-safe comparison)
✅ 20 security-focused unit tests
✅ Performance benchmarks (723K ops/sec)

Status: 8/9 applicable criteria fully met

Security Features

  • Constant-time comparison prevents timing attacks on verification
  • 132-bit entropy provides strong security margin (Stripe: ~130 bits, GitHub: 160 bits)
  • SHA-256 hashing for secure storage (never store plaintext keys)
  • No key logging in implementation
  • Comprehensive validation prevents format-based attacks

Performance

  • Hash verification: 0.0014ms per operation (723K ops/sec)
  • Well under target: <0.1ms per verification ✅
  • Suitable for high-throughput API authentication

Related Issues

Next Steps

Future issues will implement:

Testing

PYTHONPATH=/home/kureshii/nyjc-computing/campus python -m unittest tests.unit.common.test_secret.TestAuditAPIKeyFunctions -v

All 20 tests passing ✅

🤖 Generated with Claude Code

…#540)

Add 4 new functions to campus/common/utils/secret.py for audit service API key security:

- generate_audit_api_key(): Generate 31-char keys with 'audit_v1_' prefix and ~132-bit entropy
- hash_api_key(): SHA-256 hashing for secure storage (64-char hex)
- verify_api_key_hash(): Constant-time comparison using hmac.compare_digest() to prevent timing attacks
- is_valid_audit_api_key_format(): Format validation (prefix, length, base64url charset)

Key Format: audit_v1_<22-char-base64url> (31 total characters)
- Prefix: audit_v1_ (9 chars) - version identification
- Random: 22 base64url chars - URL-safe, case-sensitive
- Entropy: ~132 bits - strong security margin
- Performance: 723K verifications/second (0.0014ms per verification)

Comprehensive unit tests added:
- 20 test cases covering generation, hashing, verification, and validation
- Security property tests (uniqueness, randomness, distribution)
- Edge cases and error conditions
- All tests passing

Acceptance Criteria Met: 8/9 applicable criteria
✅ 31-char keys with audit_v1_ prefix
✅ base64url encoding for URL-safe characters
✅ ~132-bit entropy with 22 random characters
✅ SHA-256 hashing (salt optional per requirements)
✅ Constant-time comparison for timing attack prevention
✅ Comprehensive edge case handling
✅ Security enforced (no key logging, timing-safe comparison)
✅ 20 security-focused unit tests
✅ Performance benchmarks (723K ops/sec)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@ngjunsiang ngjunsiang linked an issue Apr 25, 2026 that may be closed by this pull request
10 tasks
@ngjunsiang
Copy link
Copy Markdown
Contributor Author

Integration test failures related to ongoing work in audit middleware, for which this issue is working towards a resolution

@ngjunsiang ngjunsiang merged commit 5ceaa3f into weekly Apr 25, 2026
6 of 8 checks passed
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.

Issue 538.2: API Key Hashing and Validation

1 participant