mirror of
https://github.com/librecaptcha/lc-core.git
synced 2025-02-12 19:28:05 -05:00
Initial skeleton for the framework.
This commit is contained in:
parent
864c9dd9a0
commit
4610245b38
18
build.sbt
Normal file
18
build.sbt
Normal file
@ -0,0 +1,18 @@
|
||||
import Dependencies._
|
||||
|
||||
lazy val root = (project in file(".")).
|
||||
settings(
|
||||
inThisBuild(List(
|
||||
organization := "com.example",
|
||||
scalaVersion := "2.12.3",
|
||||
version := "0.1.0-SNAPSHOT")),
|
||||
name := "LibreCaptcha",
|
||||
libraryDependencies += scalaTest % Test,
|
||||
|
||||
libraryDependencies += "com.sksamuel.scrimage" %% "scrimage-core" % "2.1.8",
|
||||
|
||||
libraryDependencies += "com.sksamuel.scrimage" %% "scrimage-io-extra" % "2.1.8",
|
||||
|
||||
libraryDependencies += "com.sksamuel.scrimage" %% "scrimage-filters" % "2.1.8"
|
||||
|
||||
)
|
5
project/Dependencies.scala
Normal file
5
project/Dependencies.scala
Normal file
@ -0,0 +1,5 @@
|
||||
import sbt._
|
||||
|
||||
object Dependencies {
|
||||
lazy val scalaTest = "org.scalatest" %% "scalatest" % "3.0.3"
|
||||
}
|
1
project/build.properties
Normal file
1
project/build.properties
Normal file
@ -0,0 +1 @@
|
||||
sbt.version=1.0.4
|
46
src/main/scala/Main.scala
Normal file
46
src/main/scala/Main.scala
Normal file
@ -0,0 +1,46 @@
|
||||
import com.sksamuel.scrimage._
|
||||
import java.io._
|
||||
|
||||
class CaptchaLibrary {
|
||||
var tokenAnswer = scala.collection.mutable.Map[String, String]()
|
||||
def init = {}
|
||||
def shutdown = {}
|
||||
def getChallenge(): Challenge = {
|
||||
//choose a captcha provider randomly
|
||||
val blurCaptcha = new BlurCaptcha
|
||||
val (challenge, answer) = blurCaptcha.getChallenge()
|
||||
tokenAnswer += challenge.token->answer
|
||||
challenge
|
||||
}
|
||||
def checkAnswer(token: String, input: String): Boolean = {
|
||||
if (tokenAnswer(token) == input) {
|
||||
true
|
||||
}
|
||||
else {
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
trait CaptchaProvider {
|
||||
def getChallenge(): (Challenge, String)
|
||||
}
|
||||
|
||||
class Challenge(val token: String, val image: Image)
|
||||
|
||||
class Answer(val token: String, val input: String)
|
||||
|
||||
class BlurCaptcha extends CaptchaProvider {
|
||||
def getChallenge(): (Challenge, String) = {
|
||||
val inFileName = "image2.png"
|
||||
var image = Image.fromStream(new FileInputStream(inFileName))
|
||||
image = image.filter(com.sksamuel.scrimage.filter.BlurFilter)
|
||||
image.output(new File("blur.png"))
|
||||
|
||||
val r = scala.util.Random
|
||||
val token = r.nextInt(1000).toString
|
||||
val challenge = new Challenge(token, image)
|
||||
val answer = "about"
|
||||
(challenge, answer)
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user