45 lines
697 B
Go
45 lines
697 B
Go
package captcha
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"image"
|
|
"math/big"
|
|
"time"
|
|
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
const (
|
|
maxAnswer = 999999
|
|
)
|
|
|
|
var errorNotFound = errors.New("captcha not found")
|
|
|
|
type ID string
|
|
type Answer string
|
|
|
|
type ICaptcha interface {
|
|
GenerateImage() *image.Image
|
|
GetImage() *image.Image
|
|
GetAnswer() Answer
|
|
Solve(answer Answer) bool
|
|
IsSolved() bool
|
|
Expire() time.Time
|
|
}
|
|
|
|
type Captcha struct {
|
|
Image image.Image
|
|
Answer Answer
|
|
Solved bool
|
|
ExpireIn time.Time
|
|
}
|
|
|
|
func GenerateAnswer() Answer {
|
|
ans, _ := rand.Int(rand.Reader, big.NewInt(maxAnswer))
|
|
return (Answer(ans.String()))
|
|
}
|
|
|
|
func ExpireDate(expiration time.Duration) time.Time {
|
|
return time.Now().Add(expiration)
|
|
}
|