Check digest function to prevent error on OTP Generation #170
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This pull request introduces a validation check in the
init
andgenerate_otp
functions to prevent errors caused by incompatible digest functions and inadequate digest sizes. The changes aim to improve the stability and reliability of OTP generation. While theRFC4226
states that the base function issha1
, the existing code will allow user to use other hash functions such asMD5
andSHAKE-128
due to their availability in the hashlib.Changes Made
Digest Function Validation
Added checks to disallow the use of
hashlib.md5
andhashlib.shake_128
as digest functions in the__init__
function ofotp.py
,hotp.py
, andtotp.py
. These functions are not suitable for OTP generation due to their shorter hash sizes.Digest Size Check
Implemented a check to ensure that the digest size is not lower than
18 bytes
. This prevents anIndexError
that could occur when the last hash byte is0xF
, causing thegenerate_otp
function to set the offset to15
. The subsequent operation would attempt to accesshmac_hash[offset + 1]
and so on, leading to an out-of-bounds error formd5
andshake128
due to their shorter digest lengths.Impact
Testing
hashlib.md5
andhashlib.shake128
are correctly rejected as digest functions on__init__
.generate_otp
.DigestFunctionTest
unit test as a negative test case where it will trigger an error if the selected digest function ismd5
orshake_128
.