Attic/scala/app4.scala
author Christian Urban <urbanc@in.tum.de>
Wed, 31 May 2017 09:26:47 +0100
changeset 514 a118052cf1d4
parent 198 2ce98ee39990
permissions -rw-r--r--
updated
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
98
3d585e603927 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
     5
  //SHA-1 + salt
93
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
98
3d585e603927 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    11
  def gt_cookie(c: Cookie) : Int = c.value.split("/") match {
3d585e603927 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    12
    case Array(s, h) 
3d585e603927 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    13
      if (s.forall(_.isDigit) && mk_hash(s) == h) => s.toInt 
3d585e603927 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    14
    case _ => 0
3d585e603927 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    15
  }
93
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
  def mk_cookie(i: Int) : Cookie = {
98
3d585e603927 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    18
    val hash = mk_hash(i.toString)
3d585e603927 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    19
    Cookie("visits", s"$i/$hash")
93
82ac034dcc9d brought order into the repository
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
  }
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
  def index = Action { request => ... }
82ac034dcc9d brought order into the repository
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
}
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