@secrez/fs 1.0.8 → 1.1.0

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/README.md CHANGED
@@ -20,6 +20,13 @@ API documentation
20
20
 
21
21
  ## History
22
22
 
23
+ **1.1.0**
24
+
25
+ - strengthen password-based export encryption in `FileCipher`: new **v2** format (PBKDF2, random salt in file, iterations out of band)
26
+ - write vault blobs and cache files with mode `0o600` via `VaultPermissions` (FS-1)
27
+ - add `fileCipherLegacy` module and `FileCipherLegacy` export for **v1** password-encrypted files (import compatibility and test fixtures)
28
+ - require Node.js 20 or later (`engines.node >=20.0.0`)
29
+
23
30
  **1.0.3**
24
31
 
25
32
  - Move `encryptFile` and `decryptFile` from ExternalFs to FileCipher
@@ -31,21 +38,22 @@ API documentation
31
38
  ## Test coverage
32
39
 
33
40
  ```
34
- 107 passing (44s)
41
+ 108 passing (5s)
35
42
 
36
43
  -----------------------|---------|----------|---------|---------|-----------------------------------
37
44
  File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
38
45
  -----------------------|---------|----------|---------|---------|-----------------------------------
39
- All files | 81.78 | 67.77 | 86.27 | 81.44 |
40
- DataCache.js | 89.41 | 74.07 | 90.9 | 89.28 | 17-23,122-123,149
46
+ All files | 81.97 | 67.87 | 86.87 | 81.65 |
47
+ DataCache.js | 89.53 | 74.07 | 90.9 | 89.41 | 18-24,123-124,150
41
48
  ExternalFs.js | 94.02 | 76.92 | 100 | 93.93 | 9,29,35,54
42
- FileCipher.js | 88.63 | 73.33 | 100 | 88.63 | 8,52,80,85,94
49
+ FileCipher.js | 88.05 | 73.8 | 100 | 88.05 | 9,39,76,85,91,95,131,136
43
50
  FsUtils.js | 100 | 100 | 100 | 100 |
44
51
  GitConflictChecker.js | 72.81 | 61.9 | 90 | 72.81 | ...95,204,222,254,259,277,289,308
45
52
  InternalFs.js | 85.09 | 71.87 | 85.18 | 84.98 | ...33,344,364,423-428,460,470-487
46
53
  Messages.js | 100 | 100 | 100 | 100 |
47
54
  Node.js | 66.1 | 54.81 | 78.72 | 65.31 | ...05,644-648,666,669,683-720,729
48
- Tree.js | 89.91 | 74.09 | 86.48 | 89.63 | ...39,466,476,530,622,630,661-672
55
+ Tree.js | 89.91 | 74.09 | 86.48 | 89.63 | ...44,471,481,535,627,635,666-677
56
+ fileCipherLegacy.js | 100 | 100 | 100 | 100 |
49
57
  -----------------------|---------|----------|---------|---------|-----------------------------------
50
58
  ```
51
59
 
package/coverage.report CHANGED
@@ -1,203 +1,199 @@
1
1
 
2
- > @secrez/fs@1.0.8 test /Users/francescosullo/Projects/Secrez/secrez/packages/fs
2
+ > @secrez/fs@1.1.0 test /Users/francescosullo/Projects/Secrez/secrez/packages/fs
3
3
  > cross-env NODE_ENV=test nyc --reporter=lcov --reporter=text mocha test/*.test.js test/**/*.test.js --exit
4
4
 
5
5
 
6
6
 
7
7
  #DataCache
8
- ✓ should initialize a dataCache
9
- ✓ should put and get data
10
- ✓ should load data from disk, if any
11
- ✓ should not load if no data
12
- ✓ should load plaintext data from disk, if any
13
- ✓ should load encrypted data from disk, if any
8
+ ✔ should initialize a dataCache
9
+ ✔ should put and get data
10
+ ✔ should load data from disk, if any
11
+ ✔ should not load if no data
12
+ ✔ should load plaintext data from disk, if any
13
+ ✔ should load encrypted data from disk, if any
14
14
 
