In our previous blog we talk about 2-Factor authenticator app. So, I thought let know about the algorithm it work on.
The TOTP algorithm generate single time password also known
as token which are only valid for a certain period of time.
This generated token are depend upon shared secret key.
This generated token are depend upon shared secret key.
This algorithm was published as RFC 6238 by Internet
Engineering Task Force (IETF).
TOTP is extension of HMAC based OTP algorithm.
This algorithm is used by 2-factor authentication app for creating dynamic pattern of code.
Without any further due let’s hope into it.
Before getting started let me introduce you to a term called "Unix Epoch Time".
Unix Epoch Time (T(Unix)) is the m=number of second that elapsed(passed) since 1 Jan 2017
00:00:00 UTC not counting leap second.
TOTP Algorithm:
1. Assume a date and time at which you want the code. So, I
take 4 Dec 2018 12:24:20
T(Unix)=1543926260 Sec
N=floor (T(Unix)/sec)
N=counter which tell the amount of time gap has completed. Here
it is 30 sec.
Sec=30 (we want to
change the code every 30 second)
floor=function which round a number downward to nearest integer.
According to the formula:
N=floor (1543926260/30)
N=51464208
2. N is then converted into hexadecimal value having 16 hexadecimal
characters (8 Byte)
If not, prepend with 0’s. (if the number is not that big add
0 on the right side to make 8 byte number)
N(Dec)=51464208
N(Hex)=0x0000000003114810
Convert the hexadecimal vale into 8-byte array and assign it
to msg.
3.You get shared secret key from the account you are doing
2-factor authentication.
K= (pofa rscd uptm dh6j aogl nda2 ryk7 7jva)
Everyone has different key.
Convert this key into 20-byte number using 32-base encode.
4.This both values are passed in HMAC-SHAI algorithm.
From this algorithm we get this output.
Now, pick the last 4-bits to this output. In my case it is A.
Here each block has 8-bit so dividing it 9 and A are of
4-bit each.
A=10 in hexadecimal format.
Starting from offset(A=10),get 4 bytes from the HMAC hash.
Convert this output in to binary and then convert it into decimal.
New binary values=0x76A7F899
Binary à
Decimal= 1990719641.
Main=1990719641.
5.Finally, the movement we are waiting for the token, which we
require to login in our account in created.
Final calculation
The value we got i.e. main is used in modulo operation.
Token = main % (10^n)
Here n= size of token
(generally it sizes of 6).
Note:If the token (size < n)then, prefix with 0's.
Every 30 second a new token is generated with this algorithm.
Fun Fact: It remain valid for 60 seconds.
If you have can Query feel free to comment in blog.I will be grateful to answer your doubt.
If you have can Query feel free to comment in blog.I will be grateful to answer your doubt.
If like the content share it with your friend.
And don’t forget to subscribe our blog which on top of the page.
Comments
Post a Comment