-
Notifications
You must be signed in to change notification settings - Fork 23
/
lib.js
90 lines (78 loc) · 2.9 KB
/
lib.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
var config = require('./config');
var fs = require("fs");
var crypto = require('crypto');
var lib = module.exports = {
sendHTML: function (res, name) {
if (config.env === 'dev') {
res.sendFile(config.root + config.htmlPath[name]);
} else {
if (!config.htmlCache[name]) {
config.htmlCache[name] = fs.readFileSync(config.root + config.htmlPath[name], { encoding: 'utf8' });
}
res.header('Content-Type', 'text/html').send(config.htmlCache[name]);
}
},
uid: function () {
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
},
guid: function () {
var uid = lib.uid;
return uid() + uid() + uid() + uid() + uid() + uid() + uid() + uid();
},
crypto: (function () {
var ALGORITHM = 'AES-256-CBC';
var KEY = config.cryptoKey;
function base64urlencode(arg) {
var s = arg.toString('base64');
s = s.split('=')[0];
s = s.replace(/\+/g, '-');
s = s.replace(/\//g, '_');
return s;
}
function base64urldecode(arg) {
var s = arg;
s = s.replace(/-/g, '+');
s = s.replace(/_/g, '/');
switch (s.length % 4) {
case 0:
break;
case 2:
s += "==";
break;
case 3:
s += "=";
break;
default:
new Buffer('', 'base64');
}
return new Buffer(s, 'base64');
}
function forceBuffer(binaryOrBuffer) {
if (Buffer.isBuffer(binaryOrBuffer)) {
return binaryOrBuffer;
} else {
return new Buffer(binaryOrBuffer, 'binary');
}
}
return {
encode: function (plain_text) {
var iv = crypto.randomBytes(16);
var plaintext = new Buffer(plain_text, 'utf8');
var cipher = crypto.createCipheriv(ALGORITHM, KEY, iv);
var ciphertextStart = forceBuffer(cipher.update(plaintext));
var ciphertextEnd = forceBuffer(cipher.final());
var ciphertext = Buffer.concat([ciphertextStart, ciphertextEnd]);
return [base64urlencode(iv), base64urlencode(ciphertext)].join('.');
},
decode: function (content) {
var components = content.split(".");
var iv = base64urldecode(components[0]);
var ciphertext = base64urldecode(components[1]);
var cipher = crypto.createDecipheriv(ALGORITHM, KEY, iv);
var plaintext = cipher.update(ciphertext, 'binary', 'utf8');
plaintext += cipher.final('utf8');
return plaintext;
}
}
})()
};