-
Notifications
You must be signed in to change notification settings - Fork 0
/
digits.go
59 lines (47 loc) · 1.58 KB
/
digits.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
package totp
import (
"fmt"
"github.com/pquerna/otp"
)
// ----------------------------------------------------------------------------
// Type: Digits
// ----------------------------------------------------------------------------
// Digits represents the number of digits present in the user's OTP passcode.
// Six and Eight are the most common values.
type Digits uint
const (
// DigitsSix is the default number of digits in a TOTP passcode.
DigitsSix Digits = 6
// DigitsEight is an alternative number of digits in a TOTP passcode.
DigitsEight Digits = 8
)
// ----------------------------------------------------------------------------
// Constructor
// ----------------------------------------------------------------------------
// NewDigitsInt returns a new Digits object from the given value.
func NewDigitsInt(digits int) Digits {
return Digits(uint(digits))
}
// NewDigitsStr returns a new Digits object from the given string in decimal
// format.
func NewDigitsStr(digits string) Digits {
return Digits(StrToUint(digits))
}
// ----------------------------------------------------------------------------
// Methods
// ----------------------------------------------------------------------------
// OTPDigits returns the value in otp.Digits type. Undefined Digits will always
// return otp.DigitsSix.
func (d Digits) OTPDigits() otp.Digits {
switch d {
case DigitsSix:
return otp.DigitsSix
case DigitsEight:
return otp.DigitsEight
}
return otp.DigitsSix
}
// String returns the string representation of the Digits.
func (d Digits) String() string {
return fmt.Sprintf("%d", d)
}