- 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
- Keep it simple
- 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 :)
- 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.
First install the package
go get github.com/MrTuNNe/GoTOTP
// To generate a random secret you can use this
secret, err := GoTOTP.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 := GoTOTP.TOTP{
Key: "OK6ZZOALZY6RNZBPM4QKD2ZFO5F3PTP56VIAXLDJLEHBPLJJIZNQ",
Issuer: "mrtunne.info"
UserName: "[email protected]"
}
// 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
totp.GenerateURI() // This will generate an URI that can be used for generating a QR code with all the details required for the app to understand.
// Example return:
// otpauth://totp/mrtunne.info:%[email protected]?algorithm=SHA256&digits=6&issuer=mrtunne.info&period=30&secret=OK6ZZOALZY6RNZBPM4QKD2ZFO5F3PTP56VIAXLDJLEHBPLJJIZNQ