15
15
  #ExternalFs
16
16
  getNormalizedPath
17
- ✓ should normalize "~/fileSystems"
18
- ✓ should normalize "~"
19
- ✓ should normalize "/var"
17
+ ✔ should normalize "~/fileSystems"
18
+ ✔ should normalize "~"
19
+ ✔ should normalize "/var"
20
20
  fileCompletion
21
- ✓ should return a list of files
22
- ✓ should return a list of only directories
23
- ✓ should return a list of only files
24
- ✓ should return the file itself, if it is not a directory
25
- ✓ should return the list of the files satisfying wildcards
26
- ✓ should return an empty list if the files does not exist
21
+ ✔ should return a list of files
22
+ ✔ should return a list of only directories
23
+ ✔ should return a list of only files
24
+ ✔ should return the file itself, if it is not a directory
25
+ ✔ should return the list of the files satisfying wildcards
26
+ ✔ should return an empty list if the files does not exist
27
27
  getVersionedBasename
28
- ✓ should version a file
28
+ ✔ should version a file
29
29
  isDir
30
- ✓ should confirm that "utils" is a dir
31
- ✓ should return that "config.test.js" is not a dir
32
- ✓ should return that a not-existent file is not a dir
30
+ ✔ should confirm that "utils" is a dir
31
+ ✔ should return that "config.test.js" is not a dir
32
+ ✔ should return that a not-existent file is not a dir
33
33
  isFile
34
- ✓ should return that "config.test.js" is a file
35
- ✓ should confirm that "utils" is not a file
36
- ✓ should return that a not-existent file is not a dir
34
+ ✔ should return that "config.test.js" is a file
35
+ ✔ should confirm that "utils" is not a file
36
+ ✔ should return that a not-existent file is not a dir
37
37
 
38
38
  #FileCipher
39
39
  encrypt/decrypt external file
40
- ✓ should encrypt a file using a specific password
41
- ✓ should encrypt a file using a shared key
42
- ✓ should encrypt a file using user's own public key
40
+ ✔ should encrypt a file using a specific password (v2)
41
+ ✔ should decrypt v1 password-encrypted files
42
+ ✔ should encrypt a file using a shared key
43
+ ✔ should encrypt a file using user's own public key
43
44
 
44
45
  #FsUtils
45
46
  preParseCommandLine
46
- ✓ should preParse a command line
47
- ✓ should preParse a command line with escaped pars
48
- ✓ should preParse a command line using quotes for params
47
+ ✔ should preParse a command line
48
+ ✔ should preParse a command line with escaped pars
49
+ ✔ should preParse a command line using quotes for params
49
50
  parseCommandLine
50
- ✓ should parse a command line with definitions
51
- ✓ should return a {} w/out definitions
52
- ✓ should return a {} w/out params
53
- ✓ should return an _unknown if not supported option is passed
51
+ ✔ should parse a command line with definitions
52
+ ✔ should return a {} w/out definitions
53
+ ✔ should return a {} w/out params
54
+ ✔ should return an _unknown if not supported option is passed
54
55
  filterLs
55
- ✓ should filter an array of files and return itself
56
- ✓ should filter an array of files starting from "te"
57
- ✓ should filter an array of files of type .log
58
- ✓ should filter an array of files and find the folders
59
- ✓ should get all the files
60
- ✓ should return an empty array if nothing is found
61
- ✓ should return an empty array if parameters are missed or empty
62
- ✓ should return all the list array if files are missed
56
+ ✔ should filter an array of files and return itself
57
+ ✔ should filter an array of files starting from "te"
58
+ ✔ should filter an array of files of type .log
59
+ ✔ should filter an array of files and find the folders
60
+ ✔ should get all the files
61
+ ✔ should return an empty array if nothing is found
62
+ ✔ should return an empty array if parameters are missed or empty
63
+ ✔ should return all the list array if files are missed
63
64
 
64
65
  #GitConflictChecker
