A function for generating an answer was transformed into a constructor with specified type of integer in its name. maxAnswer constant renamed to maxIntAnswer.

This commit is contained in:
Alexander Andreev 2022-08-17 21:46:10 +04:00
parent d7a04516e3
commit 18431e0001
Signed by: Arav
GPG Key ID: 0388CC8FAA51063F

View File

@ -8,11 +8,16 @@ import (
) )
const ( const (
maxAnswer = 999999 maxIntAnswer = 999999
) )
type Answer string type Answer string
func NewIntAnswer() Answer {
ans, _ := rand.Int(rand.Reader, big.NewInt(maxIntAnswer))
return (Answer(ans.String()))
}
// Captcha interface that should be implemented by captcha. // Captcha interface that should be implemented by captcha.
type Captcha interface { type Captcha interface {
// Image generates and returns an image of captcha. // Image generates and returns an image of captcha.
@ -54,7 +59,7 @@ func (c *BaseCaptcha) Solve(answer Answer) bool {
func (c *BaseCaptcha) GetAnswer() Answer { func (c *BaseCaptcha) GetAnswer() Answer {
if c.Answer == "" { if c.Answer == "" {
c.Answer = generateAnswer() c.Answer = NewIntAnswer()
} }
return c.Answer return c.Answer
} }
@ -67,11 +72,6 @@ func (c *BaseCaptcha) Expiry() time.Time {
return c.ExpireIn return c.ExpireIn
} }
func generateAnswer() Answer {
ans, _ := rand.Int(rand.Reader, big.NewInt(maxAnswer))
return (Answer(ans.String()))
}
func ExpiryDate(expiry time.Duration) time.Time { func ExpiryDate(expiry time.Duration) time.Time {
return time.Now().Add(expiry) return time.Now().Add(expiry)
} }