Skip to content

Commit

Permalink
fix: #33 feature functional option pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
KEINOS committed Apr 25, 2024
1 parent 9c7f256 commit 47ea7c9
Show file tree
Hide file tree
Showing 8 changed files with 301 additions and 80 deletions.
8 changes: 4 additions & 4 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
run:
tests: true
fast: true
build-tags:
- golangci
skip-dirs:
allow-parallel-runners: true

issues:
exclude-dirs:
- .github
- .vscode
allow-parallel-runners: true

output:
format: colored-line-number
sort-results: true

linters:
Expand Down
46 changes: 31 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,20 @@
## Usage

```go
// Install package
go get "github.com/KEINOS/go-totp"
```

```go
import (
"fmt"
"log"

"github.com/KEINOS/go-totp/totp"
)
// import "github.com/KEINOS/go-totp/totp"

func Example() {
// Generate a new secret key
// Generate a new secret key with default options:
// Algorithm: SHA1
// Period: 30
// Secret Size: 128
// Skew: 0
// Digits: 6
Issuer := "Example.com"
AccountName := "[email protected]"

Expand All @@ -43,29 +44,41 @@ func Example() {
if key.Validate(passcode) {
fmt.Println("Passcode is valid")
}

//
// Output: Passcode is valid
}
```

- [View it online](https://go.dev/play/p/s7bAGoLY25R) @ Go Playground

```go
// Generate a secret for TOTP (totp.Key object).
key, err := totp.GenerateKey(Issuer, AccountName)
// --------------------------------------------------
// Generate a new secret key with custom options
// --------------------------------------------------
key, err := totp.GenerateKey(Issuer, AccountName,
totp.WithAlgorithm(totp.Algorithm("SHA256")),
totp.WithPeriod(15),
totp.WithSecretSize(256),
totp.WithSkew(5),
totp.WithDigits(totp.DigitsEight),
)

// --------------------------------------------------
// Basic methods of totp.Key object
// Methods of totp.Key object
// --------------------------------------------------

// Generate the current passcode. Which is a string of
// 6 digit numbers and valid for 30 seconds by default.
// Generate the current passcode.
//
// Which is a string of 8 digit numbers and valid for
// 15 seconds with ±5 seconds skew/tolerance (as set
// in the above example).
passcode, err := key.PassCode()

// Validate the received passcode.
ok := key.Validate(passcode)

// Get 100x100 px image of QR code as PNG byte data.
//
// FixLevelDefault is the 15% of error correction.
qrCodeObj, err := key.QRCode(totp.FixLevelDefault)
pngBytes, err := qrCodeObj.PNG(100, 100)
Expand All @@ -81,6 +94,9 @@ base32Key := key.Secret.Base32()

// Get the secret value in Base62 format string.
base62Key := key.Secret.Base62()

// Get the secret value in bytes.
rawKey := key.Secret.Bytes()
```

- [View __more examples__ and advanced usages](https://pkg.go.dev/github.com/KEINOS/go-totp/totp#pkg-examples) @ pkg.go.dev
Expand Down Expand Up @@ -111,10 +127,10 @@ Any Pull-Request for improvement is welcome!
- [CIs](https://github.com/KEINOS/go-totp/actions) on PR/Push: `unit-tests` `golangci-lint` `codeQL-analysis` `platform-tests`
- [Security policy](https://github.com/KEINOS/go-totp/security/policy)
- Help wanted
- https://github.com/KEINOS/go-totp/issues
- [https://github.com/KEINOS/go-totp/issues](https://github.com/KEINOS/go-totp/issues)

## License, copyright and credits

- MIT, Copyright (c) 2022 [The go-totp contributors](https://github.com/KEINOS/go-totp/graphs/contributors).
- MIT, Copyright (c) 2022- [The go-totp contributors](https://github.com/KEINOS/go-totp/graphs/contributors).
- This Go package relies heavily on support from the `github.com/pquerna/otp` package.
- [https://github.com/pquerna/otp](https://github.com/pquerna/otp) with [Apache-2.0 license](https://github.com/pquerna/otp/blob/master/LICENSE)
2 changes: 1 addition & 1 deletion totp/algorithm.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Algorithm string
// ----------------------------------------------------------------------------

// NewAlgorithmStr creates a new Algorithm object from a string.
// Choices are: MD5, SHA1, SHA256 and SHA512.
// Choices of algo are: MD5, SHA1, SHA256 and SHA512.
func NewAlgorithmStr(algo string) (Algorithm, error) {
const (
cMD5 = "MD5"
Expand Down
Loading

0 comments on commit 47ea7c9

Please sign in to comment.