progs/app4.scala
changeset 93 82ac034dcc9d
child 98 3d585e603927
equal deleted inserted replaced
92:af1aeec504cb 93:82ac034dcc9d
       
     1 object Application extends Controller {
       
     2 
       
     3   val salt = "my secret key"
       
     4 
       
     5   //SHA-1, SHA-256 + salt
       
     6   def mk_hash(s: String) : String = {
       
     7     val hash_fun = MessageDigest.getInstance("SHA-1")
       
     8     hash_fun.digest((s + salt).getBytes).map{ "%02x".format(_) }.mkString
       
     9   }
       
    10 
       
    11   def gt_cookie(c: Option[Cookie]) : Int = 
       
    12     c.map(_.value.split("/")) match {
       
    13       case Some(Array(s, h)) 
       
    14         if (s.forall(_.isDigit) && mk_hash(s) == h) => s.toInt 
       
    15       case _ => 0
       
    16     }
       
    17 
       
    18   def mk_cookie(i: Int) : Cookie = {
       
    19     val s = i.toString
       
    20     Cookie("visits", s + "/" + mk_hash(s))
       
    21   }
       
    22    
       
    23   def index = Action { request => ... }
       
    24 }
       
    25 
       
    26 
       
    27 
       
    28