HOTP and TOTP-SHA-1 working.

why the frak aren't the SHA-256 and SHA-512 variants working
This commit is contained in:
Kyle Isom
2013-12-19 00:04:26 -07:00
parent dbbd5116b5
commit dc04475120
5 changed files with 209 additions and 62 deletions

51
hotp.go
View File

@@ -3,6 +3,10 @@ package twofactor
import (
"crypto"
"crypto/sha1"
"encoding/base32"
"io"
"net/url"
"strconv"
)
type HOTP struct {
@@ -38,3 +42,50 @@ func (otp *HOTP) URL(label string) string {
func (otp *HOTP) SetProvider(provider string) {
otp.provider = provider
}
func GenerateGoogleHOTP() *HOTP {
key := make([]byte, sha1.Size)
if _, err := io.ReadFull(PRNG, key); err != nil {
return nil
}
return NewHOTP(key, 0, 6)
}
func hotpFromURL(u *url.URL) (*HOTP, string, error) {
label := u.Path[1:]
v := u.Query()
secret := v.Get("secret")
if secret == "" {
return nil, "", ErrInvalidURL
}
var digits = 6
if sdigit := v.Get("digits"); sdigit != "" {
tmpDigits, err := strconv.ParseInt(sdigit, 10, 8)
if err != nil {
return nil, "", err
}
digits = int(tmpDigits)
}
var counter uint64 = 0
if scounter := v.Get("counter"); scounter != "" {
var err error
counter, err = strconv.ParseUint(scounter, 10, 64)
if err != nil {
return nil, "", err
}
}
key, err := base32.StdEncoding.DecodeString(secret)
if err != nil {
return nil, "", err
}
otp := NewHOTP(key, counter, digits)
return otp, label, nil
}
func (otp *HOTP) QR(label string) ([]byte, error) {
return otp.oath.QR(otp.Type(), label)
}