Skip to content

Commit

Permalink
fix names
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-mora committed Feb 7, 2024
1 parent 25a2b40 commit c45b953
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 15 deletions.
29 changes: 16 additions & 13 deletions htop.go → hotp.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,47 @@ import (
"net/url"
)

// HTOP represents a Sequence-based One-Time Password generator.
type hopt struct {
// hotp represents a Sequence-based One-Time Password generator.
type hotp struct {
otp OTP
Counter int
}

// HOTPConfig holds configuration parameters for HOTP generation.
type HOTPConfig struct {
CodeLength int
HashType HashType
Secret []byte
Counter int
CodeLength int // CodeLength is the length of the generated OTP code.
HashType HashType // HashType is the hash algorithm used for OTP generation.
Secret []byte // Secret is the shared secret key used for OTP generation.
Counter int // Counter is the initial counter value for HOTP generation.
}

func NewHTOP(config HOTPConfig) *hopt {
return &hopt{
// NewHTOP creates a new instance of hopt based on the provided HOTPConfig.
func NewHTOP(config HOTPConfig) *hotp {
return &hotp{
otp: NewOTP(config.Secret, config.HashType, config.CodeLength),
Counter: config.Counter,
}
}

func (h *hopt) Generate() string {
// Generate generates a HOTP code.
func (h *hotp) Generate() string {
code := h.otp.Generate(h.Counter)
h.Counter = h.Counter + 1
return code
}

func (h *hopt) Validate(input string) bool {
// Validate validates an input OTP code against the current counter value.
func (h *hotp) Validate(input string) bool {
if h.otp.Generate(h.Counter) == input {
h.Counter = h.Counter + 1
return true
}
return false
}

// URI generates the URI for the TOTP according to the Google Authenticator Key URI Format.
// URI generates the URI according to the Google Authenticator Key URI Format.
// See: https://github.com/google/google-authenticator/wiki/Key-Uri-Format
func (t *hopt) URI(label string, issuer string) string {
func (t *hotp) URI(label string, issuer string) string {
// Encode secret in Base32 without padding
encodedSecret := base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(t.otp.secret)

Expand All @@ -52,7 +55,7 @@ func (t *hopt) URI(label string, issuer string) string {
encodedLabel := url.PathEscape(label)

// Construct the URI
return fmt.Sprintf("otpauth://hopt/%s?secret=%s&issuer=%s&algorithm=%s&digits=%d&counter=%d",
return fmt.Sprintf("otpauth://hotp/%s?secret=%s&issuer=%s&algorithm=%s&digits=%d&counter=%d",
encodedLabel,
encodedSecret,
encodedIssuer,
Expand Down
4 changes: 2 additions & 2 deletions htop_test.go → hotp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func TestHTOPValidate(t *testing.T) {
}
}

func TesHOPTURI(t *testing.T) {
func TestHOPTURI(t *testing.T) {
secretKey := []byte("Hello!")

hotpConfig := basicOTP.HOTPConfig{
Expand All @@ -127,7 +127,7 @@ func TesHOPTURI(t *testing.T) {

hopt := basicOTP.NewHTOP(hotpConfig)

expectedURI := "otpauth://totp/TEST:alice@google.com?secret=JBSWY3DPEE&issuer=Example&algorithm=SHA1&digits=4&counter=12"
expectedURI := "otpauth://hotp/TEST:alice@google.com?secret=JBSWY3DPEE&issuer=Example&algorithm=SHA1&digits=4&counter=12"
result := hopt.URI("TEST:alice@google.com", "Example")
if result != expectedURI {
t.Errorf("Expected %s, Got %s", expectedURI, result)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit c45b953

Please sign in to comment.