Skip to content

Commit b247e7d

Browse files
author
GuoHaoLan
committed
Add
1 parent 5f558c6 commit b247e7d

1 file changed

Lines changed: 22 additions & 3 deletions

File tree

main.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ type CaptchaRes struct {
6565
}
6666

6767
type Captcha struct {
68-
cache *captcha.Store
68+
cache captcha.Store
6969
}
7070

7171
func (c *Captcha) GenCaptcha(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
@@ -115,15 +115,34 @@ func (c *Captcha) HandleCaptcha(w http.ResponseWriter, r *http.Request, ps httpr
115115
return
116116
}
117117

118-
val := c.cache.Get(_captchaID)
118+
val := c.cache.Get(_captchaID, true)
119119

120-
if string(val) != _phrase {
120+
if !bytes.Equal(val, ConvertStringToByte(_phrase)) {
121121
ThrowError(w, CREDENTIAL_NOT_MATCH, "Phrase Not correct", "phrase")
122122
return
123123
}
124124

125125
}
126126

127+
func ConvertStringToByte(digits string) []byte {
128+
if digits == "" {
129+
return nil
130+
}
131+
ns := make([]byte, len(digits))
132+
for i := range ns {
133+
d := digits[i]
134+
switch {
135+
case '0' <= d && d <= '9':
136+
ns[i] = d - '0'
137+
case d == ' ' || d == ',':
138+
// ignore
139+
default:
140+
return nil
141+
}
142+
}
143+
return ns
144+
}
145+
127146
func ThrowError(w http.ResponseWriter, ErrorType int, ErrorDescription string, opts ...string) {
128147
_newErr := &GeneralResult{
129148
ErrCode: ErrorType,

0 commit comments

Comments
 (0)