65
- ✓ should detect non-git repository
66
- ✓ should return null status for non-git repository
67
- ✓ should not detect conflict risk in non-git repository
68
- with real GitHub repository
69
- ✓ should detect git repository
70
- ✓ should get git status when up to date with remote (3228ms)
71
- ✓ should detect conflict risk when behind remote (5175ms)
72
- ✓ should get warning message for conflict risk
73
- ✓ should not show alert again after user proceeds (3137ms)
74
- ✓ should reset cache
66
+ ✔ should detect non-git repository
67
+ ✔ should return null status for non-git repository
68
+ ✔ should not detect conflict risk in non-git repository
69
+ with local git repository
70
+ ✔ should detect git repository
71
+ ✔ should get git status when up to date with remote (73ms)
72
+ ✔ should detect conflict risk when behind remote (164ms)
73
+ ✔ should get warning message for conflict risk
74
+ ✔ should not show alert again after user proceeds (73ms)
75
+ ✔ should reset cache
75
76
 
76
77
  #InternalFs
77
78
  #constructor
78
- ✓ should instantiate the internal file system and initialize it
79
- ✓ should initialize the internal fs
80
- ✓ should throw if passing not a Secrez instance
79
+ ✔ should instantiate the internal file system and initialize it
80
+ ✔ should initialize the internal fs
81
+ ✔ should throw if passing not a Secrez instance
81
82
  getFullPath
82
- ✓ should return the fullPath of an entry
83
+ ✔ should return the fullPath of an entry
83
84
  normalizePath
84
- ✓ should normalize a path
85
- ✓ should throw if the path is empty, is not a string or is longer than 255 chars
85
+ ✔ should normalize a path
86
+ ✔ should throw if the path is empty, is not a string or is longer than 255 chars
86
87
  getNormalizedPath
87
- ✓ should normalize a path
88
+ ✔ should normalize a path
88
89
  make
89
- ✓ should create directories and files
90
- ✓ should throw trying to re-add the same entry
90
+ ✔ should create directories and files
91
+ ✔ should throw trying to re-add the same entry
91
92
  change
92
- ✓ should create directories and files and update them
93
- ✓ should return an error
94
- ✓ should move files between datasets
93
+ ✔ should create directories and files and update them
94
+ ✔ should return an error
95
+ ✔ should move files between datasets
95
96
  remove
96
- ✓ should remove a file from the tree
97
+ ✔ should remove a file from the tree
97
98
  load
98
- ✓ should create directories and files and loading a tree from disk
99
- ✓ should create directories and files, and load a tree from disk
100
- ✓ should create directories and files, delete everything and loading a tree from disk
101
- ✓ should compress and reload an empty set of entries
99
+ ✔ should create directories and files and loading a tree from disk
100
+ ✔ should create directories and files, and load a tree from disk
101
+ ✔ should create directories and files, delete everything and loading a tree from disk
102
+ ✔ should compress and reload an empty set of entries
102
103
  #getFileList
103
- ✓ should get the current folder dir
104
+ ✔ should get the current folder dir
104
105
 
105
106
  #Node
106
107
  #constructor
107
- ✓ should instantiate the Node
108
- ✓ should throw if passing a rot without required parameters
109
- ✓ should throw if not passing an object
110
- ✓ should throw if not passing wrong type
108
+ ✔ should instantiate the Node
109
+ ✔ should throw if passing a rot without required parameters
110
+ ✔ should throw if not passing an object
111
+ ✔ should throw if not passing wrong type
111
112
  #getName && #getFile && #getContent && getOptions
112
- ✓ should get name, file and options
113
- ✓ should throw if version not found
113
+ ✔ should get name, file and options
114
+ ✔ should throw if version not found
114
115
  #getFindRe
115
- ✓ should get a re to check a filepath
116
+ ✔ should get a re to check a filepath
116
117
  #getNames
117
- ✓ should get the names of all children
118
- ✓ should throw if trying to get children of a file
118
+ ✔ should get the names of all children
119
+ ✔ should throw if trying to get children of a file
119
120
  #find
120
- ✓ should find all childrens satisfying criterias
121
+ ✔ should find all childrens satisfying criterias
121
122
  #add
