Skip to content

Commit

Permalink
Merge pull request #1 from tilgovi/passwordtiming
Browse files Browse the repository at this point in the history
Prevent timing attacks on passwords
  • Loading branch information
sontek committed May 7, 2014
2 parents a730a72 + 29c81d1 commit fd56ccb
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion horus/flows/local/services.py
Expand Up @@ -4,6 +4,24 @@
)


try:
from hmac import compare_digest as is_equal
except ImportError:
def is_equal(lhs, rhs):
"""Returns True if the two strings are equal, False otherwise.
The comparison is based on a common implementation found in Django.
This version avoids a short-circuit even for unequal lengths to reveal
as little as possible. It takes time proportional to the length of its
second argument.
"""
result = 0 if len(lhs) == len(rhs) else 1
lhs = lhs.ljust(len(rhs))
for x, y in zip(lhs, rhs):
result |= ord(x) ^ ord(y)
return result == 0


class AuthenticationService(object):
def __init__(self, backend):
self.backend = backend
Expand All @@ -22,7 +40,7 @@ def login(self, login, password):

if (
user is None or
user.password != password
is_equal(user.password, password) is False
):
raise AuthenticationException()

Expand Down

0 comments on commit fd56ccb

Please sign in to comment.