-
Notifications
You must be signed in to change notification settings - Fork 0
/
doc.go
114 lines (92 loc) · 3.01 KB
/
doc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
Package otp provides an implementation of one-time password (OTP) generation using TOTP, HOTP, and OCRA algorithms.
The OTP generation is based on the following RFCs:
- RFC 4226: HOTP: An HMAC-Based One-Time Password Algorithm
- RFC 6238: TOTP: Time-Based One-Time Password Algorithm
- RFC 6287: OCRA: OATH Challenge-Response Algorithm
## Overview
This package contains structures and methods for generating OTPs using different algorithms:
1. **TOTP** (Time-based One-Time Password)
2. **HOTP** (HMAC-based One-Time Password)
3. **OCRA** (OATH Challenge-Response Algorithm)
## Usage
To use the package, create a configuration for the desired OTP type, set the necessary parameters, and call the Generate method to obtain the OTP.
### Example
package main
import (
"fmt"
"log"
"github.com/hasan-kilici/otp/totp"
"github.com/hasan-kilici/otp/ocra"
"github.com/hasan-kilici/otp/hotp"
"github.com/hasan-kilici/otp/utils"
)
func main() {
// TOTP Example
totpConfig := totp.Config{
Secret: "JBSWY3DPEHPK3PXP",
TimeStep: 30,
Digits: 6,
Algorithm: utils.AlgorithmSHA1,
}
totpOptions := totp.Options{
Issuer: utils.DefaultIssuer,
AccountName: utils.DefaultAccount,
}
totpInstance := totp.New(totpConfig)
totpCode, err := totpInstance.Generate(totpOptions)
if err != nil {
log.Fatalf("TOTP Error: %v", err)
}
fmt.Println("TOTP Code:", totpCode)
// HOTP Example
hotpConfig := hotp.Config{
Secret: "JBSWY3DPEHPK3PXP",
Counter: 1,
Digits: 6,
Algorithm: utils.AlgorithmSHA256,
}
hotpOptions := hotp.Options{
Issuer: utils.DefaultIssuer,
AccountName: utils.DefaultAccount,
}
hotpInstance := hotp.New(hotpConfig)
hotpCode, err := hotpInstance.Generate(hotpOptions)
if err != nil {
log.Fatalf("HOTP Error: %v", err)
}
fmt.Println("HOTP Code:", hotpCode)
// OCRA Example
ocraConfig := ocra.Config{
SecretKey: "JBSWY3DPEHPK3PXP",
Suite: "OCRA-1",
Counter: "1",
Algorithm: utils.AlgorithmSHA512,
}
ocraOptions := ocra.Options{
Issuer: utils.DefaultIssuer,
AccountName: utils.DefaultAccount,
}
ocraInstance := ocra.New(ocraConfig)
ocraCode, err := ocraInstance.Generate(ocraOptions)
if err != nil {
log.Fatalf("OCRA Error: %v", err)
}
fmt.Println("OCRA Code:", ocraCode)
}
## Structures
### Config
The Config structure contains the settings necessary for OTP generation:
- Secret: The shared secret used for generating OTPs.
- TimeStep: The time interval for TOTP (in seconds).
- Counter: The counter value for HOTP.
- Digits: The number of digits in the generated OTP.
- Algorithm: The hash algorithm used (SHA1, SHA256, SHA512).
### Options
The Options structure contains optional parameters for OTP generation:
- Issuer: The name of the application issuing the OTP.
- AccountName: The account name for which the OTP is generated.
## License
This project is licensed under the MIT License. See the LICENSE file for more details.
*/
package otp