@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 +13 -5
- package/coverage.report +123 -127
- package/package.json +7 -8
- package/src/DataCache.js +4 -3
- package/src/FileCipher.js +106 -58
- package/src/Tree.js +8 -3
- package/src/fileCipherLegacy.js +31 -0
- package/src/index.js +1 -0
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
|
-
|
|
41
|
+
108 passing (5s)
|
|
35
42
|
|
|
36
43
|
-----------------------|---------|----------|---------|---------|-----------------------------------
|
|
37
44
|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
|
|
38
45
|
-----------------------|---------|----------|---------|---------|-----------------------------------
|
|
39
|
-
All files | 81.
|
|
40
|
-
DataCache.js | 89.
|
|
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.
|
|
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 | ...
|
|
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
|
|
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
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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
|
-
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
✔ should normalize "~/fileSystems"
|
|
18
|
+
✔ should normalize "~"
|
|
19
|
+
✔ should normalize "/var"
|
|
20
20
|
fileCompletion
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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
|
-
|
|
28
|
+
✔ should version a file
|
|
29
29
|
isDir
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
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
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
with
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
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
|
-
|
|
79
|
-
|
|
80
|
-
|
|
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
|
-
|
|
83
|
+
✔ should return the fullPath of an entry
|
|
83
84
|
normalizePath
|
|
84
|
-
|
|
85
|
-
|
|
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
|
-
|
|
88
|
+
✔ should normalize a path
|
|
88
89
|
make
|
|
89
|
-
|
|
90
|
-
|
|
90
|
+
✔ should create directories and files
|
|
91
|
+
✔ should throw trying to re-add the same entry
|
|
91
92
|
change
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
93
|
+
✔ should create directories and files and update them
|
|
94
|
+
✔ should return an error
|
|
95
|
+
✔ should move files between datasets
|
|
95
96
|
remove
|
|
96
|
-
|
|
97
|
+
✔ should remove a file from the tree
|
|
97
98
|
load
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
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
|
-
|
|
104
|
+
✔ should get the current folder dir
|
|
104
105
|
|
|
105
106
|
#Node
|
|
106
107
|
#constructor
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
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
|
-
|
|
113
|
-
|
|
113
|
+
✔ should get name, file and options
|
|
114
|
+
✔ should throw if version not found
|
|
114
115
|
#getFindRe
|
|
115
|
-
|
|
116
|
+
✔ should get a re to check a filepath
|
|
116
117
|
#getNames
|
|
117
|
-
|
|
118
|
-
|
|
118
|
+
✔ should get the names of all children
|
|
119
|
+
✔ should throw if trying to get children of a file
|
|
119
120
|
#find
|
|
120
|
-
|
|
121
|
+
✔ should find all childrens satisfying criterias
|
|
121
122
|
#add
|
|
122
|
-
|
|
123
|
-
|
|
123
|
+
✔ should add children to root
|
|
124
|
+
✔ should throw if node is a file
|
|
124
125
|
#move
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
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
|
-
|
|
131
|
-
|
|
132
|
-
|
|
131
|
+
✔ should remove a node
|
|
132
|
+
✔ should remove itself
|
|
133
|
+
✔ should throw trying to remove root
|
|
133
134
|
#toCompressedJSON && Node#fromJSON
|
|
134
|
-
|
|
135
|
-
|
|
135
|
+
✔ should prepare a json for saving
|
|
136
|
+
✔ should build an index from a json file
|
|
136
137
|
#getChildFromPath
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
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
|
-
|
|
143
|
-
|
|
144
|
-
|
|
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
|
-
|
|
147
|
-
|
|
148
|
-
|
|
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
|
-
|
|
153
|
-
|
|
153
|
+
✔ should instantiate the Tree
|
|
154
|
+
✔ should throw if passing not an Secrez instance
|
|
154
155
|
#load
|
|
155
|
-
|
|
156
|
-
|
|
156
|
+
✔ should load an empty Tree
|
|
157
|
+
✔ should do nothing if already loaded
|
|
157
158
|
getEntryDetails
|
|
158
|
-
|
|
159
|
+
✔ should return the entry details of a node
|
|
159
160
|
#Fix
|
|
160
|
-
|
|
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
|
-
|
|
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
|
-
|
|
165
|
+
✔ should load the trees of two data sets
|
|
165
166
|
|
|
166
167
|
|
|
167
|
-
|
|
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
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
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 |
|
|
193
|
-
DataCache.js | 89.
|
|
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.
|
|
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 |
|
|
201
|
-
Tree.js |
|
|
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
|
|
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
|
|
10
|
-
"@secrez/crypto": "~1.0
|
|
11
|
-
"@secrez/utils": "~1.0
|
|
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.
|
|
17
|
+
"@secrez/test-helpers": "~2.0.3",
|
|
18
18
|
"chai": "^4.5.0",
|
|
19
19
|
"cross-env": "^7.0.3",
|
|
20
|
-
"
|
|
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": ">=
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
19
|
-
|
|
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
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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
|
|
55
|
-
|
|
78
|
+
const version = encryptedContent[0];
|
|
79
|
+
if (version === "2") {
|
|
80
|
+
return this.decryptFileV2(encryptedContent, options);
|
|
81
|
+
}
|
|
56
82
|
if (version === "1") {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
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
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
if (
|
|
85
|
-
|
|
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
|
-
|
|
94
|
-
|
|
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 {
|
|
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
|
|
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
|
|
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
|
};
|