forked from dlom/anesidora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencryption.js
55 lines (48 loc) · 1.39 KB
/
encryption.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
var crypto = require("crypto");
var iv = new Buffer("");
var PADDING_LENGTH = 16;
var PADDING = Array(PADDING_LENGTH).join("\0");
var createCryptor = function(key) {
key = new Buffer(key);
return function(data) {
var cipher = crypto.createCipheriv("bf-ecb", key, iv);
cipher.setAutoPadding(false);
var padLength = PADDING_LENGTH - (data.length % PADDING_LENGTH);
if (padLength === PADDING_LENGTH) {
padLength = 0;
}
try {
return Buffer.concat([
cipher.update(data + PADDING.substr(0, padLength)),
cipher.final()
]);
} catch (e) {
return null;
}
};
};
var createDecryptor = function(key) {
key = new Buffer(key);
return function(data) {
var cipher = crypto.createDecipheriv("bf-ecb", key, iv);
cipher.setAutoPadding(false);
try {
return Buffer.concat([
cipher.update(data),
cipher.final()
]);
} catch (e) {
return null;
}
};
};
exports.decrypt = function(password, ciphered) {
var blowfish = createDecryptor(password);
var buff = blowfish(new Buffer(ciphered, "hex"));
return buff;
};
exports.encrypt = function(password, plain) {
var blowfish = createCryptor(password);
var buff = blowfish(plain);
return buff;
};