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