@drifted/raven 0.0.12 → 0.0.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,11 +1,10 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- var CarrierPigeon = require('carrier-pigeon');
4
- var path = require('path');
5
- var fs = require('fs');
6
- var crypto = require('crypto');
7
- const JSONDataFile = require(path.join(__dirname, '..'));
8
-
3
+ const CarrierPigeon = require('carrier-pigeon');
4
+ const path = require('path');
5
+ const fs = require('fs');
6
+ const crypto = require('crypto');
7
+ const RavenDataFile = require(path.join(__dirname, '..'));
9
8
 
10
9
  var parser = new CarrierPigeon({strict: true});
11
10
  parser.commands('version', 'usage', 'show', 'secret', 'init', 'generate', 'expose', 'conceal', 'encrypt', 'decrypt');
@@ -19,11 +18,11 @@ parser.option('location', {type: 'file', default: process.cwd()});
19
18
  parser.option('verbose', {default: false});
20
19
 
21
20
  var options = parser.parse(process.argv);
22
- var command = options.command;
23
- delete options.command;
24
21
 
22
+ var command = options.command;
25
23
  process.chdir(options.location);
26
24
  delete options.location;
25
+ delete options.command;
27
26
 
28
27
 
29
28
 
@@ -36,11 +35,15 @@ if (options.verbose) {
36
35
 
37
36
 
38
37
  if (command == 'encrypt') {
39
- var file = new JSONDataFile(options);
38
+ var file = new RavenDataFile(options);
40
39
 
41
40
  var data = file.encrypt(options.data);
42
41
  if (options.json) {
43
- console.log(JSON.stringify({action: 'encrypt', options: options, data: data}, null, 2))
42
+ console.log(JSON.stringify({
43
+ action: 'encrypt',
44
+ options: options,
45
+ data: data
46
+ }, null, 2))
44
47
  } else {
45
48
  console.log('data:', data);
46
49
  }
@@ -53,7 +56,7 @@ if (command == 'decrypt') {
53
56
  var secret = options.secret;
54
57
 
55
58
  delete options.data;
56
- var file = new JSONDataFile(options);
59
+ var file = new RavenDataFile(options);
57
60
  var json = file.decrypt(data);
58
61
 
59
62
  try {
@@ -64,7 +67,11 @@ if (command == 'decrypt') {
64
67
  }
65
68
  }
66
69
  if (options.json) {
67
- console.log(JSON.stringify({action: 'decrypt', options: options, data: json}, null, 2))
70
+ console.log(JSON.stringify({
71
+ action: 'decrypt',
72
+ options: options,
73
+ data: json
74
+ }, null, 2))
68
75
  } else {
69
76
  console.log(json);
70
77
  }
@@ -73,7 +80,7 @@ if (command == 'decrypt') {
73
80
 
74
81
 
75
82
  if (command == 'generate') {
76
- JSONDataFile.generate().then((generated) => {
83
+ RavenDataFile.generate().then((generated) => {
77
84
  if (options.json) {
78
85
 
79
86
  console.log(JSON.stringify({
@@ -84,7 +91,7 @@ if (command == 'generate') {
84
91
  }, null, 2))
85
92
 
86
93
  } else {
87
- console.log('file created at', JSONDataFile.secretFile().replace(process.cwd()+path.sep, ''));
94
+ console.log('file created at', RavenDataFile.secretFile().replace(process.cwd()+path.sep, ''));
88
95
  }
89
96
  })
90
97
  }
@@ -92,14 +99,14 @@ if (command == 'generate') {
92
99
 
93
100
 
94
101
  if (command == 'init') {
95
- JSONDataFile.init().then((initialized) => {
102
+ RavenDataFile.init().then((initialized) => {
96
103
  if (options.json) {
97
104
  console.log(JSON.stringify({action: 'init', options: options, location: process.cwd(), success: initialized}, null, 2))
98
105
  } else {
99
106
  if (initialized) {
100
- console.log('file created at', JSONDataFile.secretFile().replace(process.cwd()+path.sep, ''));
107
+ console.log('file created at', RavenDataFile.secretFile().replace(process.cwd()+path.sep, ''));
101
108
  } else {
102
- console.log(JSONDataFile.secretFile().replace(process.cwd()+path.sep, ''), 'already exists.');
109
+ console.log(RavenDataFile.secretFile().replace(process.cwd()+path.sep, ''), 'already exists.');
103
110
  }
104
111
  }
105
112
  })
@@ -109,16 +116,16 @@ if (command == 'init') {
109
116
 
110
117
  if (command == 'secret') {
111
118
  if (options.json) {
112
- console.log(JSON.stringify({ secret: JSONDataFile.secret() }, null, 2));
119
+ console.log(JSON.stringify({ secret: RavenDataFile.secret() }, null, 2));
113
120
  } else {
114
- console.log('secret:', JSONDataFile.secret());
121
+ console.log('secret:', RavenDataFile.secret());
115
122
  }
116
123
  }
117
124
 
118
125
 
119
126
 
120
127
  if (command == 'show') {
121
- var json = JSON.parse(JSONDataFile.show());
128
+ var json = JSON.parse(RavenDataFile.show());
122
129
  if (options.json) {
123
130
  console.log(JSON.stringify(json, null, 2));
124
131
  } else {
@@ -129,11 +136,15 @@ if (command == 'show') {
129
136
 
130
137
 
131
138
  if (command == 'expose') {
132
- var file = new JSONDataFile(options);
133
- //console.log(file);
139
+ var file = new RavenDataFile(options);
134
140
  file.expose().then(() => {
135
141
  if (options.json) {
136
- console.log(JSON.stringify({file: file.shortPath, action: 'expose', options: options, secret: file.secret}, null, 2))
142
+ console.log(JSON.stringify({
143
+ file: file.shortPath(),
144
+ action: 'expose',
145
+ options: options,
146
+ secret: file.secret
147
+ }, null, 2))
137
148
  } else {
138
149
  console.log('');
139
150
  console.log(file.shortPath(), 'decrypted');
@@ -146,11 +157,15 @@ if (command == 'expose') {
146
157
 
147
158
 
148
159
  if (command == 'conceal') {
149
- var file = new JSONDataFile(options);
150
- //console.log(file);
160
+ var file = new RavenDataFile(options);
151
161
  file.conceal().then(() => {
152
162
  if (options.json) {
153
- console.log(JSON.stringify({file: file.shortPath, action: 'conceal', options: options, secret: file.secret}, null, 2))
163
+ console.log(JSON.stringify({
164
+ file: file.shortPath(),
165
+ action: 'conceal',
166
+ options: options,
167
+ secret: file.secret
168
+ }, null, 2))
154
169
  } else {
155
170
  console.log('');
156
171
  console.log(file.shortPath(), 'encrypted');
package/index.js CHANGED
@@ -1,14 +1,12 @@
1
1
  const path = require('path');
2
2
  const crypto = require('crypto');
3
3
  const fs = require('fs');
4
- const { fileTypeFromBuffer } = require('file-type');
5
4
  const ivLength = 16;
6
5
  const { Readable, pipeline, buffer } = require('stream');
7
6
  const Bufferable = require(path.join(__dirname, 'bufferable'));
8
- const { isUtf8, isAscii } = require('node:buffer');
9
7
 
10
- class RavenDataFile {
11
8
 
9
+ class RavenDataFile {
12
10
 
13
11
  constructor(options={}) {
14
12
  Object.assign(this, options)
@@ -131,7 +129,10 @@ class RavenDataFile {
131
129
  });
132
130
  })
133
131
  }
134
-
132
+
133
+ static createIV() {
134
+ return crypto.randomBytes(ivLength);
135
+ }
135
136
 
136
137
  static secret() {
137
138
  return crypto.randomBytes(32).toString('base64');
@@ -153,7 +154,6 @@ class RavenDataFile {
153
154
  }
154
155
 
155
156
  cipher(options={}) {
156
- //console.log(options);
157
157
  return crypto.createCipheriv(options.algorithm,
158
158
  Buffer.from(options.secret, 'base64'),
159
159
  options.iv);
@@ -166,7 +166,7 @@ class RavenDataFile {
166
166
  }
167
167
 
168
168
  encrypt(buffer) {
169
- const iv = crypto.randomBytes(ivLength);
169
+ const iv = RavenDataFile.createIV();
170
170
  const cipher = this.cipher({iv: iv, secret: this.secret, algorithm: this.algorithm});
171
171
 
172
172
  var encrypted = cipher.update(buffer, 'utf8', 'hex');
@@ -229,31 +229,30 @@ class RavenDataFile {
229
229
  conceal() {
230
230
  var self = this;
231
231
  return new Promise(async(resolve) => {
232
- const iv = crypto.randomBytes(ivLength);
232
+ const iv = RavenDataFile.createIV();
233
233
  const cipher = self.cipher({iv: iv, secret: self.secret, algorithm: self.algorithm});
234
234
 
235
- self.read().then(async (buffer) => {
235
+ self.read().then(async (buffer) => {
236
236
  var readable = Readable.from(buffer)
237
- const output = new Bufferable();
238
- readable.pipe(cipher).pipe(output);
237
+ const collector = new Bufferable();
238
+ readable.pipe(cipher).pipe(collector);
239
239
 
240
- output.on('finish', async() => {
240
+ collector.on('finish', async() => {
241
241
  const authTag = cipher.getAuthTag().toString('hex');
242
- var encrypted = output.buffer;
243
-
242
+ var encrypted = collector.buffer;
244
243
 
245
244
  fs.truncate(self.file, 0, () => {
246
- const stream = fs.createWriteStream(self.file, { flags: 'a' });
247
- stream.write(`${iv.toString('hex')}:${authTag}:`);
248
- stream.write(encrypted.toString('base64'));
249
- stream.end();
245
+ const destination = fs.createWriteStream(self.file, { flags: 'a' });
246
+ destination.write(`${iv.toString('hex')}:${authTag}:`);
247
+ destination.write(encrypted.toString('base64'));
248
+ destination.end();
250
249
 
251
- stream.on('finish', () => {
250
+ destination.on('finish', () => {
252
251
  resolve({success: true});
253
252
  });
254
253
  })
255
254
  });
256
- })
255
+ })
257
256
  })
258
257
  }
259
258
 
@@ -263,16 +262,17 @@ class RavenDataFile {
263
262
  return new Promise(async(resolve) => {
264
263
  self.read().then((buffer) => {
265
264
  const [ivHex, authTagHex, encryptedText] = buffer.toString().split(':');
266
-
265
+
267
266
  var readable = Readable.from(Buffer.from(encryptedText, 'base64'))
268
- const output = new Bufferable();
267
+ const collector = new Bufferable();
268
+
269
269
  const decipher = self.decipher({iv:ivHex, secret: self.secret, algorithm: self.algorithm});
270
270
  decipher.setAuthTag(Buffer.from(authTagHex, 'hex'));
271
+
272
+ readable.pipe(decipher).pipe(collector);
271
273
 
272
- readable.pipe(decipher).pipe(output);
273
-
274
- output.on('finish', () => {
275
- self.data = output.buffer;
274
+ collector.on('finish', () => {
275
+ self.data = collector.buffer;
276
276
 
277
277
  self.save().then(() => {
278
278
  resolve({success: true});
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "@drifted/raven",
3
- "version": "0.0.12",
3
+ "version": "0.0.14",
4
4
  "description": "",
5
5
  "homepage": "https://github.com/driftless-group/raven",
6
6
  "bin": {
7
- "raven": "./bin/cli.js"
7
+ "craven": "./bin/console.js",
8
+ "raven": "./bin/raven.js"
8
9
  },
9
10
  "repository": {
10
11
  "type": "git",
package/test/helpers.js CHANGED
@@ -8,7 +8,7 @@ const crypto = require('crypto');
8
8
 
9
9
 
10
10
  function run(...specifics) {
11
- var parts = [process.execPath, path.join(__dirname, '..', 'bin', 'cli.js')].concat(specifics);
11
+ var parts = [process.execPath, path.join(__dirname, '..', 'bin', 'raven.js')].concat(specifics);
12
12
  return new Promise((resolve, reject) => {
13
13
  exec(parts.join(' '), (err, stdout, stdin) => {
14
14
  if (err) {