progs/scala/app3.scala
changeset 197 9c968d0de9a0
parent 98 3d585e603927
equal deleted inserted replaced
196:22f027da67ec 197:9c968d0de9a0
       
     1 object Application extends Controller {
       
     2 
       
     3   //SHA-1, SHA-256
       
     4   def mk_hash(s: String) : String = {
       
     5     val hash_fun = MessageDigest.getInstance("SHA-1")
       
     6     hash_fun.digest(s.getBytes).map{ "%02x".format(_) }.mkString
       
     7   }
       
     8 
       
     9   def gt_cookie(c: Cookie) : Int = c.value.split("/") match {
       
    10     case Array(s, h) 
       
    11       if (s.forall(_.isDigit) && mk_hash(s) == h) => s.toInt 
       
    12     case _ => 0
       
    13   }
       
    14 
       
    15   def mk_cookie(i: Int) : Cookie = {
       
    16     val hash = mk_hash(i.toString)
       
    17     Cookie("visits", s"$i/$hash")
       
    18   }
       
    19    
       
    20   def index = Action { request => ... }     
       
    21 }
       
    22 
       
    23 
       
    24 
       
    25