progs/app4.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Sun, 22 Sep 2013 15:35:50 +0100
changeset 93 82ac034dcc9d
child 98 3d585e603927
permissions -rw-r--r--
brought order into the repository
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
93
82ac034dcc9d brought order into the repository
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
object Application extends Controller {
82ac034dcc9d brought order into the repository
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
82ac034dcc9d brought order into the repository
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
  val salt = "my secret key"
82ac034dcc9d brought order into the repository
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
82ac034dcc9d brought order into the repository
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
  //SHA-1, SHA-256 + salt
82ac034dcc9d brought order into the repository
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
  def mk_hash(s: String) : String = {
82ac034dcc9d brought order into the repository
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
    val hash_fun = MessageDigest.getInstance("SHA-1")
82ac034dcc9d brought order into the repository
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
    hash_fun.digest((s + salt).getBytes).map{ "%02x".format(_) }.mkString
82ac034dcc9d brought order into the repository
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
  }
82ac034dcc9d brought order into the repository
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
82ac034dcc9d brought order into the repository
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
  def gt_cookie(c: Option[Cookie]) : Int = 
82ac034dcc9d brought order into the repository
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
    c.map(_.value.split("/")) match {
82ac034dcc9d brought order into the repository
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
      case Some(Array(s, h)) 
82ac034dcc9d brought order into the repository
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
        if (s.forall(_.isDigit) && mk_hash(s) == h) => s.toInt 
82ac034dcc9d brought order into the repository
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
      case _ => 0
82ac034dcc9d brought order into the repository
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
    }
82ac034dcc9d brought order into the repository
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
82ac034dcc9d brought order into the repository
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
  def mk_cookie(i: Int) : Cookie = {
82ac034dcc9d brought order into the repository
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
    val s = i.toString
82ac034dcc9d brought order into the repository
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
    Cookie("visits", s + "/" + mk_hash(s))
82ac034dcc9d brought order into the repository
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
  }
82ac034dcc9d brought order into the repository
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
   
82ac034dcc9d brought order into the repository
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
  def index = Action { request => ... }
82ac034dcc9d brought order into the repository
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
}
82ac034dcc9d brought order into the repository
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
82ac034dcc9d brought order into the repository
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
82ac034dcc9d brought order into the repository
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
82ac034dcc9d brought order into the repository
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28