From f5effb5ec0be130bde0f400d444e73fc8f655d79 Mon Sep 17 00:00:00 2001 From: forthxu Date: Thu, 2 Dec 2021 16:57:08 +0800 Subject: [PATCH 1/2] update enable private key --- README.md | 6 +++--- captcha.go | 4 ++-- captcha_with_etcd_exmaple.md | 2 +- driver_audio.go | 9 +++++++-- driver_audio_test.go | 2 +- driver_chinese.go | 10 +++++++--- driver_chinese_test.go | 2 +- driver_digit.go | 8 ++++++-- driver_digit_test.go | 2 +- driver_language.go | 8 ++++++-- driver_language_test.go | 4 ++-- driver_math.go | 8 ++++++-- driver_math_test.go | 2 +- driver_string.go | 8 ++++++-- driver_string_test.go | 2 +- interface_driver.go | 2 +- 16 files changed, 52 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 864bf1a..e3bec5c 100644 --- a/README.md +++ b/README.md @@ -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) } ``` @@ -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 @@ -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 diff --git a/captcha.go b/captcha.go index 4fac1c4..5e341d9 100644 --- a/captcha.go +++ b/captcha.go @@ -30,8 +30,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 diff --git a/captcha_with_etcd_exmaple.md b/captcha_with_etcd_exmaple.md index d5c63e3..5cbff0a 100644 --- a/captcha_with_etcd_exmaple.md +++ b/captcha_with_etcd_exmaple.md @@ -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 diff --git a/driver_audio.go b/driver_audio.go index 8ffcb74..def34a4 100644 --- a/driver_audio.go +++ b/driver_audio.go @@ -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 diff --git a/driver_audio_test.go b/driver_audio_test.go index 40145b9..266a5aa 100644 --- a/driver_audio_test.go +++ b/driver_audio_test.go @@ -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) } diff --git a/driver_chinese.go b/driver_chinese.go index 140949e..c11fcf6 100644 --- a/driver_chinese.go +++ b/driver_chinese.go @@ -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 { diff --git a/driver_chinese_test.go b/driver_chinese_test.go index 8f9bd35..df5c0bd 100644 --- a/driver_chinese_test.go +++ b/driver_chinese_test.go @@ -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) } diff --git a/driver_digit.go b/driver_digit.go index e96b06f..8ee8c78 100644 --- a/driver_digit.go +++ b/driver_digit.go @@ -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 diff --git a/driver_digit_test.go b/driver_digit_test.go index d2346e4..74682cd 100644 --- a/driver_digit_test.go +++ b/driver_digit_test.go @@ -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) } diff --git a/driver_language.go b/driver_language.go index 0efb2fd..5050d19 100644 --- a/driver_language.go +++ b/driver_language.go @@ -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 } diff --git a/driver_language_test.go b/driver_language_test.go index 3a8a71b..a2d79b0 100644 --- a/driver_language_test.go +++ b/driver_language_test.go @@ -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) @@ -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) } diff --git a/driver_math.go b/driver_math.go index 7ee1b7c..9834738 100644 --- a/driver_math.go +++ b/driver_math.go @@ -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)] { diff --git a/driver_math_test.go b/driver_math_test.go index ecafcdc..048702e 100644 --- a/driver_math_test.go +++ b/driver_math_test.go @@ -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 { diff --git a/driver_string.go b/driver_string.go index 6f36d1f..a777d36 100644 --- a/driver_string.go +++ b/driver_string.go @@ -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 } diff --git a/driver_string_test.go b/driver_string_test.go index d9ff843..23af3d5 100644 --- a/driver_string_test.go +++ b/driver_string_test.go @@ -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) } diff --git a/interface_driver.go b/interface_driver.go index 06e25ff..c157ab6 100644 --- a/interface_driver.go +++ b/interface_driver.go @@ -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) } From ffa41735e8043e6d47386316d4d743bae44ef909 Mon Sep 17 00:00:00 2001 From: forthxu Date: Tue, 7 Dec 2021 15:35:09 +0800 Subject: [PATCH 2/2] update use Store.Verify --- captcha.go | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/captcha.go b/captcha.go index 5e341d9..fac30c0 100644 --- a/captcha.go +++ b/captcha.go @@ -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 @@ -49,8 +47,5 @@ func (c *Captcha) Generate(key string) (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) }