API References¶
Here are the list of API reference; it might be helpful for developers.
HOTP¶
Implementation of RFC4226, An HMAC-Based One-Time Password Algorithm.
- class otpauth.HOTP(secret: bytes, digit: int = 6, algorithm: Literal['SHA1', 'SHA256', 'SHA512'] = 'SHA1')¶
Implementation of RFC4226, An HMAC-Based One-Time Password Algorithm.
- Parameters:
secret – A secret in bytes
digit – Number of digits in the HOTP code.
algorithm – Hash algorithm used in HOTP.
- classmethod from_b32encode(secret: bytes | str, digit: int = 6, algorithm: Literal['SHA1', 'SHA256', 'SHA512'] = 'SHA1') Self ¶
Create the instance with a base32 encoded secret.
- Parameters:
secret – A base32 encoded secret string or bytes.
digit – Number of digits in the OTP code.
algorithm – Hash algorithm used in HOTP.
- generate(counter: int) int ¶
Generate a HOTP code. The returning result is an integer. To convert it into string with the correct digit length, developers can use
string_code()
:int_code = hotp.generate(4) str_code = hotp.string_code(int_code)
- Parameters:
counter – HOTP is a counter based algorithm.
- string_code(code: int) str ¶
Add leading 0 if the code length does not match the defined length.
For instance, parameter
digit=6
, butcode=123
, this method would return000123
:>>> otp.string_code(123) '000123'
- Parameters:
code – The number that this OTP generated.
- otpauth.generate_hotp(secret: bytes, counter: int, digit: int = 6, algorithm: Literal['SHA1', 'SHA256', 'SHA512'] = 'SHA1') int ¶
Generate a HOTP code.
- Parameters:
secret – A secret token for the authentication.
counter – HOTP is a counter based algorithm.
digit – Number of digits in the HOTP code.
algorithm – Hash algorithm used in HOTP.
TOTP¶
Implementation of RFC6238, Time-Based One-Time Password Algorithm.
- class otpauth.TOTP(secret: bytes, digit: int = 6, algorithm: Literal['SHA1', 'SHA256', 'SHA512'] = 'SHA1', period: int = 30)¶
Implementation of RFC6238, Time-Based One-Time Password Algorithm.
- Parameters:
secret – A secret in bytes
digit – Number of digits in the HOTP code.
algorithm – Hash algorithm used in HOTP.
period – The password valid in “period” seconds.
- classmethod from_b32encode(secret: bytes | str, digit: int = 6, algorithm: Literal['SHA1', 'SHA256', 'SHA512'] = 'SHA1') Self ¶
Create the instance with a base32 encoded secret.
- Parameters:
secret – A base32 encoded secret string or bytes.
digit – Number of digits in the OTP code.
algorithm – Hash algorithm used in HOTP.
- generate(timestamp: int | None = None) int ¶
Generate a TOTP code. The returning result is an integer. To convert it into string with the correct digit length, developers can use
string_code()
:int_code = totp.generate() str_code = totp.string_code(int_code)
- Parameters:
timestamp – Create TOTP at this given timestamp, default is now.
- string_code(code: int) str ¶
Add leading 0 if the code length does not match the defined length.
For instance, parameter
digit=6
, butcode=123
, this method would return000123
:>>> otp.string_code(123) '000123'
- Parameters:
code – The number that this OTP generated.
- otpauth.generate_totp(secret: bytes, period: int = 30, timestamp: int | None = None, digit: int = 6, algorithm: Literal['SHA1', 'SHA256', 'SHA512'] = 'SHA1') int ¶
Generate a TOTP code.
A TOTP code is an extension of TOTP algorithm.
- Parameters:
secret – A secret token for the authentication.
period – A period that a TOTP code is valid in seconds
timestamp – Create TOTP at this given timestamp, default is now.