Skip to content

Commit

Permalink
OTP.byte_secret: do not mutate state
Browse files Browse the repository at this point in the history
  • Loading branch information
kislyuk committed Feb 4, 2021
1 parent e4ddcf3 commit ee827b4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/pyotp/otp.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ def generate_otp(self, input: int) -> str:
return str_code

def byte_secret(self) -> bytes:
missing_padding = len(self.secret) % 8
secret = self.secret
missing_padding = len(secret) % 8
if missing_padding != 0:
self.secret += '=' * (8 - missing_padding)
return base64.b32decode(self.secret, casefold=True)
secret += '=' * (8 - missing_padding)
return base64.b32decode(secret, casefold=True)

@staticmethod
def int_to_bytestring(i: int, padding: int = 8) -> bytes:
Expand Down
5 changes: 5 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ def test_provisioning_uri(self):
).provisioning_uri()
)

code = pyotp.totp.TOTP("S46SQCPPTCNPROMHWYBDCTBZXV")
self.assertEqual(code.provisioning_uri(), "otpauth://totp/Secret?secret=S46SQCPPTCNPROMHWYBDCTBZXV")
code.verify("123456")
self.assertEqual(code.provisioning_uri(), "otpauth://totp/Secret?secret=S46SQCPPTCNPROMHWYBDCTBZXV")

def test_other_secret(self):
hotp = pyotp.HOTP(
'N3OVNIBRERIO5OHGVCMDGS4V4RJ3AUZOUN34J6FRM4P6JIFCG3ZA')
Expand Down

0 comments on commit ee827b4

Please sign in to comment.