dchest / captcha

Go package captcha implements generation and verification of image and audio CAPTCHAs.
godoc.org/github.com/dchest/captcha
MIT License
1.92k stars 294 forks source link

Func VerifyString it works incorrectly #46

Closed Nappy-Says closed 2 years ago

Nappy-Says commented 2 years ago

I was advised by a colleague to use this repository, as he was able to implement protection. When I started testing I ran into a problem -- verify and verifystring display false all the time.

code:

func main() {
    id := captcha.New()
    path_img, _ := os.Create("data.png")

    var w io.WriterTo
    digits := captcha.RandomDigits(6)
    w = captcha.NewImage(id, digits, 256, 128)
    w.WriteTo(path_img)

    var x string
    fmt.Scan(&x)
    fmt.Println(
        "result: ", captcha.VerifyString(id, x), 
        "\ngenerate digits:", digits, 
        "\nid:", id,
    )
}

result from console: image

dchest commented 2 years ago

On these lines:

    digits := captcha.RandomDigits(6)
    w = captcha.NewImage(id, digits, 256, 128)

You just create new random digits unrelated to the captcha ID that you generated. To generate an image with the actual digits that get stored in the global store when you call captcha.New, use:

captcha.WriteImage(w, id, 256, 128)

Here's how it's implemented.