-
Notifications
You must be signed in to change notification settings - Fork 0
/
algorithm.go
127 lines (108 loc) · 3.04 KB
/
algorithm.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
115
116
117
118
119
120
121
122
123
124
125
126
127
package totp
import (
"strings"
"github.com/pkg/errors"
"github.com/pquerna/otp"
)
// ----------------------------------------------------------------------------
// Type: Algorithm
// ----------------------------------------------------------------------------
// Algorithm is a string that represents the algorithm used for HMAC.
type Algorithm string
// ----------------------------------------------------------------------------
// Constructor
// ----------------------------------------------------------------------------
// NewAlgorithmStr creates a new Algorithm object from a string.
// Choices are: MD5, SHA1, SHA256 and SHA512.
func NewAlgorithmStr(algo string) (Algorithm, error) {
const (
cMD5 = "MD5"
cSHA1 = "SHA1"
cSHA256 = "SHA256"
cSHA512 = "SHA512"
)
algo = strings.ToUpper(algo)
switch algo {
case cMD5, cSHA1, cSHA256, cSHA512:
return Algorithm(algo), nil
}
return "", errors.New("unsupported algorithm. it should be MD5, SHA1, SHA256 or SHA512")
}
// NewAlgorithmID creates a new Algorithm object from an int.
func NewAlgorithmID(algoID int) (Algorithm, error) {
const (
cMD5 = "MD5"
cSHA1 = "SHA1"
cSHA256 = "SHA256"
cSHA512 = "SHA512"
)
switch algoID {
case int(otp.AlgorithmSHA1):
return cSHA1, nil
case int(otp.AlgorithmSHA256):
return cSHA256, nil
case int(otp.AlgorithmSHA512):
return cSHA512, nil
case int(otp.AlgorithmMD5):
return cMD5, nil
}
return "", errors.New("invalid algorithm ID. it should be 0, 1, 2 or 3")
}
// ----------------------------------------------------------------------------
// Methods
// ----------------------------------------------------------------------------
// ID returns the ID of the algorithm which is the same int value as the
// original OTP library.
//
// Undefined ID will always return 2 (SHA512).
func (algo Algorithm) ID() int {
const (
cMD5 = "MD5"
cSHA1 = "SHA1"
cSHA256 = "SHA256"
cSHA512 = "SHA512"
UnsupportedAlgo = -1 // see issue #6
)
switch algo {
case cMD5:
return int(otp.AlgorithmMD5)
case cSHA1:
return int(otp.AlgorithmSHA1)
case cSHA256:
return int(otp.AlgorithmSHA256)
case cSHA512:
return int(otp.AlgorithmSHA512)
default:
return UnsupportedAlgo
}
}
// IsSupported returns true if the algorithm is supported.
func (algo Algorithm) IsSupported() bool {
switch algo {
case "MD5", OptionAlgorithmDefault, "SHA256", "SHA512":
return true
}
return false
}
// OTPAlgorithm is similar to ID() but returns in the original type of the OTP
// library.
//
// Undefined Algorithm type will always return `otp.AlgorithmSHA512`.
func (algo Algorithm) OTPAlgorithm() otp.Algorithm {
switch algo {
case "MD5":
return otp.AlgorithmMD5
case OptionAlgorithmDefault: // SHA1
return otp.AlgorithmSHA1
case "SHA256":
return otp.AlgorithmSHA256
case "SHA512":
return otp.AlgorithmSHA512
default:
return otp.Algorithm(-1) // fix: issue #6
}
}
// String is an implementation of the Stringer interface.
func (algo Algorithm) String() string {
return strings.ToUpper(string(algo))
}