HOTP and TOTP-SHA-1 working.
why the frak aren't the SHA-256 and SHA-512 variants working
This commit is contained in:
29
otp.go
29
otp.go
@@ -2,8 +2,10 @@ package twofactor
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"errors"
|
||||
"fmt"
|
||||
"hash"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
type Type uint
|
||||
@@ -13,8 +15,15 @@ const (
|
||||
OATH_TOTP
|
||||
)
|
||||
|
||||
// PRNG is an io.Reader that provides a cryptographically secure
|
||||
// random byte stream.
|
||||
var PRNG = rand.Reader
|
||||
|
||||
var (
|
||||
ErrInvalidURL = errors.New("twofactor: invalid URL")
|
||||
ErrInvalidAlgo = errors.New("twofactor: invalid algorithm")
|
||||
)
|
||||
|
||||
// Type OTP represents a one-time password token -- whether a
|
||||
// software taken (as in the case of Google Authenticator) or a
|
||||
// hardware token (as in the case of a YubiKey).
|
||||
@@ -59,3 +68,23 @@ func OTPString(otp OTP) string {
|
||||
}
|
||||
return fmt.Sprintf("%s, %d", typeName, otp.Size())
|
||||
}
|
||||
|
||||
func FromURL(URL string) (OTP, string, error) {
|
||||
u, err := url.Parse(URL)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
if u.Scheme != "otpauth" {
|
||||
return nil, "", ErrInvalidURL
|
||||
}
|
||||
|
||||
switch {
|
||||
case u.Host == "totp":
|
||||
return totpFromURL(u)
|
||||
case u.Host == "hotp":
|
||||
return hotpFromURL(u)
|
||||
default:
|
||||
return nil, "", ErrInvalidURL
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user