@sunknudsen/totp
v1.2.0
Published
Generate and validate TOTP tokens.
Downloads
46,625
Readme
@sunknudsen/totp
Generate and validate TOTP tokens.
@sunknudsen/totp has zero dependencies and 100% unit test code coverage.
Package is used in production by Superbacked and many others.
Installation
$ npm install @sunknudsen/totp
Usage
import {
generateSecret,
generateUri,
generateToken,
validateToken,
} from "@sunknudsen/totp"
const secret = generateSecret()
console.log(secret)
// DMJKP7AU22WKWRG3DNIQ3ERA
const uri = generateUri(
"Superbacked",
"[email protected]",
"DMJKP7AU22WKWRG3DNIQ3ERA",
"Superbacked"
)
console.log(uri)
// otpauth://totp/Superbacked:john%40protonmail.com?secret=DMJKP7AU22WKWRG3DNIQ3ERA&issuer=Superbacked&algorithm=SHA1&digits=6&period=30
const token = generateToken("DMJKP7AU22WKWRG3DNIQ3ERA")
console.log(token)
// 616692
const result = validateToken("DMJKP7AU22WKWRG3DNIQ3ERA", "616692")
console.log(result)
// true