122
- ✓ should add children to root
123
- ✓ should throw if node is a file
123
+ ✔ should add children to root
124
+ ✔ should throw if node is a file
124
125
  #move
125
- ✓ should rename a node
126
- ✓ should move a node
127
- ✓ should throw trying to move root
128
- ✓ should throw trying to modify a node with different id
126
+ ✔ should rename a node
127
+ ✔ should move a node
128
+ ✔ should throw trying to move root
129
+ ✔ should throw trying to modify a node with different id
129
130
  #remove
130
- ✓ should remove a node
131
- ✓ should remove itself
132
- ✓ should throw trying to remove root
131
+ ✔ should remove a node
132
+ ✔ should remove itself
133
+ ✔ should throw trying to remove root
133
134
  #toCompressedJSON && Node#fromJSON
134
- ✓ should prepare a json for saving
135
- ✓ should build an index from a json file
135
+ ✔ should prepare a json for saving
136
+ ✔ should build an index from a json file
136
137
  #getChildFromPath
137
- ✓ should find a node starting from a path
138
- ✓ should find the last ancestor starting from a path
139
- ✓ should throw if the path is incorrect or does not exist
140
- ✓ should throw if looking for last ancestor and find existent node
138
+ ✔ should find a node starting from a path
139
+ ✔ should find the last ancestor starting from a path
140
+ ✔ should throw if the path is incorrect or does not exist
141
+ ✔ should throw if looking for last ancestor and find existent node
141
142
  #getPathToChild
142
- ✓ should find a node starting from a path
143
- ✓ should throw if child is not a Node
144
- ✓ should throw if child belows to another tree
143
+ ✔ should find a node starting from a path
144
+ ✔ should throw if child is not a Node
145
+ ✔ should throw if child belows to another tree
145
146
  #findDirectChildByName && #findChildById
146
- ✓ should find a direct child by name and by id
147
- ✓ should throw if looking for child of a file
148
- ✓ should throw if looking for child of a file
147
+ ✔ should find a direct child by name and by id
148
+ ✔ should throw if looking for child of a file
149
+ ✔ should throw if looking for child of a file
149
150
 
150
151
  #Tree
151
152
  #constructor
152
- ✓ should instantiate the Tree
153
- ✓ should throw if passing not an Secrez instance
153
+ ✔ should instantiate the Tree
154
+ ✔ should throw if passing not an Secrez instance
154
155
  #load
155
- ✓ should load an empty Tree
156
- ✓ should do nothing if already loaded
156
+ ✔ should load an empty Tree
157
+ ✔ should do nothing if already loaded
157
158
  getEntryDetails
158
- ✓ should return the entry details of a node
159
+ ✔ should return the entry details of a node
159
160
  #Fix
160
- ✓ should load a tree with tags
161
+ ✔ should load a tree with tags
161
162
  1) should simulate a conflict in the repo and recover lost entries when a valid tree exists
162
- ✓ should simulate a lost index in the repo and recover the entries when no valid tree is found (45ms)
163
+ ✔ should simulate a lost index in the repo and recover the entries when no valid tree is found
163
164
  Multi data sets
164
- ✓ should load the trees of two data sets
165
+ ✔ should load the trees of two data sets
165
166
 
166
167
 
167
- 106 passing (39s)
168
+ 107 passing (3s)
168
169
  1 failing
169
170
 
170
171
  1) #Tree
171
172
  #Fix
172
173
  should simulate a conflict in the repo and recover lost entries when a valid tree exists:
173
-
174
- AssertionError: expected [ '/B/D/g', '/E/L/N', '/E/c', …(1) ] to deeply equal [ '/B/D/g', '/B/b', '/E/L/N', '/E/c' ]
175
- + expected - actual
176
-
177
- [
178
- "/B/D/g"
179
- + "/B/b"
180
- "/E/L/N"
181
- "/E/c"
182
- - "/REC_20251010150924/d"
183
- ]
184
-
185
- at Context.<anonymous> (test/Tree.test.js:306:14)
174
+ RangeError: Invalid count value: -1
175
+ at String.repeat (<anonymous>)
176
+ at Crypto.getTimestampWithMicroseconds (/Users/francescosullo/Projects/Secrez/secrez/packages/crypto/src/index.js:417:39)
177
+ at Secrez.encryptEntry (/Users/francescosullo/Projects/Secrez/secrez/packages/core/src/Secrez.js:228:20)
178
+ at Tree.add (src/Tree.js:11:3592)
179
+ at InternalFs.make (src/InternalFs.js:4:1671)
180
+ at async Context.<anonymous> (test/Tree.test.js:253:7)
186
181
 
