Coders Packet

OTP generator in C++

By Chandan Giri

It is a C++ based OTP generator which uses random function to create random OTPs which is very secure as traditional passwords for logging into any site.

OTP also known as one time password which is a very secure way of logging in into any site. It is C++ based OTP generator which uses rand()  function and pre-defined string to create a 6-7 length random string or OTP. The code is :

 

#include <bits/stdc++.h> 
#include 
#include 
#include 
#include 
#include
using namespace std; 
   
string generateOTP(int len) 
{ 
    
    string str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKL"
               "MNOPQRSTUVWXYZ0123456789"; 
    int n = str.length(); 
    
    string OTP; 
   
    for (int index=1; index<=len; index++) 
        OTP.push_back(str[rand() % n]); 
   
    return(OTP); 
} 
   
 
int main() 
{ 
    srand(time(NULL)); 
    
    int len = 6; 
    char ch='y';
    while(ch=='y' || ch=='Y'){
    cout << "Your Generated OTP is :- "<<generateOTP(len).c_str()<<endl; 
    cout<<"--Regenerate OTP--\nY/N : ";
    cin>>ch;
    }
    system("pause");
    return 0; 
} 

 

Download project

Reviews Report

Submitted by Chandan Giri (chandan017)

Download packets of source code on Coders Packet