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.

ALGORITHMS: ClassVar[List[str]] = ['SHA1', 'SHA256', 'SHA512']

The supportted algorithms

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, but code=123, this method would return 000123:

>>> otp.string_code(123)
'000123'
Parameters:

code – The number that this OTP generated.

to_uri(label: str, issuer: str, counter: int) str

Generate the otpauth protocal string for HOTP.

Parameters:
  • label – Label of the identifier.

  • issuer – The company, the organization or something else.

  • counter – Initial counter of the HOTP algorithm.

verify(code: int, counter: int) bool

Valid a HOTP code at the given counter.

Parameters:
  • code – A number to be verified.

  • counter – The counter HOTP code based on.

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.

ALGORITHMS: ClassVar[List[str]] = ['SHA1', 'SHA256', 'SHA512']

The supportted algorithms

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, but code=123, this method would return 000123:

>>> otp.string_code(123)
'000123'
Parameters:

code – The number that this OTP generated.

to_uri(label: str, issuer: str) str

Generate the otpauth protocal string for TOTP.

Parameters:
  • label – Label of the identifier.

  • issuer – The company, the organization or something else.

verify(code: int, timestamp: int | None = None) bool

Valid a TOTP code for the given timestamp.

Parameters:
  • code – A number to be verified.

  • timestamp – Validate TOTP at this given timestamp, default is now.

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.