@@ -34,9 +34,9 @@ Instead of natural language (ambiguous, slow), PULSE uses **semantic concepts**:
3434- 📝 ** JSON Encoding** - Human-readable format for debugging and development
3535- ⚡ ** Binary Encoding** - MessagePack format with 10× size reduction (Week 2 ✅)
3636- ✅ ** Automatic Validation** - Validates against vocabulary with helpful error messages
37- - 🔒 ** Security Ready ** - Framework for HMAC signing and replay protection (Week 3)
37+ - 🔒 ** Security Features ** - HMAC-SHA256 signing and replay protection (Week 3 ✅ )
3838- 📊 ** Type Safe** - Full type hints for excellent IDE support
39- - 🧪 ** Well Tested** - 100 + unit tests, 90%+ coverage
39+ - 🧪 ** Well Tested** - 140 + unit tests, 90%+ coverage
4040- 📖 ** Fully Documented** - Comprehensive docstrings, examples, and guides
4141
4242---
@@ -164,6 +164,45 @@ binary_bytes = encoder.encode(message, format="binary")
164164decoded_msg = encoder.decode(binary_bytes) # Detects binary format
165165```
166166
167+ ### Security Features (Week 3 ✅)
168+
169+ ``` python
170+ from pulse import PulseMessage, SecurityManager, KeyManager
171+
172+ # Initialize security manager with secret key
173+ security = SecurityManager(secret_key = " my-secret-key" )
174+
175+ # Create and sign a message
176+ message = PulseMessage(
177+ action = " ACT.TRANSFER.MONEY" ,
178+ target = " ENT.RESOURCE.DATABASE" ,
179+ parameters = {" amount" : 1000 , " to" : " account-123" }
180+ )
181+
182+ # Sign message with HMAC-SHA256
183+ signature = security.sign_message(message)
184+ print (f " Signature: { signature[:32 ]} ... " )
185+
186+ # Verify signature
187+ is_valid = security.verify_signature(message)
188+ print (f " Valid: { is_valid} " ) # True
189+
190+ # Tamper detection
191+ message.content[' parameters' ][' amount' ] = 1000000
192+ is_valid = security.verify_signature(message)
193+ print (f " Valid after tampering: { is_valid} " ) # False - tampering detected!
194+
195+ # Replay protection
196+ nonce_store = set ()
197+ result = security.check_replay_protection(message, nonce_store = nonce_store)
198+ print (f " Replay check: { result[' is_valid' ]} " )
199+
200+ # Key management
201+ km = KeyManager()
202+ key = km.generate_and_store(" agent-1" )
203+ retrieved_key = km.get_key(" agent-1" )
204+ ```
205+
167206---
168207
169208## 📊 Vocabulary Categories
@@ -293,12 +332,54 @@ for attempt in range(1, max_retries + 1):
293332 print (" Max retries reached" )
294333```
295334
335+ ### Example 6: Security Features 🔒
336+
337+ ``` python
338+ from pulse import PulseMessage, SecurityManager, KeyManager
339+
340+ # Create security manager
341+ security = SecurityManager(secret_key = " my-secret-key" )
342+
343+ # Sign a message
344+ message = PulseMessage(
345+ action = " ACT.ANALYZE.SENTIMENT" ,
346+ target = " ENT.DATA.TEXT" ,
347+ parameters = {" text" : " PULSE is secure!" }
348+ )
349+
350+ signature = security.sign_message(message)
351+ print (f " Signed: { signature[:32 ]} ... " )
352+
353+ # Verify signature
354+ is_valid = security.verify_signature(message)
355+ print (f " Valid: { is_valid} " ) # True
356+
357+ # Detect tampering
358+ message.content[' parameters' ][' text' ] = " MODIFIED"
359+ is_valid = security.verify_signature(message)
360+ print (f " Valid after tampering: { is_valid} " ) # False!
361+
362+ # Replay protection
363+ nonce_store = set ()
364+ result = security.check_replay_protection(message, nonce_store = nonce_store)
365+ print (f " Age: { result[' age_seconds' ]:.2f } s " )
366+ print (f " Valid: { result[' is_valid' ]} " )
367+
368+ # Typical output:
369+ # Signed: a3f7b2c8...
370+ # Valid: True
371+ # Valid after tampering: False
372+ # Age: 0.02s
373+ # Valid: True
374+ ```
375+
296376** See [ examples/] ( ./examples/ ) for complete runnable examples:**
297377- ` 01_hello_world.py ` - Basic message creation
298378- ` 02_vocabulary_validation.py ` - Working with vocabulary
299379- ` 03_use_cases.py ` - Real-world scenarios
300380- ` 04_binary_encoding.py ` - Performance benchmarks ⚡
301381- ` 05_error_handling.py ` - Error patterns and recovery ⚡
382+ - ` 06_security_features.py ` - Message signing and verification 🔒
302383
303384---
304385
@@ -321,13 +402,14 @@ pytest -v
321402pytest -m unit
322403```
323404
324- ** Test Coverage:** 100 + tests, 90%+ code coverage
405+ ** Test Coverage:** 140 + tests, 90%+ code coverage
325406
326407** Test Structure:**
327408- ` test_message.py ` - Core message functionality
328409- ` test_vocabulary.py ` - Vocabulary and concept validation
329410- ` test_validator.py ` - Three-stage validation pipeline
330411- ` test_encoder.py ` - Binary encoding, roundtrip, performance ⚡
412+ - ` test_security.py ` - HMAC signing, replay protection 🔒
331413
332414---
333415
@@ -453,6 +535,54 @@ decoded = binary_encoder.decode(binary_bytes)
453535- `" binary" ` - MessagePack, ~ 80 bytes (10 × reduction) ⚡
454536- `" compact" ` - Custom format , ~ 60 bytes (13 × reduction) - Coming soon
455537
538+ # ## Security Classes 🔒
539+
540+ ```python
541+ from pulse import SecurityManager, KeyManager
542+
543+ # SecurityManager - HMAC-SHA256 signing and verification
544+ security = SecurityManager(secret_key = " my-secret-key" )
545+
546+ # Sign message
547+ message = PulseMessage(action = " ACT.QUERY.DATA" )
548+ signature = security.sign_message(message)
549+
550+ # Verify signature
551+ is_valid = security.verify_signature(message)
552+
553+ # Check replay protection
554+ result = security.check_replay_protection(
555+ message,
556+ max_age_seconds = 300 , # 5 minutes
557+ nonce_store = set () # For nonce deduplication
558+ )
559+
560+ # KeyManager - Simple key storage
561+ km = KeyManager()
562+ key = km.generate_and_store(" agent-1" )
563+ retrieved = km.get_key(" agent-1" )
564+ ```
565+
566+ ** SecurityManager Methods:**
567+ - ` sign_message(message) -> str ` - Sign message with HMAC-SHA256
568+ - ` verify_signature(message, expected_signature=None) -> bool ` - Verify signature
569+ - ` check_replay_protection(message, max_age_seconds=300, nonce_store=None) -> dict ` - Check replay indicators
570+ - ` generate_key() -> str ` - Static method to generate secure random key
571+
572+ ** KeyManager Methods:**
573+ - ` generate_and_store(agent_id) -> str ` - Generate and store key for agent
574+ - ` store_key(agent_id, key) ` - Store existing key
575+ - ` get_key(agent_id) -> Optional[str] ` - Retrieve stored key
576+ - ` remove_key(agent_id) -> bool ` - Remove stored key
577+ - ` list_agents() -> list ` - List all agents with keys
578+
579+ ** Security Features:**
580+ - HMAC-SHA256 message signing
581+ - Constant-time signature comparison (timing attack protection)
582+ - Replay protection (timestamp freshness + nonce deduplication)
583+ - Tamper detection (any modification invalidates signature)
584+ - Performance: ~ 1-2ms per operation
585+
456586---
457587
458588## 🛠️ Development
@@ -500,19 +630,22 @@ pulse-python/
500630│ ├── vocabulary.py # Vocabulary system (120+ concepts)
501631│ ├── validator.py # MessageValidator
502632│ ├── encoder.py # JSON/Binary/Compact encoders ⚡
633+ │ ├── security.py # SecurityManager, KeyManager 🔒
503634│ ├── exceptions.py # Custom exceptions
504635│ └── version.py # Version info
505- ├── tests/ # Test suite (100 + tests)
636+ ├── tests/ # Test suite (140 + tests)
506637│ ├── test_message.py
507638│ ├── test_vocabulary.py
508639│ ├── test_validator.py
509- │ └── test_encoder.py # Binary encoding tests ⚡
640+ │ ├── test_encoder.py # Binary encoding tests ⚡
641+ │ └── test_security.py # Security tests 🔒
510642├── examples/ # Usage examples
511643│ ├── 01_hello_world.py
512644│ ├── 02_vocabulary_validation.py
513645│ ├── 03_use_cases.py
514646│ ├── 04_binary_encoding.py ⚡
515- │ └── 05_error_handling.py ⚡
647+ │ ├── 05_error_handling.py ⚡
648+ │ └── 06_security_features.py 🔒
516649└── docs/ # Documentation
517650```
518651
@@ -543,29 +676,32 @@ This project is open source and will remain free forever.
543676
544677## 📊 Project Status
545678
546- ** Version:** 0.2 .0 (Alpha - Week 2 Complete ✅)
679+ ** Version:** 0.3 .0 (Alpha - Week 3 Complete ✅)
547680** Python:** 3.8+
548681** Status:** Active Development
549682
550683### What's Working ✅
551684- Core message creation and parsing
552685- JSON encoding/decoding (human-readable)
553686- ** Binary encoding/decoding (MessagePack, 10× size reduction)** ⚡
687+ - ** HMAC-SHA256 message signing for integrity** 🔒
688+ - ** Replay protection (timestamp + nonce deduplication)** 🔒
689+ - ** Tamper detection and signature verification** 🔒
554690- Vocabulary system (120+ concepts across 10 categories)
555691- Three-stage message validation
556692- Error handling patterns (retry, circuit breaker, graceful degradation)
557693- Unified Encoder with auto-format detection
558- - 100+ unit tests with 90%+ coverage
694+ - Key management (SecurityManager, KeyManager)
695+ - 140+ unit tests with 90%+ coverage
559696
560697### Coming Soon 🚧
561- - ** Week 3:** Security (HMAC signing, replay protection, TLS)
562698- ** Week 4:** CLI tool, performance optimization, full documentation
563- - ** Future:** Compact encoding (13× reduction), network client/server, framework integrations, 1,000 concepts
699+ - ** Future:** Compact encoding (13× reduction), TLS integration, network client/server, framework integrations, 1,000 concepts
564700
565701### Known Limitations
566702- Vocabulary contains 120 concepts (target: 1,000)
567703- Compact encoding not yet implemented (placeholder in place)
568- - Security features framework only (implementation in Week 3 )
704+ - TLS integration not yet implemented ( Week 4 )
569705- No network transport yet (Week 4)
570706
571707---
0 commit comments