Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update enable private key #96

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ type Driver interface {
//DrawCaptcha draws binary item
DrawCaptcha(content string) (item Item, err error)
//GenerateIdQuestionAnswer creates rand id, content and answer
GenerateIdQuestionAnswer() (id, q, a string)
GenerateIdQuestionAnswer(key string) (id, q, a string)
}
```

Expand Down Expand Up @@ -98,7 +98,7 @@ func NewCaptcha(driver Driver, store Store) *Captcha {

//Generate generates a random id, base64 image string or an error if any
func (c *Captcha) Generate() (id, b64s string, err error) {
id,content, answer := c.Driver.GenerateIdQuestionAnswer()
id,content, answer := c.Driver.GenerateIdQuestionAnswer("")
item, err := c.Driver.DrawCaptcha(content)
if err != nil {
return "", "", err
Expand All @@ -121,7 +121,7 @@ func (c *Captcha) Verify(id, answer string, clear bool) (match bool) {
#### 2.3.4 🚵🚵🚵 ‍Generate Base64(image/audio) string
```go
func (c *Captcha) Generate() (id, b64s string, err error) {
id,content, answer := c.Driver.GenerateIdQuestionAnswer()
id,content, answer := c.Driver.GenerateIdQuestionAnswer("")
item, err := c.Driver.DrawCaptcha(content)
if err != nil {
return "", "", err
Expand Down
11 changes: 3 additions & 8 deletions captcha.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
// base64Captcha is used for fast development of RESTful APIs, web apps and backend services in Go. give a string identifier to the package and it returns with a base64-encoding-png-string
package base64Captcha

import "strings"

// Captcha captcha basic information.
type Captcha struct {
Driver Driver
Expand All @@ -30,8 +28,8 @@ func NewCaptcha(driver Driver, store Store) *Captcha {
}

//Generate generates a random id, base64 image string or an error if any
func (c *Captcha) Generate() (id, b64s string, err error) {
id, content, answer := c.Driver.GenerateIdQuestionAnswer()
func (c *Captcha) Generate(key string) (id, b64s string, err error) {
id, content, answer := c.Driver.GenerateIdQuestionAnswer(key)
item, err := c.Driver.DrawCaptcha(content)
if err != nil {
return "", "", err
Expand All @@ -49,8 +47,5 @@ func (c *Captcha) Generate() (id, b64s string, err error) {
//if you has multiple captcha instances which share a same store.
//You may want to call `store.Verify` method instead.
func (c *Captcha) Verify(id, answer string, clear bool) (match bool) {
vv := c.Store.Get(id, clear)
//fix issue for some redis key-value string value
vv = strings.TrimSpace(vv)
return vv == strings.TrimSpace(answer)
return c.Store.Verify(id, answer, clear)
}
2 changes: 1 addition & 1 deletion captcha_with_etcd_exmaple.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const (

//GenerateIdAndImage create image
func (c *CaptchaEtcd) GenerateIdAndImage() (id, b64s, ans string, err error) {
id, content, answer := c.GenerateIdQuestionAnswer()
id, content, answer := c.GenerateIdQuestionAnswer("")
item, err := c.DrawCaptcha(content)
if err != nil {
return "", "", "", err
Expand Down
9 changes: 7 additions & 2 deletions driver_audio.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ func (d *DriverAudio) DrawCaptcha(content string) (item Item, err error) {
}

//GenerateIdQuestionAnswer creates id,captcha content and answer
func (d *DriverAudio) GenerateIdQuestionAnswer() (id, q, a string) {
id = RandomId()
func (d *DriverAudio) GenerateIdQuestionAnswer(key string) (id, q, a string) {
if len(key)==0 {
id = RandomId()
}else{
id = key
}

digits := randomDigits(d.Length)
a = parseDigitsToString(digits)
return id, a, a
Expand Down
2 changes: 1 addition & 1 deletion driver_audio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestDriverAudio_GenerateIdQuestionAnswer(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotId, gotQ, gotA := tt.d.GenerateIdQuestionAnswer()
gotId, gotQ, gotA := tt.d.GenerateIdQuestionAnswer("")
if gotId != tt.wantId {
t.Errorf("DriverAudio.GenerateIdQuestionAnswer() gotId = %v, want %v", gotId, tt.wantId)
}
Expand Down
10 changes: 7 additions & 3 deletions driver_chinese.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,13 @@ func (d *DriverChinese) ConvertFonts() *DriverChinese {
}

//GenerateIdQuestionAnswer generates captcha content and its answer
func (d *DriverChinese) GenerateIdQuestionAnswer() (id, content, answer string) {
id = RandomId()

func (d *DriverChinese) GenerateIdQuestionAnswer(key string) (id, content, answer string) {
if len(key)==0 {
id = RandomId()
}else{
id = key
}

ss := strings.Split(d.Source, ",")
length := len(ss)
if length == 1 {
Expand Down
2 changes: 1 addition & 1 deletion driver_chinese_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestDriverChinese_GenerateIdQuestionAnswer(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotId, gotContent, gotAnswer := tt.d.GenerateIdQuestionAnswer()
gotId, gotContent, gotAnswer := tt.d.GenerateIdQuestionAnswer("")
if gotId != tt.wantId {
t.Errorf("DriverChinese.GenerateIdQuestionAnswer() gotId = %v, want %v", gotId, tt.wantId)
}
Expand Down
8 changes: 6 additions & 2 deletions driver_digit.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@ func NewDriverDigit(height int, width int, length int, maxSkew float64, dotCount
var DefaultDriverDigit = NewDriverDigit(80, 240, 5, 0.7, 80)

//GenerateIdQuestionAnswer creates captcha content and answer
func (d *DriverDigit) GenerateIdQuestionAnswer() (id, q, a string) {
id = RandomId()
func (d *DriverDigit) GenerateIdQuestionAnswer(key string) (id, q, a string) {
if len(key)==0 {
id = RandomId()
}else{
id = key
}
digits := randomDigits(d.Length)
a = parseDigitsToString(digits)
return id, a, a
Expand Down
2 changes: 1 addition & 1 deletion driver_digit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func TestDriverDigit_GenerateIdQuestionAnswer(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotId, gotQ, gotA := tt.d.GenerateIdQuestionAnswer()
gotId, gotQ, gotA := tt.d.GenerateIdQuestionAnswer("")
if gotId != tt.wantId {
t.Errorf("DriverDigit.GenerateIdQuestionAnswer() gotId = %v, want %v", gotId, tt.wantId)
}
Expand Down
8 changes: 6 additions & 2 deletions driver_language.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,12 @@ func NewDriverLanguage(height int, width int, noiseCount int, showLineOptions in
}

//GenerateIdQuestionAnswer creates content and answer
func (d *DriverLanguage) GenerateIdQuestionAnswer() (id, content, answer string) {
id = RandomId()
func (d *DriverLanguage) GenerateIdQuestionAnswer(key string) (id, content, answer string) {
if len(key)==0 {
id = RandomId()
}else{
id = key
}
content = generateRandomRune(d.Length, d.LanguageCode)
return id, content, content
}
Expand Down
4 changes: 2 additions & 2 deletions driver_language_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestDriverLanguage_DrawCaptcha(t *testing.T) {
ds := NewDriverLanguage(80, 240, 5, OptionShowSineLine|OptionShowSlimeLine|OptionShowHollowLine, 5, nil, nil, []*truetype.Font{fontChinese}, "emotion")

for i := 0; i < 40; i++ {
_, q, _ := ds.GenerateIdQuestionAnswer()
_, q, _ := ds.GenerateIdQuestionAnswer("")
item, err := ds.DrawCaptcha(q)
if err != nil {
t.Error(err)
Expand Down Expand Up @@ -81,7 +81,7 @@ func TestDriverLanguage_GenerateIdQuestionAnswer(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotId, gotContent, gotAnswer := tt.d.GenerateIdQuestionAnswer()
gotId, gotContent, gotAnswer := tt.d.GenerateIdQuestionAnswer("")
if gotId != tt.wantId {
t.Errorf("DriverLanguage.GenerateIdQuestionAnswer() gotId = %v, want %v", gotId, tt.wantId)
}
Expand Down
8 changes: 6 additions & 2 deletions driver_math.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,12 @@ func (d *DriverMath) ConvertFonts() *DriverMath {
}

//GenerateIdQuestionAnswer creates id,captcha content and answer
func (d *DriverMath) GenerateIdQuestionAnswer() (id, question, answer string) {
id = RandomId()
func (d *DriverMath) GenerateIdQuestionAnswer(key string) (id, question, answer string) {
if len(key)==0 {
id = RandomId()
}else{
id = key
}
operators := []string{"+", "-", "x"}
var mathResult int32
switch operators[rand.Int31n(3)] {
Expand Down
2 changes: 1 addition & 1 deletion driver_math_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestDriverMath_DrawCaptcha(t *testing.T) {
Fonts: tt.fields.Fonts,
}
d.ConvertFonts()
_, q, a := d.GenerateIdQuestionAnswer()
_, q, a := d.GenerateIdQuestionAnswer("")

gotItem, err := d.DrawCaptcha(q)
if (err != nil) != tt.wantErr {
Expand Down
8 changes: 6 additions & 2 deletions driver_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,12 @@ func (d *DriverString) ConvertFonts() *DriverString {
}

//GenerateIdQuestionAnswer creates id,content and answer
func (d *DriverString) GenerateIdQuestionAnswer() (id, content, answer string) {
id = RandomId()
func (d *DriverString) GenerateIdQuestionAnswer(key string) (id, content, answer string) {
if len(key)==0 {
id = RandomId()
}else{
id = key
}
content = RandText(d.Length, d.Source)
return id, content, content
}
Expand Down
2 changes: 1 addition & 1 deletion driver_string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func TestDriverString_GenerateIdQuestionAnswer(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotId, gotContent, gotAnswer := tt.d.GenerateIdQuestionAnswer()
gotId, gotContent, gotAnswer := tt.d.GenerateIdQuestionAnswer("")
if gotId != tt.wantId {
t.Errorf("DriverString.GenerateIdQuestionAnswer() gotId = %v, want %v", gotId, tt.wantId)
}
Expand Down
2 changes: 1 addition & 1 deletion interface_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ type Driver interface {
//DrawCaptcha draws binary item
DrawCaptcha(content string) (item Item, err error)
//GenerateIdQuestionAnswer creates rand id, content and answer
GenerateIdQuestionAnswer() (id, q, a string)
GenerateIdQuestionAnswer(key string) (id, q, a string)
}