
A simple Time Based One-Time Password library
- This library is created with the focus on simplicity and stable code.
- This library will not change much once it will reach maturity.
- While other implementations already exists, this project is mostly created because I was bored of building only websites :)
- This library will try as much possible not to reinvent the wheel. If there is an implementation in the standard library, that implementation will be used. (I've seen in some other libraries this weird behavior of reinventing the wheel)
This implementation is based on RFC 6238
Project Goals & Roadmap
- Keep it simple
- Create a way of generating random secret keys (will be made soon)
- Implementing a method of generating the QR code for Google Authenticator and other similar apps like this.
- Keep it rock solid for years to come :)
Other things to keep in mind
- The default behavior is to generate a code that will expire in 30 seconds
- The generated code will have 6 numbers.
Maybe in the future I'm going to make it in a way to control this yourself, however this is not a priority.
Code Examples
First install the package
go get github.com/MrTuNNe/GoTOTP`
// To generate a random secret you can use this
secret, err := totp.GenerateRandomSecret(32) // the length of the secret key
if err != nil {
// handle the error
}
// The secret key must be base32 encoded and without padding
totp := totp.TOTP{Key: "OK6ZZOALZY6RNZBPM4QKD2ZFO5F3PTP56VIAXLDJLEHBPLJJIZNQ"}
// generate the TOTP with the timestamp
totp.GenerateTOTP(time.Now().Unix())
if totp.Verify("149425") { // check the input code from the user
// do something if the code is good
}
// you can also check a code with a timestamp
totp.VerifyWithTimestamp(1723719527, "611626") // this will return true
totp.VerifyWithTimestamp(1723719580, "611626") // this will return false