Search code examples
javascriptencryptionwindows-store-appspassword-protectionone-time-password

How to convert a otpauth secret key to an otp pin in JavaScript?


I want to write a JavaScript application like Authy or the Google Authentificator. Sadly I didn’t find any Information how to calculate the secret key to the actual PIN (One-Time-Password) to using it for a Login.

I know, I must use this key with the actual timestamp, but don’t know how to Combine the secret key and the timestamp and then generate the 6 Digit OTP that the Google Authentificator and Authy can generate.

If I activate 2 factor authentification in Google, I get a QR-Code that results in an link like this

otpauth://totp/Google%3Asome.name%40googlemail.com?secret=x6wup537ptmmohbjkczluxit4r6eppfc&issuer=Google

So x6wup537ptmmohbjkczluxit4r6eppfc is the secret key. What must I do, to get an OTP out of this, that actually works with a Google Account to log in?

Best regards an thanks, kaiserkiwi


Solution

  • I came to this question from search results because I was looking for a library which can do what question (from the title) asks. After more searching I found one npm module which can do that - https://www.npmjs.com/package/otplib. If you look at the demo you'll see it gives you a token. From documentation for this module you can see that in order to get pin you can use:

    const token = authenticator.generate(secret);