-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcaptcha.go
35 lines (25 loc) · 854 Bytes
/
captcha.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package main
import (
"bytes"
"encoding/base64"
"fmt"
"syscall/js"
captcha "github.com/dchest/captcha"
)
var currentCaptchaId string
func generateCaptcha(this js.Value, inputs []js.Value) interface{} {
currentCaptchaId = captcha.New()
img_buf := bytes.NewBufferString("")
captcha.WriteImage(img_buf, currentCaptchaId, 192, 96)
return "data:image/png;base64," + base64.StdEncoding.EncodeToString(img_buf.Bytes())
}
func verifyCaptcha(this js.Value, inputs []js.Value) interface{} {
fmt.Println("Checking captcha:", currentCaptchaId, inputs[0].String())
return captcha.VerifyString(currentCaptchaId, inputs[0].String())
}
func main() {
fmt.Println("WebAssembly starting...")
js.Global().Set("generateCaptcha", js.FuncOf(generateCaptcha))
js.Global().Set("verifyCaptcha", js.FuncOf(verifyCaptcha))
<-make(chan bool) // Waiting...
}