Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow lowercase algorithms #169

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/pyotp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,12 @@ def parse_uri(uri: str) -> OTP:
raise ValueError("If issuer is specified in both label and parameters, it should be equal.")
otp_data["issuer"] = value
elif key == "algorithm":
if value == "SHA1":
upper_value = value.upper()
if upper_value == "SHA1":
otp_data["digest"] = hashlib.sha1
elif value == "SHA256":
elif upper_value == "SHA256":
otp_data["digest"] = hashlib.sha256
elif value == "SHA512":
elif upper_value == "SHA512":
otp_data["digest"] = hashlib.sha512
else:
raise ValueError("Invalid value for algorithm, must be SHA1, SHA256 or SHA512")
Expand Down
5 changes: 5 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,11 @@ def test_algorithms(self):
otp.provisioning_uri(name="n", issuer_name="i"), "otpauth://hotp/i:n?secret=GEZDGNBV&issuer=i&counter=1"
)

otp = pyotp.parse_uri("otpauth://hotp?algorithm=sha1&secret=GEZDGNBV")
self.assertEqual(hashlib.sha1, otp.digest)
self.assertEqual(otp.at(0), "734055")
self.assertEqual(otp.at(1), "662488")

otp = pyotp.parse_uri("otpauth://totp?algorithm=SHA1&secret=GEZDGNBV&algorithm=SHA256")
self.assertEqual(hashlib.sha256, otp.digest)
self.assertEqual(otp.at(0), "918961")
Expand Down