Skip to content

Commit

Permalink
fix: use formatted strings instead of bytes in verify method
Browse files Browse the repository at this point in the history
  • Loading branch information
lepture committed Sep 9, 2023
1 parent b0847d4 commit 6c0f358
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/otpauth/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def string_code(self, code: int) -> str:
:param code: The number that this OTP generated.
"""
return "{code:0{w}}".format(code=code, w=self.digit)
return f'{code:0{self.digit}}'

@abstractmethod
def generate(self, *args: t.Any, **kwargs: t.Any) -> int:
Expand Down
2 changes: 1 addition & 1 deletion src/otpauth/rfc4226.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def verify(self, code: int, counter: int) -> bool: # type: ignore[override]
"""
if len(str(code)) > self.digit:
return False
return hmac.compare_digest(f'{self.generate(counter):0{self.digit}}', f'{code:0{self.digit}}')
return hmac.compare_digest(self.string_code(self.generate(counter)), self.string_code(code))

def to_uri(self, label: str, issuer: str, counter: int) -> str: # type: ignore[override]
"""Generate the otpauth protocal string for HOTP.
Expand Down
2 changes: 1 addition & 1 deletion src/otpauth/rfc6238.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def verify(self, code: int, timestamp: t.Optional[int] = None) -> bool: # type:
"""
if len(str(code)) > self.digit:
return False
return hmac.compare_digest(f'{self.generate(timestamp):0{self.digit}}', f'{code:0{self.digit}}')
return hmac.compare_digest(self.string_code(self.generate(timestamp)), self.string_code(code))

def to_uri(self, label: str, issuer: str) -> str: # type: ignore[override]
"""Generate the otpauth protocal string for TOTP.
Expand Down

0 comments on commit 6c0f358

Please sign in to comment.