Add VerifyString function.
Dmitry Chestnykh dmitry@codingrobots.com
Mon, 25 Apr 2011 00:42:43 +0200
1 files changed,
25 insertions(+),
0 deletions(-)
jump to
M
captcha.go
→
captcha.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