Skip to content
This repository has been archived by the owner on Feb 8, 2018. It is now read-only.

Commit

Permalink
Merge pull request #4412 from gratipay/hmac_compare
Browse files Browse the repository at this point in the history
Use hmac.compare_digest()
  • Loading branch information
chadwhitacre authored Apr 24, 2017
2 parents 77bdc7d + abaec02 commit 0522511
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions gratipay/security/crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import random
import string
import time
import hmac

from cryptography.fernet import Fernet, MultiFernet

Expand Down Expand Up @@ -57,13 +58,9 @@ def constant_time_compare(val1, val2):
Returns True if the two strings are equal, False otherwise.
The time taken is independent of the number of characters that match.
https://codahale.com/a-lesson-in-timing-attacks/
"""
if len(val1) != len(val2):
return False
result = 0
for x, y in zip(val1, val2):
result |= ord(x) ^ ord(y)
return result == 0
return hmac.compare_digest(bytes(val1), bytes(val2))


# Encrypting Packer
Expand Down

0 comments on commit 0522511

Please sign in to comment.