Browse Source

Add VerifyString function.

Dmitry Chestnykh 14 years ago
parent
commit
d010e3f940
1 changed files with 25 additions and 0 deletions
  1. 25 0
      captcha.go

+ 25 - 0
captcha.go

@@ -89,6 +89,9 @@ func WriteAudio(w io.Writer, id string) os.Error {
 // 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
@@ -96,6 +99,28 @@ func Verify(id string, digits []byte) bool {
 	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
 // called automatically by New function every CollectNum generated captchas,
 // but still exported to enable freeing memory manually if needed.