Add support for SHA256 and SHA512

This commit is contained in:
Hangkun Ung 2022-02-20 19:13:34 -05:00 committed by Pierre Carrier
parent bce7065e62
commit 3709c4a20b
5 changed files with 28 additions and 5 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.DS_STORE

View File

@ -20,6 +20,7 @@ Usage
Airbnb:abcd efgh ijkl mnop
Google:a2b3c4d5e6f7ghij
Github:234567qrstuvwxyz
otpauth://totp/testOrg:testuser?secret=AAAQEAYEAUDAOCAJ======&issuer=testOrg&algorithm=SHA512&digits=8&period=30
- Restrict access to your user:
@ -53,7 +54,7 @@ Encryption
`gauth` will then prompt you for that password on every run:
$ gauth
Encryption password:
Encryption password:
prev curr next
LastPass 915200 479333 408710

View File

@ -6,9 +6,12 @@ import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"errors"
"fmt"
"hash"
"io/ioutil"
"strings"
"time"
@ -24,6 +27,21 @@ func IndexNow() (uint64, int) {
return uint64(time / 30), int(time % 30)
}
// pickAlgorithm returns a constructor for the named hash function, or
// an error if the name is not a supported algorithm.
func pickAlgorithm(name string) (func() hash.Hash, error) {
switch name {
case "", "SHA1":
return sha1.New, nil
case "SHA256":
return sha256.New, nil
case "SHA512":
return sha512.New, nil
default:
return nil, fmt.Errorf("unsupported algorithm: %q", name)
}
}
// Codes returns the previous, current, and next codes from u.
func Codes(u *otpauth.URL) (prev, curr, next string, _ error) {
var ts uint64
@ -40,11 +58,14 @@ func Codes(u *otpauth.URL) (prev, curr, next string, _ error) {
func CodesAtTimeStep(u *otpauth.URL, timeStep uint64) (prev, curr, next string, _ error) {
if u.Type != "totp" {
return "", "", "", fmt.Errorf("unsupported type: %q", u.Type)
} else if u.Algorithm != "" && u.Algorithm != "SHA1" {
return "", "", "", fmt.Errorf("unsupported algorithm: %q", u.Algorithm)
}
cfg := otp.Config{Digits: u.Digits}
alg, err := pickAlgorithm(u.Algorithm)
if err != nil {
return "", "", "", err
}
cfg := otp.Config{Hash: alg, Digits: u.Digits}
if err := cfg.ParseKey(u.RawSecret); err != nil {
return "", "", "", fmt.Errorf("invalid secret: %v", err)
}

Binary file not shown.
Can't render this file because it contains an unexpected character in line 1 and column 54.

View File

@ -1,3 +1,3 @@
test2:AEBAGBAFAYDQQCIK
test1:AAAQEAYEAUDAOCAJ
otpauth://totp/test3:testuser3?secret=AAAQEAYEAUDAOCAJ======&issuer=test3&algorithm=SHA512&digits=8&period=30

1 test2:AEBAGBAFAYDQQCIK
2 test1:AAAQEAYEAUDAOCAJ
3 otpauth://totp/test3:testuser3?secret=AAAQEAYEAUDAOCAJ======&issuer=test3&algorithm=SHA512&digits=8&period=30