Skip to content

Instantly share code, notes, and snippets.

@birowo
Last active December 17, 2018 08:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save birowo/19817828a43383894edb258f88dfb26b to your computer and use it in GitHub Desktop.
Save birowo/19817828a43383894edb258f88dfb26b to your computer and use it in GitHub Desktop.
GOLANG: latihan captcha pakai library https://github.com/mojocn/base64Captcha
package main
import (
"net/http"
"github.com/julienschmidt/httprouter"
"github.com/mojocn/base64Captcha"
)
func main() {
r := httprouter.New()
r.GET("/captcha", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
//create a captchaInterface instance.
//GenerateCaptcha first parameter is empty string,so the package will generate a random uuid for you.
idkey, captchaInterfaceIntance := base64Captcha.GenerateCaptcha("", base64Captcha.ConfigCharacter{
Height: 60,
Width: 180,
//const CaptchaModeNumber:数字,CaptchaModeAlphabet:字母,CaptchaModeArithmetic:算术,CaptchaModeNumberAlphabet:数字字母混合.
Mode: base64Captcha.CaptchaModeNumberAlphabet,
ComplexOfNoiseText: base64Captcha.CaptchaComplexLower,
ComplexOfNoiseDot: base64Captcha.CaptchaComplexLower,
IsUseSimpleFont: false,
IsShowHollowLine: false,
IsShowNoiseDot: true,
IsShowNoiseText: false,
IsShowSlimeLine: false,
IsShowSineLine: false,
CaptchaLen: 6,
})
http.SetCookie(w, &http.Cookie{
Name: "idkey",
Value: idkey,
HttpOnly: true,
})
captchaInterfaceIntance.WriteTo(w)
})
r.POST("/captcha", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
idkey, _ := r.Cookie("idkey")
if base64Captcha.VerifyCaptcha(idkey.Value, r.PostFormValue("captcha")) {
w.Write([]byte("verify OK"))
} else {
w.Write([]byte("verify false"))
}
})
r.GET("/", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
w.WriteHeader(200)
w.Write([]byte(`
<iframe src="/captcha" name="captcha"></iframe><br>
<a href="/captcha" target="captcha">coba gambar lain</a><br>
<form method="POST" action="/captcha" target="hasil">
ketikkan teks di atas: <input name="captcha"><br>
<input type="submit" value="kirim">
</form>
<iframe src="about:blank" name="hasil"></iframe>
`))
})
http.ListenAndServe(":8080", r)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment