Skip to content
This repository was archived by the owner on Mar 3, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ Finally, edit your `package.json` to add the `encrypt` and `decrypt` scripts, su

Here, the `encrypt` script will encrypt the `config.json` file as `config.json.cast5`, and the `decrypt` script will decrypt the `config.json.cast5` file as `config.json`. In both cases, the name of the encrypted file can be chosen arbitrarily.

You can choose to bypass manual password entry by supplying the name of an environmental variable you have set to store a password for you using the `--PW` flag:
```json
{
"name": "my-project",
"version": "0.0.1",
"scripts": {
...
"encrypt": "encrypt config.json config.json.cast5 --PW=ENV_VAR_PW_KEY",
"decrypt": "decrypt config.json.cast5 config.json --PW=ENV_VAR_PW_KEY"
}
}
```


Usage
-----

Expand Down
99 changes: 56 additions & 43 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,64 @@
var crypto = require("crypto")
var fs = require("fs")
var crypto = require("crypto")
var fs = require("fs")
var readline = require("readline")
var path = require("path")
var path = require("path")

module.exports = function(fn) {
var from = path.join(process.cwd(), process.argv[2])
var to = path.join(process.cwd(), process.argv[3])
var argv = require('minimist')(process.argv.slice(2));
var from = path.join(process.cwd(), process.argv[2])
var to = path.join(process.cwd(), process.argv[3])

var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})

var input = function(query, callback) {
var stdin = process.openStdin(),
i = 0;
process.stdin.on("data", function(char) {
char = char + "";
switch (char) {
case "\n":
case "\r":
case "\u0004":
stdin.pause();
break;
default:
process.stdout.write("\033[2K\033[200D" + Array(rl.line.length+1).join("*"));
i++;
break;
}
});
var input = function(query, callback) {
if (argv.PW){
if(process.env[argv.PW]){
callback(process.env[argv.PW], true);
}else{
console.error(new Error("Environmental variable \""+argv.PW+"\" has not been set."));
process.exit(1)
}
}else{
var stdin = process.openStdin(),
i = 0;
process.stdin.on("data", function(char) {
char = char + "";
switch (char) {
case "\n":
case "\r":
case "\u0004":
stdin.pause();
break;
default:
process.stdout.write("\033[2K\033[200D" + Array(rl.line.length+1).join("*"));
i++;
break;
}
});

rl.question(query, function(value) {
rl.history = rl.history.slice(1);
callback(value);
});
}

input("Enter the config password ("+path.basename(to)+"):\n", function(password) {
from = fs.createReadStream(from)
to = fs.createWriteStream(to)
fn = fn("cast5-cbc", password)
rl.question(query, function(value) {
rl.history = rl.history.slice(1);
callback(value);
});
}
}

input("Enter the config password ("+path.basename(to)+"):\n", function(password, env_var_bool) {
from = fs.createReadStream(from)
to = fs.createWriteStream(to)
fn = fn("cast5-cbc", password)

from.pipe(fn).pipe(to)
from.on("end", function () {
rl.write("done\n");
rl.close.bind(rl);
});

})
from.pipe(fn).pipe(to)
from.on("end", function () {
rl.write("done\n");
if(!env_var_bool){
rl.close.bind(rl);
}else{
process.exit();
}
});
})
}
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
"scripts": {
"test": "sh test.sh",
"encrypt": "encrypt config.json config.json.cast5",
"decrypt": "decrypt config.json.cast5 config.copy.json"
"decrypt": "decrypt config.json.cast5 config.copy.json",
"encrypt_env": "encrypt config.json config.json.cast5 --PW=ENV_PW",
"decrypt_env": "decrypt config.json.cast5 config.copy.json --PW=ENV_PW"
},
"dependencies": {
"minimist":"^1.2.0"
},
"bin": {
"encrypt": "./encrypt.js",
Expand Down
9 changes: 9 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,12 @@ echo "cynd1Lauper" | npm run decrypt

# diff the two files, and exit with its length
comm -3 config.json config.copy.json | wc -c | exit

# encyrpt using a password stored in the ENV_PW environmental variable
npm run encrypt_env

# decrypt using a password stored in the ENV_PW environmental variable
npm run decrypt_env

# diff the two files, and exit with its length
comm -3 config.json config.copy.json | wc -c | exit