187
182
 
188
183
 
189
184
  -----------------------|---------|----------|---------|---------|-----------------------------------
190
185
  File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
191
186
  -----------------------|---------|----------|---------|---------|-----------------------------------
192
- All files | 80.57 | 67.11 | 86.27 | 80.21 |
193
- DataCache.js | 89.41 | 74.07 | 90.9 | 89.28 | 17-23,122-123,149
187
+ All files | 76.07 | 64.36 | 85.62 | 76.01 |
188
+ DataCache.js | 89.53 | 74.07 | 90.9 | 89.41 | 18-24,123-124,150
194
189
  ExternalFs.js | 94.02 | 76.92 | 100 | 93.93 | 9,29,35,54
195
- FileCipher.js | 88.63 | 73.33 | 100 | 88.63 | 8,52,80,85,94
190
+ FileCipher.js | 88.05 | 73.8 | 100 | 88.05 | 9,39,76,85,91,95,131,136
196
191
  FsUtils.js | 100 | 100 | 100 | 100 |
197
192
  GitConflictChecker.js | 72.81 | 61.9 | 90 | 72.81 | ...95,204,222,254,259,277,289,308
198
193
  InternalFs.js | 82.35 | 70.83 | 85.18 | 82.21 | ...64,377-385,423-428,460,470-487
199
194
  Messages.js | 100 | 100 | 100 | 100 |
200
- Node.js | 66.1 | 54.81 | 78.72 | 65.31 | ...05,644-648,666,669,683-720,729
201
- Tree.js | 87.46 | 71.68 | 86.48 | 87.11 | ...39,466,476,530,622,630,661-672
195
+ Node.js | 65.81 | 53.7 | 78.72 | 65.02 | ...05,644-648,666,669,683-720,729
196
+ Tree.js | 70.29 | 57.83 | 81.08 | 70.86 | ...44,471,481,535,627,635,666-677
197
+ fileCipherLegacy.js | 100 | 100 | 100 | 100 |
202
198
  -----------------------|---------|----------|---------|---------|-----------------------------------
203
199
   ELIFECYCLE  Test failed. See above for more details.
package/package.json CHANGED
@@ -1,24 +1,23 @@
1
1
  {
2
2
  "name": "@secrez/fs",
3
- "version": "1.0.8",
3
+ "version": "1.1.0",
4
4
  "license": "MIT",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
8
8
  "dependencies": {
9
- "@secrez/core": "~1.0.6",
10
- "@secrez/crypto": "~1.0.5",
11
- "@secrez/utils": "~1.0.5",
9
+ "@secrez/core": "~1.1.0",
10
+ "@secrez/crypto": "~1.1.0",
11
+ "@secrez/utils": "~1.1.0",
12
12
  "command-line-args": "^5.2.1",
13
13
  "fs-extra": "^9.1.0",
14
14
  "lodash": "^4.17.21"
15
15
  },
16
16
  "devDependencies": {
17
- "@secrez/test-helpers": "~2.0.1",
17
+ "@secrez/test-helpers": "~2.0.3",
18
18
  "chai": "^4.5.0",
19
19
  "cross-env": "^7.0.3",
20
- "dotenv": "^17.2.2",
21
- "mocha": "^7.2.0",
20
+ "mocha": "^10.8.2",
22
21
  "nyc": "^15.1.0"
23
22
  },
24
23
  "description": "The Secrez fs library.",
@@ -32,7 +31,7 @@
32
31
  "email": "francesco@sullo.co"
33
32
  },
