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