equal
deleted
inserted
replaced
|
1 object Application extends Controller { |
|
2 |
|
3 val salt = "my secret key" |
|
4 |
|
5 //SHA-1 + 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: Cookie) : Int = c.value.split("/") match { |
|
12 case Array(s, h) |
|
13 if (s.forall(_.isDigit) && mk_hash(s) == h) => s.toInt |
|
14 case _ => 0 |
|
15 } |
|
16 |
|
17 def mk_cookie(i: Int) : Cookie = { |
|
18 val hash = mk_hash(i.toString) |
|
19 Cookie("visits", s"$i/$hash") |
|
20 } |
|
21 |
|
22 def index = Action { request => ... } |
|
23 } |
|
24 |
|
25 |
|
26 |
|
27 |