@drifted/raven 0.0.12 → 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.
Files changed (2) hide show
  1. package/index.js +25 -25
  2. package/package.json +1 -1
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,6 +1,6 @@
1
1
  {
2
2
  "name": "@drifted/raven",
3
- "version": "0.0.12",
3
+ "version": "0.0.13",
4
4
  "description": "",
5
5
  "homepage": "https://github.com/driftless-group/raven",
6
6
  "bin": {