TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Genarate One Time Password String
Vishal Jadhav
Sep 14
2014
Code
4.4
k
0
4
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
public
static
string
TOTPGenerator(
string
uniqueIdentity)
{
string
oneTimePassword =
""
;
DateTime dateTime = DateTime.Now;
string
_strParsedReqNo = dateTime.Day.ToString();
_strParsedReqNo = _strParsedReqNo + dateTime.Month.ToString();
_strParsedReqNo = _strParsedReqNo + dateTime.Year.ToString();
_strParsedReqNo = _strParsedReqNo + dateTime.Hour.ToString();
_strParsedReqNo = _strParsedReqNo + dateTime.Minute.ToString();
_strParsedReqNo = _strParsedReqNo + dateTime.Second.ToString();
_strParsedReqNo = _strParsedReqNo + dateTime.Millisecond.ToString();
_strParsedReqNo = _strParsedReqNo + uniqueCustomerIdentity;
//Append above string with Policy number to get desired output.
Console.WriteLine(
"TOTP value: "
+ _strParsedReqNo);
using
(MD5 md5 = MD5.Create())
{
//Get hash code of entered request id in byte format.
byte
[] _reqByte = md5.ComputeHash(Encoding.UTF8.GetBytes(_strParsedReqNo));
//convert byte array to integer.
int
_parsedReqNo = BitConverter.ToInt32(_reqByte, 0);
string
_strParsedReqId = Math.Abs(_parsedReqNo).ToString();
//Check if length of hash code is less than 9.
//If so, then prepend multiple zeros upto the length becomes atleast 9 characters.
if
(_strParsedReqId.Length < 9)
{
StringBuilder sb =
new
StringBuilder(_strParsedReqId);
for
(
int
k = 0; k < (9 - _strParsedReqId.Length); k++)
{
sb.Insert(0,
'0'
);
}
_strParsedReqId = sb.ToString();
}
oneTimePassword = _strParsedReqId;
}
//Adding random letters to the OTP.
StringBuilder builder =
new
StringBuilder();
Random random =
new
Random();
string
randomString =
""
;
for
(
int
i = 0; i < 4; i++)
{
randomString += Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
}
//Deciding number of characters in OTP.
Random ran =
new
Random();
int
randomNumber = ran.Next(2, 5);
Random num =
new
Random();
//Form alphanumeric OTP and rearrange it reandomly.
string
otpString = randomString.Substring(0, randomNumber);
otpString += oneTimePassword.Substring(0, 7 - randomNumber);
oneTimePassword =
new
string
(otpString.ToCharArray().OrderBy(s => (num.Next(2) % 2) == 0).ToArray());
return
oneTimePassword;
}
code for OTP string
Generate OTP string C#
learn C#