Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add issuer parsing and SteamGuard encoding #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions oath.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type OATH struct {
hash func() hash.Hash
algo crypto.Hash
provider string
issuer string
}

// Size returns the output size (in characters) of the password.
Expand Down Expand Up @@ -82,6 +83,10 @@ func (o OATH) URL(t Type, label string) string {
v.Add("provider", o.provider)
}

if o.issuer != "" {
v.Add("issuer", o.issuer)
}

u.RawQuery = v.Encode()
return u.String()

Expand Down Expand Up @@ -119,9 +124,24 @@ func (o OATH) OTP(counter uint64) string {

h := hmac.New(o.hash, o.key)
h.Write(ctr[:])
dt := truncate(h.Sum(nil)) % mod
fmtStr := fmt.Sprintf("%%0%dd", o.size)
return fmt.Sprintf(fmtStr, dt)

if o.issuer == "Steam" {
res := truncate(h.Sum(nil))

steamChars := "23456789BCDFGHJKMNPQRTVWXY"
steamCharsLen := int64(len(steamChars))
hotp := make([]byte, 5, 5)
for i := 0; i < 5; i++ {
idx := res % steamCharsLen
hotp[i] = steamChars[int(idx)]
res = res / steamCharsLen
}
return string(hotp)
} else {
dt := truncate(h.Sum(nil)) % mod
fmtStr := fmt.Sprintf("%%0%dd", o.size)
return fmt.Sprintf(fmtStr, dt)
}
}

// truncate contains the DT function from the RFC; this is used to
Expand Down
6 changes: 6 additions & 0 deletions totp.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,18 @@ func totpFromURL(u *url.URL) (*TOTP, string, error) {
}
}

var issuer = ""
if sissuer := v.Get("issuer"); sissuer != "" {
issuer = sissuer
}

key, err := base32.StdEncoding.DecodeString(Pad(secret))
if err != nil {
// assume secret isn't base32 encoded
key = []byte(secret)
}
otp := NewTOTP(key, 0, period, digits, algo)
otp.issuer = issuer
return otp, label, nil
}

Expand Down