-
Notifications
You must be signed in to change notification settings - Fork 29
/
link_otp.go
43 lines (33 loc) · 998 Bytes
/
link_otp.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
package secretcode
import (
// nolint:gosec
"crypto/md5"
"crypto/subtle"
"fmt"
"strings"
"github.com/authgear/authgear-server/pkg/util/base32"
"github.com/authgear/authgear-server/pkg/util/rand"
)
const linkOTPLength = 32
var LinkOTPSecretCode = LinkOTPSecretCodeType{}
type LinkOTPSecretCodeType struct{}
func (LinkOTPSecretCodeType) Length() int {
return linkOTPLength
}
func (LinkOTPSecretCodeType) Generate() string {
code := rand.StringWithAlphabet(linkOTPLength, base32.Alphabet, rand.SecureRand)
return code
}
func (LinkOTPSecretCodeType) GenerateDeterministic(data string) string {
b := []byte(data)
hash := md5.Sum(b) // nolint:gosec
return fmt.Sprintf("%x", hash)
}
func (LinkOTPSecretCodeType) Compare(a, b string) bool {
formattedCode := strings.TrimSpace(a)
targetCode := strings.TrimSpace(b)
return subtle.ConstantTimeCompare([]byte(formattedCode), []byte(targetCode)) == 1
}
func (LinkOTPSecretCodeType) CheckFormat(value interface{}) error {
return nil
}