34
33
  "engines": {
35
- "node": ">=10.0.0"
34
+ "node": ">=20.0.0"
36
35
  },
37
36
  "scripts": {
38
37
  "all-tests": "find test/** -name '*.test.js' | xargs ./node_modules/.bin/mocha -R spec",
package/src/DataCache.js CHANGED
@@ -1,5 +1,6 @@
1
1
  const path = require("path");
2
2
  const fs = require("fs-extra");
3
+ const { VaultPermissions } = require("@secrez/core");
3
4
 
4
5
  class DataCache {
5
6
  constructor(dataPath, secrez) {
@@ -8,7 +9,7 @@ class DataCache {
8
9
  this.encrypted = {};
9
10
  if (dataPath) {
10
11
  this.dataPath = dataPath;
11
- fs.ensureDirSync(dataPath);
12
+ VaultPermissions.ensureDirSync(dataPath);
12
13
  this.ensured = {};
13
14
  }
14
15
  }
@@ -102,7 +103,7 @@ class DataCache {
102
103
 
103
104
  ensure(key) {
104
105
  if (!this.ensured[key]) {
105
- fs.ensureDirSync(path.join(this.dataPath, key));
106
+ VaultPermissions.ensureDirSync(path.join(this.dataPath, key));
106
107
  this.ensured[key] = true;
107
108
  }
108
109
  }
@@ -124,7 +125,7 @@ class DataCache {
124
125
  }
125
126
  }
126
127
  if (changed) {
127
- await fs.writeFile(p, content);
128
+ await VaultPermissions.writeFile(p, content);
128
129
  }
129
130
  if (this.encrypted[key]) {
130
131
  data.encryptedValue = value;
package/src/FileCipher.js CHANGED
@@ -1,4 +1,5 @@
1
1
  const Crypto = require("@secrez/crypto");
2
+ const legacy = require("./fileCipherLegacy");
2
3
 
3
4
  class FileCipher {
4
5
  constructor(secrez) {
@@ -15,33 +16,56 @@ class FileCipher {
15
16
  return pk.split("$")[0].substring(0, 16);
16
17
  }
17
18
 
18
- getUint8ArrayPassword(password) {
19
- return Crypto.bufferToUint8Array(Crypto.SHA3(password));
19
+ deriveExportPasswordKey(password, iterations, randomSaltB64) {
20
+ const passwordKey = Crypto.SHA3(password);
21
+ const salt = Crypto.SHA3(
22
+ passwordKey + iterations.toString() + randomSaltB64
23
+ );
24
+ return Crypto.bufferToUint8Array(
25
+ Crypto.deriveKey(passwordKey, salt, iterations, 32)
26
+ );
20
27
  }
21
28
 
22
29
  encryptFile(content, options) {
23
- let key = Crypto.generateKey();
24
- let result = [
25
- "1", //version
30
+ if (options.password) {
31
+ return this.encryptFileWithPassword(content, options);
32
+ }
33
+ return this.encryptFileWithPublicKeys(content, options);
34
+ }
35
+
36
+ encryptFileWithPassword(content, options) {
37
+ const iterations = parseInt(options.iterations, 10);
38
+ if (!iterations || iterations < 1) {
39
+ throw new Error("A positive number of iterations is required");
40
+ }
41
+ const randomSaltB64 = Crypto.bs64.encode(Crypto.generateKey());
42
+ const key = Crypto.generateKey();
43
+ const derivedPassword = this.deriveExportPasswordKey(
44
+ options.password,
45
+ iterations,
46
+ randomSaltB64
47
+ );
48
+ return [
49
+ "2",
26
50
  Crypto.encrypt(content, key),
51
+ randomSaltB64,
52
+ Crypto.encrypt(key, derivedPassword),
27
53
  ];
28
- if (options.password) {
29
- result[2] = Crypto.encrypt(
30
- key,
31
- this.getUint8ArrayPassword(options.password)
32
- );
33
- } else {
34
- const myShort = this.shortPublicKey(this.secrez.getPublicKey());
35
- result[2] = myShort;
36
- if (options.publicKeys) {
37
- for (let publicKey of options.publicKeys) {
38
- let short = this.shortPublicKey(publicKey);
39
- let encKey =
40
- short === myShort
41
- ? this.secrez.encryptData(key) // encrypted for themself
42
- : this.secrez.encryptSharedData(key, publicKey);
43
- result.push(short + encKey);
44
- }
54
+ }
55
+
56
+ encryptFileWithPublicKeys(content, options) {
57
+ const key = Crypto.generateKey();
58
+ const result = ["1", Crypto.encrypt(content, key)];
59
+ const myShort = this.shortPublicKey(this.secrez.getPublicKey());
60
+ result[2] = myShort;
61
+ if (options.publicKeys) {
62
+ for (let publicKey of options.publicKeys) {
63
+ let short = this.shortPublicKey(publicKey);
64
+ let encKey =
65
+ short === myShort
66
+ ? this.secrez.encryptData(key)
67
+ : this.secrez.encryptSharedData(key, publicKey);
68
+ result.push(short + encKey);
45
69
  }
46
70
  }
47
71
  return result;
@@ -51,48 +75,72 @@ class FileCipher {
51
75
  if (typeof encryptedContent === "string") {
52
76
  encryptedContent = encryptedContent.split(",");
53
77
  }
54
- const [version, content, passwordOrShortPublicKey, ...keys] =
55
- encryptedContent;
78
+ const version = encryptedContent[0];
79
+ if (version === "2") {
80
+ return this.decryptFileV2(encryptedContent, options);
81
+ }
56
82
  if (version === "1") {
57
- if (keys && keys.length) {
58
- let myShort = this.shortPublicKey(this.secrez.getPublicKey());
59
- let contactPublicKey = options.contactPublicKey;
60
- if (!contactPublicKey) {
61
- for (let pk of options.contactsPublicKeys || []) {
62
- if (this.shortPublicKey(pk) === passwordOrShortPublicKey) {
63
- contactPublicKey = pk;
64
- break;
65
- }
66
- }
67
- }
68
- let key;
69
- for (let item of keys) {
70
- let short = item.substring(0, 16);
71
- item = item.substring(16);
72
- if (myShort === short) {
73
- key =
74
- myShort === passwordOrShortPublicKey
75
- ? this.secrez.decryptData(item)
76
- : this.secrez.decryptSharedData(item, contactPublicKey, true);
83
+ return this.decryptFileV1(encryptedContent, options);
84
+ }
85
+ throw new Error("Unsupported version");
86
+ }
87
+
88
+ decryptFileV2(encryptedContent, options) {
89
+ const [, content, randomSaltB64, encryptedKey] = encryptedContent;
90
+ if (!options.password) {
91
+ throw new Error("A password is required");
92
+ }
93
+ const iterations = parseInt(options.iterations, 10);
94
+ if (!iterations || iterations < 1) {
95
+ throw new Error("A positive number of iterations is required");
96
+ }
97
+ const derivedPassword = this.deriveExportPasswordKey(
98
+ options.password,
99
+ iterations,
100
+ randomSaltB64
101
+ );
102
+ const key = Crypto.decrypt(encryptedKey, derivedPassword);
103
+ return Crypto.decrypt(content, key, options.returnUint8Array);
104
+ }
105
+
106
+ decryptFileV1(encryptedContent, options) {
107
+ const [, content, passwordOrShortPublicKey, ...keys] = encryptedContent;
108
+ if (keys && keys.length) {
109
+ let myShort = this.shortPublicKey(this.secrez.getPublicKey());
110
+ let contactPublicKey = options.contactPublicKey;
111
+ if (!contactPublicKey) {
112
+ for (let pk of options.contactsPublicKeys || []) {
113
+ if (this.shortPublicKey(pk) === passwordOrShortPublicKey) {
114
+ contactPublicKey = pk;
115
+ break;
77
116
  }
78
117
  }
79
- if (!key) {
80
- throw new Error("The sender didn't encrypt the data for you");
81
- }
82
- return Crypto.decrypt(content, key);
83
- } else {
84
- if (!options.password) {
85
- throw new Error("A password is required");
118
+ }
119
+ let key;
120
+ for (let item of keys) {
121
+ let short = item.substring(0, 16);
122
+ item = item.substring(16);
123
+ if (myShort === short) {
124
+ key =
125
+ myShort === passwordOrShortPublicKey
126
+ ? this.secrez.decryptData(item)
127
+ : this.secrez.decryptSharedData(item, contactPublicKey, true);
86
128
  }
87
- let key = Crypto.decrypt(
88
- passwordOrShortPublicKey,
89
- this.getUint8ArrayPassword(options.password)
90
- );
91
- return Crypto.decrypt(content, key, options.returnUint8Array);
92
129
  }
93
- } else {
94
- throw new Error("Unsupported version");
130
+ if (!key) {
131
+ throw new Error("The sender didn't encrypt the data for you");
132
+ }
133
+ return Crypto.decrypt(content, key);
134
+ }
135
+ if (!options.password) {
136
+ throw new Error("A password is required");
95
137
  }
138
+ return legacy.decryptWithPassword(
139
+ content,
140
+ passwordOrShortPublicKey,
141
+ options.password,
142
+ options.returnUint8Array
143
+ );
96
144
  }
97
145
  }
98
146
 
package/src/Tree.js CHANGED
@@ -1,7 +1,12 @@
1
1
  const fs = require("fs-extra");
2
2
  const path = require("path");
3
3
  const Node = require("./Node");
4
- const { config, Entry, ConfigUtils } = require("@secrez/core");
4
+ const {
5
+ config,
6
+ Entry,
7
+ ConfigUtils,
8
+ VaultPermissions,
9
+ } = require("@secrez/core");
5
10
  const Crypto = require("@secrez/crypto");
6
11
 
7
12
  class Tree {
@@ -454,7 +459,7 @@ class Tree {
454
459
  ? (entry.encryptedContent || "") +
455
460
  (entry.extraName ? "$" + entry.extraName : "")
456
461
  : "";
457
- await fs.writeFile(fullPath, encryptedContent);
462
+ await VaultPermissions.writeFile(fullPath, encryptedContent);
458
463
  return entry;
459
464
  }
460
465
 
@@ -637,7 +642,7 @@ class Tree {
637
642
  type: config.types.NAME,
638
643
  });
639
644
  let encryptedEntry = this.secrez.encryptEntry(entry);
640
- await fs.writeFile(
645
+ await VaultPermissions.writeFile(
641
646
  path.join(this.dataPath, encryptedEntry.encryptedName),
642
647
  ""
643
648
  );
@@ -0,0 +1,31 @@
1
+ const Crypto = require("@secrez/crypto");
2
+
3
+ function getV1PasswordKey(password) {
4
+ return Crypto.bufferToUint8Array(Crypto.SHA3(password));
5
+ }
6
+
7
+ function encryptWithPassword(content, password) {
8
+ const key = Crypto.generateKey();
9
+ return [
10
+ "1",
11
+ Crypto.encrypt(content, key),
12
+ Crypto.encrypt(key, getV1PasswordKey(password)),
13
+ ];
14
+ }
15
+
16
+ function decryptWithPassword(
17
+ content,
18
+ encryptedKey,
19
+ password,
20
+ returnUint8Array
21
+ ) {
22
+ const key = Crypto.decrypt(encryptedKey, getV1PasswordKey(password));
23
+ return Crypto.decrypt(content, key, returnUint8Array);
24
+ }
25
+
26
+ module.exports = {
27
+ VERSION: "1",
28
+ getV1PasswordKey,
29
+ encryptWithPassword,
30
+ decryptWithPassword,
31
+ };
package/src/index.js CHANGED
@@ -6,6 +6,7 @@ module.exports = {
6
6
  Node: require("./Node"),
7
7
  DataCache: require("./DataCache"),
8
8
  FileCipher: require("./FileCipher"),
9
+ FileCipherLegacy: require("./fileCipherLegacy"),
9
10
  GitConflictChecker: require("./GitConflictChecker"),
10
11
  version: require("../package").version,
11
12
  };