# HG changeset patch # User Christian Urban # Date 1412423164 -3600 # Node ID 9c968d0de9a07758d85476940d3d038c489fbbda # Parent 22f027da67ecafbcaea0be802ab89a24fc82e289 moved old scala files into a subdirectory diff -r 22f027da67ec -r 9c968d0de9a0 progs/Application0.scala --- a/progs/Application0.scala Sat Oct 04 00:36:51 2014 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,24 +0,0 @@ -package controllers - -import play.api._ -import play.api.mvc._ - -// hello world program: -// just answer the GET request with a string - -object Application extends Controller { - - // answering a GET request - def index = Action { - Ok(views.html.index("222Your new application is ready.")) - //Ok("Hello World") - } - -} - -/* - * HTML can be returned using - * - * Ok("

Hello world!

").as(HTML) - * - */ diff -r 22f027da67ec -r 9c968d0de9a0 progs/Application1.scala --- a/progs/Application1.scala Sat Oct 04 00:36:51 2014 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,39 +0,0 @@ -package controllers - -import play.api._ -import play.api.mvc._ -import play.api.data._ -import play.api.data.Forms._ - -/* - * Answers a GET-request by sending a simple login form. - * - * Processes the POST-data by just printing the results. - * - */ - -object Application extends Controller { - - // GET request -> login form - val index = Action { request => - - val form = """ -
- Login:
- Password:
-
""" - - Ok(form).as(HTML) - } - - - // POST data: processing the login data - val receive = Action { request => - - val form_data = Form(tuple ("login" -> text, "password" -> text)) - val (login, password) = form_data.bindFromRequest()(request).get - - Ok(s"Received login: $login and password: $password") - } - -} diff -r 22f027da67ec -r 9c968d0de9a0 progs/Application2.scala --- a/progs/Application2.scala Sat Oct 04 00:36:51 2014 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,43 +0,0 @@ -package controllers - -import play.api.mvc._ - -/* - * Application sets a cookie in plain ASCII on the - * client's browser recording the number of visits - * of a page. - */ - -object Application extends Controller { - - //no or invalid cookie results in the counter being 0 - def gt_cookie(c: Cookie) : Int = c.value match { - case s if (s.forall(_.isDigit)) => s.toInt - case _ => 0 - } - - def mk_cookie(i: Int) : Cookie = { - Cookie("visits", i.toString) - } - - // GET request: read cookie data first - def index = Action { request => - - //reads the cookie and extracts the visits counter - val visits_cookie = request.cookies.get("visits") - val visits = visits_cookie.map(gt_cookie).getOrElse(0) - - //printing a message according to value of visits counter - val msg = - if (visits >= 10) - s"You are a valued customer who has visited this site $visits times." - else s"You have visited this site $visits times." - - //send message with new cookie - Ok(msg).withCookies(mk_cookie(visits + 1)) - } -} - - - - diff -r 22f027da67ec -r 9c968d0de9a0 progs/Application3.scala --- a/progs/Application3.scala Sat Oct 04 00:36:51 2014 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,50 +0,0 @@ -package controllers - -import play.api.mvc._ -import java.security.MessageDigest - -/* - * Application sets a cookie in plain ASCII on the - * clients browser recording the visits of a page. - * - * The cookie data is hashed and stored in the format: - * - * visits_counter/hashed_value - */ - -object Application extends Controller { - - //hash functions: SHA-1, SHA-256, etc - def mk_hash(s: String) : String = { - val hash_fun = MessageDigest.getInstance("SHA-1") - hash_fun.digest(s.getBytes).map{ "%02x".format(_) }.mkString - } - - //extracting from the string .../... the visits - //value and hash - def gt_cookie(c: Cookie) : Int = c.value.split("/") match { - case Array(s, h) if (s.forall(_.isDigit) && mk_hash(s) == h) => s.toInt - case _ => 0 - } - - def mk_cookie(i: Int) : Cookie = { - val hash = mk_hash(i.toString) - Cookie("visits", s"$i/$hash") - } - - def index = Action { request => - val visits_cookie = request.cookies.get("visits") - val visits = visits_cookie.map(gt_cookie).getOrElse(0) - - val msg = - if (visits >= 10) - s"You are a valued customer who has visited this site $visits times." - else s"You have visited this site $visits times." - - Ok(msg).withCookies(mk_cookie(visits + 1)) - } -} - - - - diff -r 22f027da67ec -r 9c968d0de9a0 progs/Application4.scala --- a/progs/Application4.scala Sat Oct 04 00:36:51 2014 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,52 +0,0 @@ -package controllers - -import play.api.mvc._ -import java.security.MessageDigest - -/* - * Application sets a cookie in plain ASCII on the - * clients browser recording the visits of a page. - * - * The cookie data is hashed and salted with a - * secret key. - */ - - -object Application extends Controller { - - //secret key for salting - val salt = "my secret key" - - //SHA-1 + salt - def mk_hash(s: String) : String = { - val hash_fun = MessageDigest.getInstance("SHA-1") - hash_fun.digest((s + salt).getBytes).map{ "%02x".format(_) }.mkString - } - - def gt_cookie(c: Cookie) : Int = c.value.split("/") match { - case Array(s, h) if (s.forall(_.isDigit) && mk_hash(s) == h) => s.toInt - case _ => 0 - } - - def mk_cookie(i: Int) : Cookie = { - val hash = mk_hash(i.toString) - Cookie("visits", s"$i/$hash") - } - - def index = Action { request => - - val visits_cookie = request.cookies.get("visits") - val visits = visits_cookie.map(gt_cookie).getOrElse(0) - - val msg = - if (visits >= 10) - s"You are a valued customer who has visited this site $visits times." - else s"You have visited this site $visits times." - - Ok(msg).withCookies(mk_cookie(visits + 1)) - } -} - - - - diff -r 22f027da67ec -r 9c968d0de9a0 progs/app0.scala --- a/progs/app0.scala Sat Oct 04 00:36:51 2014 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,13 +0,0 @@ -package controllers -import play.api.mvc._ - -object Application extends Controller { - - // answering a GET request - val index = Action { request => - Ok("Hello world!") - } -} - - - diff -r 22f027da67ec -r 9c968d0de9a0 progs/app1.scala --- a/progs/app1.scala Sat Oct 04 00:36:51 2014 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,26 +0,0 @@ -object Application extends Controller { - - // GET request -> present login form - val index = Action { request => - - val form = - """
- Login:
- Password:
-
""" - - Ok(form).as(HTML) - } - - // POST data: processing the login data - val receive = Action { request => - - val form_data = Form(tuple ("login" -> text, "password" -> text)) - def (login, passwd) = form_data.bindFromRequest()(request).get - - Ok(s"Received login: $login and password: $passwd") - } -} - - - diff -r 22f027da67ec -r 9c968d0de9a0 progs/app2.scala --- a/progs/app2.scala Sat Oct 04 00:36:51 2014 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,30 +0,0 @@ -object Application extends Controller { - - def gt_cookie(c: Cookie) : Int = c.value match { - case s if (s.forall(_.isDigit)) => s.toInt - case _ => 0 - } - - def mk_cookie(i: Int) : Cookie = Cookie("visits", i.toString) - - // GET request: read cookie data first - def index = Action { request => - - //reads the cookie and extracts the visits counter - val visits_cookie = request.cookies.get("visits") - val visits = visits_cookie.map(gt_cookie).getOrElse(0) - - //printing a message according to value of visits counter - val msg = - if (visits >= 10) - s"You are a valued customer who has visited this site $visits times." - else s"You have visited this site $visits times." - - //send message with new cookie - Ok(msg).withCookies(mk_cookie(visits + 1)) - } -} - - - - diff -r 22f027da67ec -r 9c968d0de9a0 progs/app3.scala --- a/progs/app3.scala Sat Oct 04 00:36:51 2014 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,25 +0,0 @@ -object Application extends Controller { - - //SHA-1, SHA-256 - def mk_hash(s: String) : String = { - val hash_fun = MessageDigest.getInstance("SHA-1") - hash_fun.digest(s.getBytes).map{ "%02x".format(_) }.mkString - } - - def gt_cookie(c: Cookie) : Int = c.value.split("/") match { - case Array(s, h) - if (s.forall(_.isDigit) && mk_hash(s) == h) => s.toInt - case _ => 0 - } - - def mk_cookie(i: Int) : Cookie = { - val hash = mk_hash(i.toString) - Cookie("visits", s"$i/$hash") - } - - def index = Action { request => ... } -} - - - - diff -r 22f027da67ec -r 9c968d0de9a0 progs/app4.scala --- a/progs/app4.scala Sat Oct 04 00:36:51 2014 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,27 +0,0 @@ -object Application extends Controller { - - val salt = "my secret key" - - //SHA-1 + salt - def mk_hash(s: String) : String = { - val hash_fun = MessageDigest.getInstance("SHA-1") - hash_fun.digest((s + salt).getBytes).map{ "%02x".format(_) }.mkString - } - - def gt_cookie(c: Cookie) : Int = c.value.split("/") match { - case Array(s, h) - if (s.forall(_.isDigit) && mk_hash(s) == h) => s.toInt - case _ => 0 - } - - def mk_cookie(i: Int) : Cookie = { - val hash = mk_hash(i.toString) - Cookie("visits", s"$i/$hash") - } - - def index = Action { request => ... } -} - - - - diff -r 22f027da67ec -r 9c968d0de9a0 progs/prove.scala --- a/progs/prove.scala Sat Oct 04 00:36:51 2014 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,179 +0,0 @@ -import scala.language.implicitConversions -import scala.language.reflectiveCalls -import scala.util._ - -abstract class Term -case class Var(s: String) extends Term -case class Const(s: String) extends Term -case class Fun(s: String, ts: List[Term]) extends Term - -abstract class Form -case object True extends Form -case object False extends Form -case class Pred(s: String, ts: List[Term]) extends Form -case class Imp(f1: Form, f2: Form) extends Form -case class Says(p: String, f: Form) extends Form -case class And(f1: Form, f2: Form) extends Form -case class Or(f1: Form, f2: Form) extends Form -case class Sends(p: String, q: String, f: Form) extends Form -case class Enc(f1: Form, f2: Form) extends Form - -case class Judgement(gamma: Set[Form], f: Form) { - def lhs = gamma - def rhs = f -} - -// some syntactic sugar -implicit def FormOps(f1: Form) = new { - def -> (f2: Form) = Imp(f1, f2) -} -implicit def StringOps(p: String) = new { - def says (f: Form) = Says(p, f) -} -implicit def SetFormOps(gamma: Set[Form]) = new { - def |- (f: Form) : Judgement = Judgement(gamma, f) -} - -val Admin = "Admin" -val Bob = "Bob" -val Del = Pred("del_file", Nil) - -val Gamma: Set[Form] = - Set( (Admin says Del) -> Del, - Admin says ((Bob says Del) -> Del), - Bob says Del ) - -val goal = Gamma |- Del // request: provable or not? - -def partitions[A](s: Set[A]): Set[(A, Set[A])] = - s.map (e => (e, s - e)) - - -def prove(j: Judgement, sc: () => Unit) : Unit = { - if (j.lhs.contains(j.rhs)) sc () // Axiom rule - else { - prove1(j, sc); - for ((f, lhs_rest) <- partitions(j.lhs)) prove2(f, lhs_rest, j.rhs, sc) - } -} - -def prove1(j: Judgement, sc: () => Unit) : Unit = - j.rhs match { - case True => sc () - case False => () - case Imp(f1, f2) => prove(j.lhs + f1 |- f2, sc) - case Says(p, Enc(f1, f2)) => - prove(j.lhs |- Says(p, f1), - () => prove(j.lhs |- Says(p, f2), sc)) - case Says(p, f1) => prove(j.lhs |- f1, sc) - case Or(f1, f2) => - { prove(j.lhs |- f1, sc); - prove(j.lhs |- f2, sc) } - case And(f1, f2) => - prove(j.lhs |- f1, - () => prove(j.lhs |- f2, sc)) - case Sends(p, q, f) => prove(j.lhs + (p says f) |- (q says f), sc) - case _ => () - } - -def prove2(f: Form, lhs_rest: Set[Form], rhs: Form, sc: () => Unit) : Unit = - f match { - case True => prove(lhs_rest |- rhs, sc) - case False => sc () - case And(f1, f2) => - prove(lhs_rest + f1 + f2 |- rhs, sc) - case Imp(f1, f2) => - prove(lhs_rest |- f1, - () => prove(lhs_rest + f2 |- rhs, sc)) - case Sends(p, q, f) => - prove(lhs_rest |- (p says f), - () => prove(lhs_rest + (q says f) |- rhs, sc)) - case Enc(f1, f2) => - prove(lhs_rest |- f1, - () => prove(lhs_rest + f2 |- rhs, sc)) - case Or(f1, f2) => - prove(lhs_rest + f1 |- rhs, - () => prove(lhs_rest + f2 |- rhs, sc)) - case Says(p, Enc(f1, f2)) => - prove(lhs_rest |- Says(p, f2), - () => prove(lhs_rest + Says(p, f1) |- rhs, sc)) - case Says(p, Imp(f1, f2)) => - prove(lhs_rest |- Says(p, f1), - () => prove(lhs_rest + Says(p, f2) |- rhs, sc)) - - case _ => () - } - -// function that calls prove and returns immediately once a proof is found -def run (j : Judgement) : Unit = { - def sc () = { println ("Yes!"); throw new Exception } - Try(prove(j, sc)) getOrElse () -} - -run (goal) - -run (Set[Form]() |- False -> Del) -run (Set[Form]() |- True -> Del) -run (Set[Form]() |- Del -> True) -run (Set[Form]() |- Del -> Del) -run (Set[Form]() |- Del -> Or(False, Del)) - - -val Gamma1 : Set[Form] = - Set( Admin says ((Bob says Del) -> Del), - Bob says Del ) - -val goal1 = Gamma1 |- Del // not provable -run (goal1) - - -val f1 = "P" says Pred("F1", Nil) -val f2 = "Q" says Pred("F2", Nil) -run (Set[Form](And(f1, f2)) |- And(f2, f1)) - - -val Chr = "Christian" -val HoD = "Peter" -val Email = Pred("may_obtain_email", List(Const(Chr))) -val AtLib = Pred("is_at_library", List(Const(Chr))) -val Chr_Staff = Pred("is_staff", List(Const(Chr))) - -val Policy_HoD = (HoD says Chr_Staff) -> Chr_Staff -val Policy_Lib = And(Chr_Staff, AtLib) -> Email -val HoD_says = HoD says Chr_Staff - -run (Set[Form](AtLib, Policy_HoD, Policy_Lib) |- Email) - -println("Server Example") - -def Connect(p: String, q: String) : Form = - Pred("Connect", List(Var(p), Var(q))) - - -val A = "A" -val B = "B" -val S = "S" -val CAB = Connect(A, B) -val Msg = Pred("Msg", Nil) -val KAS = Pred("Kas", Nil) -val KBS = Pred("Kbs", Nil) -val KAB = Pred("Kab", Nil) - -val Gamma_big : Set[Form] = - Set( A says CAB, - Sends(A, S, CAB), - S says (CAB -> Enc(KAB, KAS)), - S says (CAB -> Enc(Enc(KAB, KBS), KAS)), - Sends(S, A, Enc(KAB, KAS)), - Sends(S, A, Enc(Enc(KAB, KBS), KAS)), - Sends(A, B, Enc(KAB, KBS)), - Sends(A, B, Enc(Msg, KAB)), - A says KAS, - B says KBS, - S says KAS, - A says (Enc(Msg, KAB)) - ) - -run (Gamma_big |- (B says Msg)) - - diff -r 22f027da67ec -r 9c968d0de9a0 progs/routes --- a/progs/routes Sat Oct 04 00:36:51 2014 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,10 +0,0 @@ -# Application1 needs entries for both GET and POST -# -# all other applications only need an entry for GET - -# Home page -GET / controllers.Application.index -#POST / controllers.Application.receive - -# Map static resources from the /public folder to the /assets URL path -GET /assets/*file controllers.Assets.at(path="/public", file) \ No newline at end of file diff -r 22f027da67ec -r 9c968d0de9a0 progs/scala/Application0.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/progs/scala/Application0.scala Sat Oct 04 12:46:04 2014 +0100 @@ -0,0 +1,24 @@ +package controllers + +import play.api._ +import play.api.mvc._ + +// hello world program: +// just answer the GET request with a string + +object Application extends Controller { + + // answering a GET request + def index = Action { + Ok(views.html.index("222Your new application is ready.")) + //Ok("Hello World") + } + +} + +/* + * HTML can be returned using + * + * Ok("

Hello world!

").as(HTML) + * + */ diff -r 22f027da67ec -r 9c968d0de9a0 progs/scala/Application1.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/progs/scala/Application1.scala Sat Oct 04 12:46:04 2014 +0100 @@ -0,0 +1,39 @@ +package controllers + +import play.api._ +import play.api.mvc._ +import play.api.data._ +import play.api.data.Forms._ + +/* + * Answers a GET-request by sending a simple login form. + * + * Processes the POST-data by just printing the results. + * + */ + +object Application extends Controller { + + // GET request -> login form + val index = Action { request => + + val form = """ +
+ Login:
+ Password:
+
""" + + Ok(form).as(HTML) + } + + + // POST data: processing the login data + val receive = Action { request => + + val form_data = Form(tuple ("login" -> text, "password" -> text)) + val (login, password) = form_data.bindFromRequest()(request).get + + Ok(s"Received login: $login and password: $password") + } + +} diff -r 22f027da67ec -r 9c968d0de9a0 progs/scala/Application2.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/progs/scala/Application2.scala Sat Oct 04 12:46:04 2014 +0100 @@ -0,0 +1,43 @@ +package controllers + +import play.api.mvc._ + +/* + * Application sets a cookie in plain ASCII on the + * client's browser recording the number of visits + * of a page. + */ + +object Application extends Controller { + + //no or invalid cookie results in the counter being 0 + def gt_cookie(c: Cookie) : Int = c.value match { + case s if (s.forall(_.isDigit)) => s.toInt + case _ => 0 + } + + def mk_cookie(i: Int) : Cookie = { + Cookie("visits", i.toString) + } + + // GET request: read cookie data first + def index = Action { request => + + //reads the cookie and extracts the visits counter + val visits_cookie = request.cookies.get("visits") + val visits = visits_cookie.map(gt_cookie).getOrElse(0) + + //printing a message according to value of visits counter + val msg = + if (visits >= 10) + s"You are a valued customer who has visited this site $visits times." + else s"You have visited this site $visits times." + + //send message with new cookie + Ok(msg).withCookies(mk_cookie(visits + 1)) + } +} + + + + diff -r 22f027da67ec -r 9c968d0de9a0 progs/scala/Application3.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/progs/scala/Application3.scala Sat Oct 04 12:46:04 2014 +0100 @@ -0,0 +1,50 @@ +package controllers + +import play.api.mvc._ +import java.security.MessageDigest + +/* + * Application sets a cookie in plain ASCII on the + * clients browser recording the visits of a page. + * + * The cookie data is hashed and stored in the format: + * + * visits_counter/hashed_value + */ + +object Application extends Controller { + + //hash functions: SHA-1, SHA-256, etc + def mk_hash(s: String) : String = { + val hash_fun = MessageDigest.getInstance("SHA-1") + hash_fun.digest(s.getBytes).map{ "%02x".format(_) }.mkString + } + + //extracting from the string .../... the visits + //value and hash + def gt_cookie(c: Cookie) : Int = c.value.split("/") match { + case Array(s, h) if (s.forall(_.isDigit) && mk_hash(s) == h) => s.toInt + case _ => 0 + } + + def mk_cookie(i: Int) : Cookie = { + val hash = mk_hash(i.toString) + Cookie("visits", s"$i/$hash") + } + + def index = Action { request => + val visits_cookie = request.cookies.get("visits") + val visits = visits_cookie.map(gt_cookie).getOrElse(0) + + val msg = + if (visits >= 10) + s"You are a valued customer who has visited this site $visits times." + else s"You have visited this site $visits times." + + Ok(msg).withCookies(mk_cookie(visits + 1)) + } +} + + + + diff -r 22f027da67ec -r 9c968d0de9a0 progs/scala/Application4.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/progs/scala/Application4.scala Sat Oct 04 12:46:04 2014 +0100 @@ -0,0 +1,52 @@ +package controllers + +import play.api.mvc._ +import java.security.MessageDigest + +/* + * Application sets a cookie in plain ASCII on the + * clients browser recording the visits of a page. + * + * The cookie data is hashed and salted with a + * secret key. + */ + + +object Application extends Controller { + + //secret key for salting + val salt = "my secret key" + + //SHA-1 + salt + def mk_hash(s: String) : String = { + val hash_fun = MessageDigest.getInstance("SHA-1") + hash_fun.digest((s + salt).getBytes).map{ "%02x".format(_) }.mkString + } + + def gt_cookie(c: Cookie) : Int = c.value.split("/") match { + case Array(s, h) if (s.forall(_.isDigit) && mk_hash(s) == h) => s.toInt + case _ => 0 + } + + def mk_cookie(i: Int) : Cookie = { + val hash = mk_hash(i.toString) + Cookie("visits", s"$i/$hash") + } + + def index = Action { request => + + val visits_cookie = request.cookies.get("visits") + val visits = visits_cookie.map(gt_cookie).getOrElse(0) + + val msg = + if (visits >= 10) + s"You are a valued customer who has visited this site $visits times." + else s"You have visited this site $visits times." + + Ok(msg).withCookies(mk_cookie(visits + 1)) + } +} + + + + diff -r 22f027da67ec -r 9c968d0de9a0 progs/scala/app0.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/progs/scala/app0.scala Sat Oct 04 12:46:04 2014 +0100 @@ -0,0 +1,13 @@ +package controllers +import play.api.mvc._ + +object Application extends Controller { + + // answering a GET request + val index = Action { request => + Ok("Hello world!") + } +} + + + diff -r 22f027da67ec -r 9c968d0de9a0 progs/scala/app1.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/progs/scala/app1.scala Sat Oct 04 12:46:04 2014 +0100 @@ -0,0 +1,26 @@ +object Application extends Controller { + + // GET request -> present login form + val index = Action { request => + + val form = + """
+ Login:
+ Password:
+
""" + + Ok(form).as(HTML) + } + + // POST data: processing the login data + val receive = Action { request => + + val form_data = Form(tuple ("login" -> text, "password" -> text)) + def (login, passwd) = form_data.bindFromRequest()(request).get + + Ok(s"Received login: $login and password: $passwd") + } +} + + + diff -r 22f027da67ec -r 9c968d0de9a0 progs/scala/app2.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/progs/scala/app2.scala Sat Oct 04 12:46:04 2014 +0100 @@ -0,0 +1,30 @@ +object Application extends Controller { + + def gt_cookie(c: Cookie) : Int = c.value match { + case s if (s.forall(_.isDigit)) => s.toInt + case _ => 0 + } + + def mk_cookie(i: Int) : Cookie = Cookie("visits", i.toString) + + // GET request: read cookie data first + def index = Action { request => + + //reads the cookie and extracts the visits counter + val visits_cookie = request.cookies.get("visits") + val visits = visits_cookie.map(gt_cookie).getOrElse(0) + + //printing a message according to value of visits counter + val msg = + if (visits >= 10) + s"You are a valued customer who has visited this site $visits times." + else s"You have visited this site $visits times." + + //send message with new cookie + Ok(msg).withCookies(mk_cookie(visits + 1)) + } +} + + + + diff -r 22f027da67ec -r 9c968d0de9a0 progs/scala/app3.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/progs/scala/app3.scala Sat Oct 04 12:46:04 2014 +0100 @@ -0,0 +1,25 @@ +object Application extends Controller { + + //SHA-1, SHA-256 + def mk_hash(s: String) : String = { + val hash_fun = MessageDigest.getInstance("SHA-1") + hash_fun.digest(s.getBytes).map{ "%02x".format(_) }.mkString + } + + def gt_cookie(c: Cookie) : Int = c.value.split("/") match { + case Array(s, h) + if (s.forall(_.isDigit) && mk_hash(s) == h) => s.toInt + case _ => 0 + } + + def mk_cookie(i: Int) : Cookie = { + val hash = mk_hash(i.toString) + Cookie("visits", s"$i/$hash") + } + + def index = Action { request => ... } +} + + + + diff -r 22f027da67ec -r 9c968d0de9a0 progs/scala/app4.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/progs/scala/app4.scala Sat Oct 04 12:46:04 2014 +0100 @@ -0,0 +1,27 @@ +object Application extends Controller { + + val salt = "my secret key" + + //SHA-1 + salt + def mk_hash(s: String) : String = { + val hash_fun = MessageDigest.getInstance("SHA-1") + hash_fun.digest((s + salt).getBytes).map{ "%02x".format(_) }.mkString + } + + def gt_cookie(c: Cookie) : Int = c.value.split("/") match { + case Array(s, h) + if (s.forall(_.isDigit) && mk_hash(s) == h) => s.toInt + case _ => 0 + } + + def mk_cookie(i: Int) : Cookie = { + val hash = mk_hash(i.toString) + Cookie("visits", s"$i/$hash") + } + + def index = Action { request => ... } +} + + + + diff -r 22f027da67ec -r 9c968d0de9a0 progs/scala/prove.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/progs/scala/prove.scala Sat Oct 04 12:46:04 2014 +0100 @@ -0,0 +1,179 @@ +import scala.language.implicitConversions +import scala.language.reflectiveCalls +import scala.util._ + +abstract class Term +case class Var(s: String) extends Term +case class Const(s: String) extends Term +case class Fun(s: String, ts: List[Term]) extends Term + +abstract class Form +case object True extends Form +case object False extends Form +case class Pred(s: String, ts: List[Term]) extends Form +case class Imp(f1: Form, f2: Form) extends Form +case class Says(p: String, f: Form) extends Form +case class And(f1: Form, f2: Form) extends Form +case class Or(f1: Form, f2: Form) extends Form +case class Sends(p: String, q: String, f: Form) extends Form +case class Enc(f1: Form, f2: Form) extends Form + +case class Judgement(gamma: Set[Form], f: Form) { + def lhs = gamma + def rhs = f +} + +// some syntactic sugar +implicit def FormOps(f1: Form) = new { + def -> (f2: Form) = Imp(f1, f2) +} +implicit def StringOps(p: String) = new { + def says (f: Form) = Says(p, f) +} +implicit def SetFormOps(gamma: Set[Form]) = new { + def |- (f: Form) : Judgement = Judgement(gamma, f) +} + +val Admin = "Admin" +val Bob = "Bob" +val Del = Pred("del_file", Nil) + +val Gamma: Set[Form] = + Set( (Admin says Del) -> Del, + Admin says ((Bob says Del) -> Del), + Bob says Del ) + +val goal = Gamma |- Del // request: provable or not? + +def partitions[A](s: Set[A]): Set[(A, Set[A])] = + s.map (e => (e, s - e)) + + +def prove(j: Judgement, sc: () => Unit) : Unit = { + if (j.lhs.contains(j.rhs)) sc () // Axiom rule + else { + prove1(j, sc); + for ((f, lhs_rest) <- partitions(j.lhs)) prove2(f, lhs_rest, j.rhs, sc) + } +} + +def prove1(j: Judgement, sc: () => Unit) : Unit = + j.rhs match { + case True => sc () + case False => () + case Imp(f1, f2) => prove(j.lhs + f1 |- f2, sc) + case Says(p, Enc(f1, f2)) => + prove(j.lhs |- Says(p, f1), + () => prove(j.lhs |- Says(p, f2), sc)) + case Says(p, f1) => prove(j.lhs |- f1, sc) + case Or(f1, f2) => + { prove(j.lhs |- f1, sc); + prove(j.lhs |- f2, sc) } + case And(f1, f2) => + prove(j.lhs |- f1, + () => prove(j.lhs |- f2, sc)) + case Sends(p, q, f) => prove(j.lhs + (p says f) |- (q says f), sc) + case _ => () + } + +def prove2(f: Form, lhs_rest: Set[Form], rhs: Form, sc: () => Unit) : Unit = + f match { + case True => prove(lhs_rest |- rhs, sc) + case False => sc () + case And(f1, f2) => + prove(lhs_rest + f1 + f2 |- rhs, sc) + case Imp(f1, f2) => + prove(lhs_rest |- f1, + () => prove(lhs_rest + f2 |- rhs, sc)) + case Sends(p, q, f) => + prove(lhs_rest |- (p says f), + () => prove(lhs_rest + (q says f) |- rhs, sc)) + case Enc(f1, f2) => + prove(lhs_rest |- f1, + () => prove(lhs_rest + f2 |- rhs, sc)) + case Or(f1, f2) => + prove(lhs_rest + f1 |- rhs, + () => prove(lhs_rest + f2 |- rhs, sc)) + case Says(p, Enc(f1, f2)) => + prove(lhs_rest |- Says(p, f2), + () => prove(lhs_rest + Says(p, f1) |- rhs, sc)) + case Says(p, Imp(f1, f2)) => + prove(lhs_rest |- Says(p, f1), + () => prove(lhs_rest + Says(p, f2) |- rhs, sc)) + + case _ => () + } + +// function that calls prove and returns immediately once a proof is found +def run (j : Judgement) : Unit = { + def sc () = { println ("Yes!"); throw new Exception } + Try(prove(j, sc)) getOrElse () +} + +run (goal) + +run (Set[Form]() |- False -> Del) +run (Set[Form]() |- True -> Del) +run (Set[Form]() |- Del -> True) +run (Set[Form]() |- Del -> Del) +run (Set[Form]() |- Del -> Or(False, Del)) + + +val Gamma1 : Set[Form] = + Set( Admin says ((Bob says Del) -> Del), + Bob says Del ) + +val goal1 = Gamma1 |- Del // not provable +run (goal1) + + +val f1 = "P" says Pred("F1", Nil) +val f2 = "Q" says Pred("F2", Nil) +run (Set[Form](And(f1, f2)) |- And(f2, f1)) + + +val Chr = "Christian" +val HoD = "Peter" +val Email = Pred("may_obtain_email", List(Const(Chr))) +val AtLib = Pred("is_at_library", List(Const(Chr))) +val Chr_Staff = Pred("is_staff", List(Const(Chr))) + +val Policy_HoD = (HoD says Chr_Staff) -> Chr_Staff +val Policy_Lib = And(Chr_Staff, AtLib) -> Email +val HoD_says = HoD says Chr_Staff + +run (Set[Form](AtLib, Policy_HoD, Policy_Lib) |- Email) + +println("Server Example") + +def Connect(p: String, q: String) : Form = + Pred("Connect", List(Var(p), Var(q))) + + +val A = "A" +val B = "B" +val S = "S" +val CAB = Connect(A, B) +val Msg = Pred("Msg", Nil) +val KAS = Pred("Kas", Nil) +val KBS = Pred("Kbs", Nil) +val KAB = Pred("Kab", Nil) + +val Gamma_big : Set[Form] = + Set( A says CAB, + Sends(A, S, CAB), + S says (CAB -> Enc(KAB, KAS)), + S says (CAB -> Enc(Enc(KAB, KBS), KAS)), + Sends(S, A, Enc(KAB, KAS)), + Sends(S, A, Enc(Enc(KAB, KBS), KAS)), + Sends(A, B, Enc(KAB, KBS)), + Sends(A, B, Enc(Msg, KAB)), + A says KAS, + B says KBS, + S says KAS, + A says (Enc(Msg, KAB)) + ) + +run (Gamma_big |- (B says Msg)) + + diff -r 22f027da67ec -r 9c968d0de9a0 progs/scala/random.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/progs/scala/random.scala Sat Oct 04 12:46:04 2014 +0100 @@ -0,0 +1,18 @@ + +val m = 16 +val a = 5 +val c = 1 +val X0 = 10 + +def ran(n: Int, X: Int) : Set[Int] = n match { + case 0 => Set() + case n => { + val X_new = (a * X + c) % m + Set(X) ++ ran(n - 1, X_new) + } +} + +for (i <- 0 to 16) { + val l = ran(16, i) + println(l.size.toString + " " + l.toString) +}