@drifted/raven 0.0.11 → 0.0.13

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.
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');
@@ -199,9 +199,8 @@ class RavenDataFile {
199
199
  if (fs.existsSync(self.file) == false) {
200
200
  return reject(new Error('file doesnt exist.'))
201
201
  }
202
+
202
203
  fs.readFile(self.file, {}, async (err, data) => {
203
- //self.type = isAscii(data);
204
- //console.log('type', self.type);
205
204
  self.data = data;
206
205
 
207
206
  resolve(data);
@@ -230,33 +229,30 @@ class RavenDataFile {
230
229
  conceal() {
231
230
  var self = this;
232
231
  return new Promise(async(resolve) => {
233
- const iv = crypto.randomBytes(ivLength);
232
+ const iv = RavenDataFile.createIV();
234
233
  const cipher = self.cipher({iv: iv, secret: self.secret, algorithm: self.algorithm});
235
234
 
236
- //console.log(self);
237
-
238
- self.read().then(async (buffer) => {
235
+ self.read().then(async (buffer) => {
239
236
  var readable = Readable.from(buffer)
240
- const output = new Bufferable();
241
- readable.pipe(cipher).pipe(output);
237
+ const collector = new Bufferable();
238
+ readable.pipe(cipher).pipe(collector);
242
239
 
243
- output.on('finish', async() => {
240
+ collector.on('finish', async() => {
244
241
  const authTag = cipher.getAuthTag().toString('hex');
245
- var encrypted = output.buffer;
246
-
242
+ var encrypted = collector.buffer;
247
243
 
248
244
  fs.truncate(self.file, 0, () => {
249
- const stream = fs.createWriteStream(self.file, { flags: 'a' });
250
- stream.write(`${iv.toString('hex')}:${authTag}:`);
251
- stream.write(encrypted.toString('base64'));
252
- 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();
253
249
 
254
- stream.on('finish', () => {
250
+ destination.on('finish', () => {
255
251
  resolve({success: true});
256
252
  });
257
253
  })
258
254
  });
259
- })
255
+ })
260
256
  })
261
257
  }
262
258
 
@@ -266,16 +262,17 @@ class RavenDataFile {
266
262
  return new Promise(async(resolve) => {
267
263
  self.read().then((buffer) => {
268
264
  const [ivHex, authTagHex, encryptedText] = buffer.toString().split(':');
269
-
265
+
270
266
  var readable = Readable.from(Buffer.from(encryptedText, 'base64'))
271
- const output = new Bufferable();
267
+ const collector = new Bufferable();
268
+
272
269
  const decipher = self.decipher({iv:ivHex, secret: self.secret, algorithm: self.algorithm});
273
270
  decipher.setAuthTag(Buffer.from(authTagHex, 'hex'));
271
+
272
+ readable.pipe(decipher).pipe(collector);
274
273
 
275
- readable.pipe(decipher).pipe(output);
276
-
277
- output.on('finish', () => {
278
- self.data = output.buffer;
274
+ collector.on('finish', () => {
275
+ self.data = collector.buffer;
279
276
 
280
277
  self.save().then(() => {
281
278
  resolve({success: true});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drifted/raven",
3
- "version": "0.0.11",
3
+ "version": "0.0.13",
4
4
  "description": "",
5
5
  "homepage": "https://github.com/driftless-group/raven",
6
6
  "bin": {
package/test/cli.test.js CHANGED
@@ -22,11 +22,9 @@ describe('raven', function() {
22
22
  before(async() => {
23
23
  var file = path.join(__dirname, 'data', '1984.pdf');
24
24
  var encrypted = path.join(__dirname, 'data','1984.pdf.encrypted');
25
-
26
25
  if (fs.existsSync(encrypted)) {
27
26
  await remove(encrypted)
28
- }
29
-
27
+ }
30
28
  await copy(file, encrypted);
31
29
  })
32
30
 
@@ -156,6 +154,7 @@ describe('raven', function() {
156
154
 
157
155
  run('conceal', '-f', encrypted, '-s', secret).then(() => {
158
156
  compare(original, encrypted).then((response) => {
157
+ //console.log(response);
159
158
  assert.equal(response.equal, false);
160
159
 
161
160
  run('expose', '-f', encrypted, '-s', secret).then(() => {
package/test/helpers.js CHANGED
@@ -73,13 +73,15 @@ function compare(file1, file2) {
73
73
 
74
74
  return new Promise((resolve, reject) => {
75
75
  if (fs.statSync(file1).size !== fs.statSync(file2).size) {
76
- response.equal = true;
76
+ response.equal = false;
77
+ response.reason = 'size';
77
78
  return resolve(response)
78
79
  } else {
79
80
  const hash1 = crypto.createHash('sha256').update(fs.readFileSync(file1)).digest('hex');
80
81
  const hash2 = crypto.createHash('sha256').update(fs.readFileSync(file2)).digest('hex');
81
82
 
82
83
  if (hash1 !== hash2) {
84
+ response.reason = 'hash';
83
85
  response.equal = false;
84
86
  resolve(response);
85
87
  } else {