all repos — captcha @ d010e3f940eb4120c858e62195435aa592c1ece5

Go package captcha implements generation and verification of image and audio CAPTCHAs.

Add VerifyString function.
Dmitry Chestnykh dmitry@codingrobots.com
Mon, 25 Apr 2011 00:42:43 +0200
commit

d010e3f940eb4120c858e62195435aa592c1ece5

parent

bd0535eaebba405865e1ef9553be477c52adb83c

1 files changed, 25 insertions(+), 0 deletions(-)

jump to
M captcha.gocaptcha.go

@@ -89,11 +89,36 @@ //

// The function deletes the captcha with the given id from the internal // storage, so that the same captcha can't be verified anymore. func Verify(id string, digits []byte) bool { + if digits == nil || len(digits) == 0 { + return false + } reald := globalStore.getDigitsClear(id) if reald == nil { return false } return bytes.Equal(digits, reald) +} + +// VerifyString is like Verify, but accepts a string of digits. It removes +// spaces and commas from the string, but any other characters, apart from +// digits and listed above, will cause the function to return false. +func VerifyString(id string, digits string) bool { + if digits == "" { + return false + } + ns := make([]byte, len(digits)) + for i := range ns { + d := digits[i] + switch { + case '0' <= d && d <= '9': + ns[i] = d - '0' + case d == ' ' || d == ',': + // ignore + default: + return false + } + } + return Verify(id, ns) } // Collect deletes expired or used captchas from the internal storage. It is