progs/ap3.js
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Fri, 06 May 2016 13:15:08 +0100
changeset 455 2d9e005100f4
parent 166 bba0504abcf0
child 531 35ffb7a7eafa
permissions -rw-r--r--
updated

function mk_hash(s) {
    return crypto.createHash('sha1').update(s).digest('hex')
}

function mk_cookie(c) {
    return c.toString() + "-" + mk_hash(c.toString())
}

function gt_cookie(s) {
    var splits = s.split("-", 2);
    var counter = parseInt(splits[0])
    if (mk_hash(counter.toString()) == splits[1]) {
        return counter
    } else { return 0 }
}

app.get('/', function(req, res){
    var counter = gt_cookie(req.cookies.counter) || 0;
    res.cookie('counter', mk_cookie(counter + 1));
    ...
});