-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
59 lines (43 loc) · 1.26 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
'use strict'
const session = require('koa-session')
const crypto = require('crypto')
module.exports = function(app, opts) {
opts = opts || {}
if (opts.signed === undefined) {
opts.signed = true
}
let key = opts.key || new Buffer('8734628jhsifud92')
let iv = opts.iv || new Buffer('cvdgfjf1837483jn')
let algorithm = opts.algorithm || 'aes-128-cbc'
opts.encode = encode
opts.decode = decode
app.use(session(app, opts))
/**
* utils
*/
function encrypt(text) {
let cipher = crypto.createCipheriv(algorithm, key, iv)
let crypted = cipher.update(text, 'utf8', 'base64')
crypted += cipher.final('base64')
return crypted
}
function decrypt(text) {
let decipher = crypto.createDecipheriv(algorithm, key, iv)
let dec = decipher.update(text, 'base64', 'utf8')
dec += decipher.final('utf8')
return dec
}
function encode(body) {
body = JSON.stringify(body)
let base64 = new Buffer(body).toString('base64')
return encrypt(base64)
}
function decode(text) {
let body = new Buffer(decrypt(text), 'base64').toString('utf8')
let json = JSON.parse(body)
// check if the cookie is expired
if (!json._expire) return null
if (json._expire < Date.now()) return null
return json
}
}