catpcha.go 939 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package utl
  2. import (
  3. "github.com/mojocn/base64Captcha"
  4. )
  5. var (
  6. store = base64Captcha.DefaultMemStore
  7. )
  8. // Used for generating and verify the digital captcha
  9. type DigitCaptcha struct {
  10. h *base64Captcha.Captcha
  11. }
  12. // Initializer. length is the length of the digitals
  13. func NewDigitCatpcha(length int) *DigitCaptcha {
  14. driver := &base64Captcha.DriverDigit{
  15. Height: 80,
  16. Width: 240,
  17. Length: length,
  18. MaxSkew: 0.7,
  19. DotCount: 80,
  20. }
  21. return &DigitCaptcha{
  22. h: base64Captcha.NewCaptcha(driver, store),
  23. }
  24. }
  25. // Generate a new captcha image
  26. //
  27. // return
  28. // id: is the id of the new captcha image
  29. // b64: captcha image in base64 format
  30. // err: any error
  31. func (self *DigitCaptcha) Gen() (id, b64 string, err error) {
  32. id, b64, err = self.h.Generate()
  33. return
  34. }
  35. // Verify if the user input is the same as digits in captcha.
  36. func (self *DigitCaptcha) Verify(id, value string) bool {
  37. return store.Verify(id, value, true)
  38. }