progs/ap4.js
author Christian Urban <urbanc@in.tum.de>
Thu, 15 Dec 2016 16:18:25 +0000
changeset 506 5446dc1fc992
parent 166 bba0504abcf0
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
166
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
var salt = 'secret key' 
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
function mk_hash(s) {
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
    return crypto.createHash('sha1').update(s).digest('hex')
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
}
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
function mk_cookie(c) {
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
    return c.toString() + '-' + mk_hash(c.toString() + salt)
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
}
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
function gt_cookie(s) {
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
    var splits = s.split("-", 2);
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
    var counter = parseInt(splits[0])
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
    var hash = splits[1]
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
    if (mk_hash(counter.toString() + salt) == hash) {
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
        return counter
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
    } else { return 0 }
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
}
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
app.get('/', function(req, res){ .... });