@@ -6,11 +6,11 @@ The `hmac` module provides HMAC (Hash-based Message Authentication Code) functio
66
77| Operation | Time | Space | Notes |
88| -----------| ------| -------| -------|
9- | ` hmac.new(key, msg, digestmod) ` | O(n) | O(n ) | Create HMAC, n = msg size |
10- | ` HMAC.update(msg) ` | O(n) | O(k ) | Add data to digest, k = hash block size |
9+ | ` hmac.new(key, msg, digestmod) ` | O(n) | O(1 ) | Create HMAC, n = msg size |
10+ | ` HMAC.update(msg) ` | O(n) | O(1 ) | Add data to digest, k = hash block size |
1111| ` HMAC.digest() ` | O(k) | O(k) | Get binary digest, k = hash output size |
1212| ` HMAC.hexdigest() ` | O(k) | O(k) | Get hex digest |
13- | ` hmac.compare_digest(a, b) ` | O(n) | O(1) | Constant-time comparison |
13+ | ` hmac.compare_digest(a, b) ` | O(n) | O(1) | Timing-safe for equal-length inputs |
1414
1515## Creating HMAC
1616
@@ -33,14 +33,14 @@ h = hmac.new(key, message, hashlib.sha256) # O(n)
3333digest = h.digest() # O(k) where k = output size
3434```
3535
36- ### Space Complexity: O(n )
36+ ### Space Complexity: O(1 )
3737
3838``` python
3939import hmac
4040import hashlib
4141
42- # Memory for message processing
43- h = hmac.new(key, message, hashlib.sha256) # O(n) for buffering
42+ # No buffering of the full message (beyond the input itself)
43+ h = hmac.new(key, message, hashlib.sha256) # O(1) extra space
4444```
4545
4646## Streaming Updates
@@ -127,22 +127,22 @@ import hashlib
127127
128128message = b ' data' * 1000
129129
130- # MD5: Fast but deprecated
130+ # MD5: Deprecated (cryptographically broken)
131131# O(n) time
132132h = hmac.new(b ' key' , message, hashlib.md5) # O(n)
133133digest = h.digest() # O(16) bytes
134134
135- # SHA1: Faster than SHA256, deprecated
135+ # SHA1: Deprecated (cryptographically broken)
136136# O(n) time
137137h = hmac.new(b ' key' , message, hashlib.sha1) # O(n)
138138digest = h.digest() # O(20) bytes
139139
140- # SHA256: Default , secure
141- # O(n) time (slower than MD5/SHA1)
140+ # SHA256: Common , secure choice
141+ # O(n) time
142142h = hmac.new(b ' key' , message, hashlib.sha256) # O(n)
143143digest = h.digest() # O(32) bytes
144144
145- # SHA512: More secure, slower
145+ # SHA512: Larger digest size; performance varies by platform
146146# O(n) time
147147h = hmac.new(b ' key' , message, hashlib.sha512) # O(n)
148148digest = h.digest() # O(64) bytes
@@ -209,7 +209,7 @@ if received_hmac == computed_hmac: # ❌ INSECURE
209209
210210# Good: Constant-time comparison
211211if hmac.compare_digest(received_hmac, computed_hmac): # ✓ SECURE
212- # Always takes same time, timing independent
212+ # Timing-safe for equal-length inputs; time depends on length
213213 pass
214214```
215215
@@ -373,8 +373,8 @@ import hashlib
373373# Good balance of security and performance
374374h = hmac.new(b ' key' , b ' data' , hashlib.sha256)
375375
376- # SHA512: MAXIMUM SECURITY
377- # Slower but better for critical applications
376+ # SHA512: Larger digest size
377+ # Performance varies by platform
378378h = hmac.new(b ' key' , b ' data' , hashlib.sha512)
379379```
380380
@@ -406,10 +406,7 @@ h = hmac.new(very_long_key, b'data', hashlib.sha256)
406406
407407## Version Notes
408408
409- - ** Python 2.4+** : Basic HMAC support
410- - ** Python 3.4+** : ` compare_digest() ` function
411- - ** Python 3.6+** : Better performance
412- - ** Python 3.9+** : Additional algorithm support
409+ - ** Python 3.x** : ` hmac ` module available, including ` compare_digest() `
413410
414411## Related Documentation
415412
0 commit comments