HOW TO GENERATE ONE TIME PASSWORD(OTP)USING PYTHON
I am going to be talking about how I generated my one time password using python in this article.
But first, I will like to give special thanks to SCAUNILAG for the opportunity to learn for free , I learnt python and I worked on a project; OTP generator.
Basically, OTP is used in lots of works like resetting password, email verification, mobile verification among others.
The code was written and ran using python 3.8 IDLE. To get started, we need to have python installed on the PC or any other code editor we prefer. However, I used python’s IDLE because i am more comfortable using it.
We create a new file in the IDLE and name it “otp.py”.The file has to have a ‘.py’ extension.
Below is the steps taken to get the 6-digit numeric OTP.
STEPS:
1. Import the required libraries:
In this case, we are using a math library, so we import the math library. Then we use the python’s random function to generate random numbers and this number will be used to generate the OTP.
2.Define the function:
Here the “def” keyword is used. It basically defines the function to be worked on. It’s like telling python that we want to start something so it should get ready. Since we are generating a numeric OTP, our function will be digits only. So we call our function “digits” and make it numbers 0–9 inclusive.
3. We now use “for” loop to randomly choose value position in the function and multiply it by the length. In this case, length is 10, which is the numbers between 0–9 inclusive as defined in our function. To tell Python the number of digits we want in our OTP, we enter 6 in the range. A screenshot showing the code is shown below:
To run the code, we save the file using the ‘ctrl + s’ and use the ‘f5’ keyword. The result is shown in the python shell. If there were errors in the code, python returns an error message and include type of error and what line error is.
The result when the code was run using “f5” after having the file saved is attached below:
WRITTEN BY