progs/ap3.js
changeset 166 bba0504abcf0
child 531 35ffb7a7eafa
equal deleted inserted replaced
165:6f84ad98cf49 166:bba0504abcf0
       
     1 function mk_hash(s) {
       
     2     return crypto.createHash('sha1').update(s).digest('hex')
       
     3 }
       
     4 
       
     5 function mk_cookie(c) {
       
     6     return c.toString() + "-" + mk_hash(c.toString())
       
     7 }
       
     8 
       
     9 function gt_cookie(s) {
       
    10     var splits = s.split("-", 2);
       
    11     var counter = parseInt(splits[0])
       
    12     if (mk_hash(counter.toString()) == splits[1]) {
       
    13         return counter
       
    14     } else { return 0 }
       
    15 }
       
    16 
       
    17 app.get('/', function(req, res){
       
    18     var counter = gt_cookie(req.cookies.counter) || 0;
       
    19     res.cookie('counter', mk_cookie(counter + 1));
       
    20     ...
       
    21 });
       
    22