Attic/scala/app4.scala
changeset 198 2ce98ee39990
parent 197 9c968d0de9a0
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Attic/scala/app4.scala	Sat Oct 04 13:17:18 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 => ... }
+}
+
+
+
+