Skip to content

Authenticator

Authenticator

Authenticator class which generates unique one time use password.

__init__(self, secret) special

Creates a new Authenticator instance.

Parameters:

Name Type Description Default
secret str

User secret which is used in generating one time password.

required

Returns:

Type Description

Authenticator instance.

Source code in authenticatorpy/authenticator.py
14
15
16
17
18
19
20
21
22
23
24
25
def __init__(self, secret: str):
    """Creates a new Authenticator instance.
    Args:
      secret (str):
        User secret which is used in generating one
        time password.
    Returns:
      Authenticator instance.
    """

    self._secret = secret
    self.__check_secret(secret)

create_hmac(self, secret, input)

Creates the hash value which is used in creating one time password.

Parameters:

Name Type Description Default
secret str

User secret which is used in generating one time password.

required
input float

The value of current UNIX time divided by 30.

required

Returns:

Type Description
str

SHA1 hash value.

Source code in authenticatorpy/authenticator.py
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
def create_hmac(self, secret: str, input: float) -> str:
    """Creates the hash value which is used in creating one time password.
    Args:
      secret (str):
        User secret which is used in generating one
        time password.
      input (float):
        The value of current UNIX time divided by 30.
    Returns:
      SHA1 hash value.
    """

    input_str = repr(input).encode("ascii")
    input_hash = hashlib.sha1(secret + input_str).hexdigest().encode("ascii")
    return hashlib.sha1(secret + input_hash).hexdigest()

current_timestamp(self)

Returns the current UNIX time.

Source code in authenticatorpy/authenticator.py
87
88
89
90
def current_timestamp(self) -> time:
    """Returns the current UNIX time."""

    return time.time()

decode_with_base32(self, upper_case_secret)

Creates a new Base32 decoded value from given string.

Parameters:

Name Type Description Default
upper_case_secret str

User secret which is used in generating one time password.

required

Returns:

Type Description
bytes

Base32 decoded value.

Source code in authenticatorpy/authenticator.py
75
76
77
78
79
80
81
82
83
84
85
def decode_with_base32(self, upper_case_secret: str) -> bytes:
    """Creates a new Base32 decoded value from given string.
    Args:
      upper_case_secret (str):
        User secret which is used in generating one
        time password.
    Returns:
      Base32 decoded value.
    """

    return base64.b32decode(upper_case_secret)

one_time_password(self, delay_time=30.0)

Creates one time password using secret which must be set in constructor.

Parameters:

Name Type Description Default
delay_time float

Optional time interval for token availability.

30.0

Returns:

Type Description
str

One time password as string.

Source code in authenticatorpy/authenticator.py
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
def one_time_password(self, delay_time: float = 30.0) -> str:
    """Creates one time password using secret which must be set in constructor.
    Args:
      delay_time (float):
        Optional time interval for token availability.
    Returns:
      One time password as string.
    """

    secret_without_spaces = self.remove_spaces(self._secret)
    upper_case_secret = self.to_upper_case(secret_without_spaces)
    secret = self.decode_with_base32(upper_case_secret)
    input = self.current_timestamp() / delay_time
    hmac = self.create_hmac(secret, input)
    offset = ord(hmac[len(hmac) - 1]) & 0x0F
    hex_four_characters = binascii.hexlify(hmac[offset : offset + 4].encode())
    password = int(hex_four_characters, 32) % 1000000
    return password

remove_spaces(self, secret)

Removes empty spaces from given string.

Parameters:

Name Type Description Default
secret str

User secret which is used in generating one time password.

required

Returns:

Type Description
str

String without empty spaces.

Source code in authenticatorpy/authenticator.py
49
50
51
52
53
54
55
56
57
58
59
60
61
def remove_spaces(self, secret: str) -> str:
    """Removes empty spaces from given string.
    Args:
      secret (str):
        User secret which is used in generating one
        time password.
    Returns:
      String without empty spaces.
    """

    secret_without_spaces = secret.replace(" ", "")
    secret_without_spaces = re.sub(r"\W", "", secret_without_spaces)
    return secret_without_spaces

to_upper_case(self, secret_without_spaces)

Updates given string to uppercase without changing.

Parameters:

Name Type Description Default
secret_without_spaces str

User secret which is used in generating one time password.

required

Returns:

Type Description
str

String in uppercase.

Source code in authenticatorpy/authenticator.py
63
64
65
66
67
68
69
70
71
72
73
def to_upper_case(self, secret_without_spaces: str) -> str:
    """Updates given string to uppercase without changing.
    Args:
      secret_without_spaces (str):
        User secret which is used in generating one
        time password.
    Returns:
      String in uppercase.
    """

    return secret_without_spaces.upper()