cover-img

πŸ›‘οΈ2FA with Python.

19 January, 2022

2

2

0

What is 2FA and why 2FA?πŸ€”

2FA stands for two factor authentication. It adds an extra layer of security other than password. The user must enter a 2FA code along with password in order to sign in. 2FA codes can be generated in two ways, time based codes and counter based codes.

Advantages of 2FA over E-Mail or SMS verification:

  • No network required: 2FA codes can be generated offline.
  • πŸ›‘οΈ Better security.

Time based codes vs counter based code.

Time based codesCounter based codes
Time based codes changes depending on time. πŸ•–Counter based codes change depending on number of successful sign-in(s). βœ”οΈ
No need of adding counter every time in client side.After every successful login, counter must be increased by one in server side as well as client side.

2FA flowchart

2FA with Python

Requirements

  • onetimepass python package (Can be installed using the command: pip install onetimepass).
  • Your favourite authenticator app (Example: Google authenticator, Microsoft authenticator).

Let's start!πŸ‘€

For both time based codes and counter based code, a secret string is securely shared with the authenticator app while setting up 2FA. All codes are generated based on this secret string. This string is not case sensitive.

πŸ•–Time based codes

Let us now, write a simple Python script to understand how time based 2FA works!


βœ”οΈ Counter based codes

Here is a complete Python script to understand how counter based 2FA works!


Thank you! Leave a comment and a like if you find this article useful :-)

About me

Jothin kumar

python

tutorials

tutorial

security

web-security

2

2

0

python

tutorials

tutorial

security

web-security

Jothin Kumar
Pythonista || Open source enthusiast.

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

Β© Copyright 2025. Showcase Creators Inc. All rights reserved.