index.js
2.72 KB
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
// Generated by CoffeeScript 1.12.7
(function() {
var PASSWORD, encrypted_file, fs, main, plaintext_file, yael;
fs = require('fs');
yael = require('yael');
PASSWORD = null;
plaintext_file = null;
encrypted_file = null;
main = function(options) {
var cache, orig_plaintext, plaintext;
cache = options.cache != null ? options.cache : options.cache = true;
if (cache) {
if (PASSWORD == null) {
PASSWORD = options.password;
}
if (plaintext_file == null) {
plaintext_file = options.plaintext_file;
}
if (encrypted_file == null) {
encrypted_file = options.encrypted_file || options.plaintext_file + '.yael';
}
} else {
PASSWORD = options.password;
plaintext_file = options.plaintext_file;
encrypted_file = options.encrypted_file || options.plaintext_file + '.yael';
}
if (!fs.existsSync(plaintext_file)) {
console.log("Reading secrets from " + encrypted_file + ".");
plaintext = main.readEncrypted();
return plaintext;
}
console.log("Reading secrets from " + plaintext_file + ".");
orig_plaintext = fs.readFileSync(plaintext_file).toString();
if (PASSWORD != null) {
if (!fs.existsSync(encrypted_file)) {
console.log("Saving " + encrypted_file + " because it doesn't exist.");
main.saveEncrypted();
} else {
plaintext = main.readEncrypted();
if (plaintext !== orig_plaintext) {
console.log("Updating " + encrypted_file + " because it has changed.");
main.saveEncrypted();
} else {
console.log("Not updating " + encrypted_file + " because nothing has changed.");
}
}
}
return orig_plaintext;
};
main.readEncrypted = function() {
var cipherObject, cipherbuffer, plaintext;
if (!fs.existsSync(encrypted_file)) {
throw Error("Cannot find " + plaintext_file + " or " + encrypted_file);
}
if (PASSWORD == null) {
throw Error("No encryption password was set.");
}
cipherbuffer = fs.readFileSync(encrypted_file);
cipherObject = new yael.CipherObject(cipherbuffer);
plaintext = yael.decryptSync(PASSWORD, cipherObject);
return plaintext.toString();
};
main.saveEncrypted = function() {
var cipherObject, cipherbuffer, plaintext;
if (!fs.existsSync(plaintext_file)) {
throw Error("Cannot find " + plaintext_file);
}
if (PASSWORD == null) {
throw Error("No encryption password was set.");
}
plaintext = fs.readFileSync(plaintext_file);
cipherObject = yael.encryptSync(PASSWORD, plaintext);
cipherbuffer = cipherObject.toBuffer();
return fs.writeFileSync(encrypted_file, cipherbuffer);
};
module.exports = main;
}).call(this);