Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fulygon/go-totp
Time-Based One-Time Password (TOTP) library for Go
https://github.com/fulygon/go-totp
go totp
Last synced: 10 days ago
JSON representation
Time-Based One-Time Password (TOTP) library for Go
- Host: GitHub
- URL: https://github.com/fulygon/go-totp
- Owner: FuLygon
- License: mit
- Created: 2024-03-30T12:09:56.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-05-31T06:56:49.000Z (7 months ago)
- Last Synced: 2024-06-20T15:06:38.325Z (7 months ago)
- Topics: go, totp
- Language: Go
- Homepage:
- Size: 49.8 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-totp
[![Github tag](https://badgen.net/github/tag/FuLygon/go-totp)](https://github.com/FuLygon/go-totp/tags)
[![GoReportCard](https://goreportcard.com/badge/github.com/FuLygon/go-totp/v2)](https://goreportcard.com/report/github.com/FuLygon/go-totp/v2)Package `go-totp` library implements functionalities to create and validate Time-Based One-Time Password (TOTP) for Two-Factor Authentication (2FA) applications.
TOTP generates temporary codes based on a shared secret key, enhancing security.
## Installation
Use `go get`
```shell
go get -u github.com/FuLygon/go-totp/v2
```
Import package
```go
import "github.com/FuLygon/go-totp/v2"
```## Documentation
[![GoDoc](https://godoc.org/github.com/FuLygon/go-totp/v2?status.svg)](https://pkg.go.dev/github.com/FuLygon/go-totp/v2#section-documentation)## Example
See [Example](example/main.go)## Usage
### Create TOTP
#### Generate or define a TOTP instance
```go
t, err := totp.New(totp.TOTP{
AccountName: "your_account_name",
Issuer: "your_issuer_name",
})
if err != nil {
// handle error
log.Println("error generating QR code:", err)
return
}// optionally, define TOTP details:
t := totp.TOTP{
AccountName: "your_account_name",
Issuer: "your_issuer_name",
Algorithm: totp.AlgorithmSHA1,
Digits: 6,
Period: 30,
Secret: "your_shared_secret",
}
```#### Generate TOTP URL and QR code
```go
// generate TOTP URL
url, err := t.GetURL()
if err != nil {
// handle error
log.Println("error generating TOTP URL:", err)
return
}
fmt.Println("TOTP URL:", url)// generate QR code
qr, err := t.GetQR(256)
if err != nil {
// handle error
log.Println("error generating QR code:", err)
return
}
fmt.Println("QR Code Base64:", qr.Base64)
```### Validating TOTP code
#### Create a validator instance
```go
v := totp.Validator{
Algorithm: totp.AlgorithmSHA1,
Digits: 6,
Period: 30,
Secret: "your_shared_secret",
}
```#### Validate TOTP code
```go
code := "123456" // user-provided TOTP codevalid, err := v.Validate(code)
if err != nil {
// handle error
log.Println("error validating TOTP code:", err)
return
}if valid {
fmt.Println("TOTP code is valid!")
} else {
fmt.Println("TOTP code is invalid.")
}
```