zksync 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG +1 -0
- data/Gemfile +6 -0
- data/LICENSE +339 -0
- data/bin/zksync +185 -0
- data/lib/version.rb +3 -0
- data/lib/zksync.rb +19 -0
- data/lib/zksync/archive.rb +65 -0
- data/lib/zksync/crypto_file.rb +139 -0
- data/lib/zksync/crypto_file_index.rb +130 -0
- data/lib/zksync/crypto_inode.rb +84 -0
- data/lib/zksync/inode_table.rb +71 -0
- data/lib/zksync/key.rb +81 -0
- data/lib/zksync/key_store.rb +40 -0
- data/zksync.gemspec +22 -0
- metadata +73 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 251d05e51321009d0223392abaab2b0ddc065b98
|
4
|
+
data.tar.gz: 612a7191b50033e859df0f7cb7511860356b657d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c40962cbfc56a61c4e972b1ee1377609c5449cb42112d201ad5f6af6e862b3f1d3e357ab9343fb55f45cd03e684147609fe72d87415c5d4219e49bdb343b6be9
|
7
|
+
data.tar.gz: c58d68c6ff7b44581ef3a8ef2cbb14b17063878a11d5b57aaff66e9ce37f7ef5d57a150a2a778bd702ad63bf5b20f2a3dc180582a99b6f930fb184dea38cca27
|
data/CHANGELOG
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0 - Initial version. Not intended for public use.
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,339 @@
|
|
1
|
+
GNU GENERAL PUBLIC LICENSE
|
2
|
+
Version 2, June 1991
|
3
|
+
|
4
|
+
Copyright (C) 1989, 1991 Free Software Foundation, Inc., <http://fsf.org/>
|
5
|
+
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
6
|
+
Everyone is permitted to copy and distribute verbatim copies
|
7
|
+
of this license document, but changing it is not allowed.
|
8
|
+
|
9
|
+
Preamble
|
10
|
+
|
11
|
+
The licenses for most software are designed to take away your
|
12
|
+
freedom to share and change it. By contrast, the GNU General Public
|
13
|
+
License is intended to guarantee your freedom to share and change free
|
14
|
+
software--to make sure the software is free for all its users. This
|
15
|
+
General Public License applies to most of the Free Software
|
16
|
+
Foundation's software and to any other program whose authors commit to
|
17
|
+
using it. (Some other Free Software Foundation software is covered by
|
18
|
+
the GNU Lesser General Public License instead.) You can apply it to
|
19
|
+
your programs, too.
|
20
|
+
|
21
|
+
When we speak of free software, we are referring to freedom, not
|
22
|
+
price. Our General Public Licenses are designed to make sure that you
|
23
|
+
have the freedom to distribute copies of free software (and charge for
|
24
|
+
this service if you wish), that you receive source code or can get it
|
25
|
+
if you want it, that you can change the software or use pieces of it
|
26
|
+
in new free programs; and that you know you can do these things.
|
27
|
+
|
28
|
+
To protect your rights, we need to make restrictions that forbid
|
29
|
+
anyone to deny you these rights or to ask you to surrender the rights.
|
30
|
+
These restrictions translate to certain responsibilities for you if you
|
31
|
+
distribute copies of the software, or if you modify it.
|
32
|
+
|
33
|
+
For example, if you distribute copies of such a program, whether
|
34
|
+
gratis or for a fee, you must give the recipients all the rights that
|
35
|
+
you have. You must make sure that they, too, receive or can get the
|
36
|
+
source code. And you must show them these terms so they know their
|
37
|
+
rights.
|
38
|
+
|
39
|
+
We protect your rights with two steps: (1) copyright the software, and
|
40
|
+
(2) offer you this license which gives you legal permission to copy,
|
41
|
+
distribute and/or modify the software.
|
42
|
+
|
43
|
+
Also, for each author's protection and ours, we want to make certain
|
44
|
+
that everyone understands that there is no warranty for this free
|
45
|
+
software. If the software is modified by someone else and passed on, we
|
46
|
+
want its recipients to know that what they have is not the original, so
|
47
|
+
that any problems introduced by others will not reflect on the original
|
48
|
+
authors' reputations.
|
49
|
+
|
50
|
+
Finally, any free program is threatened constantly by software
|
51
|
+
patents. We wish to avoid the danger that redistributors of a free
|
52
|
+
program will individually obtain patent licenses, in effect making the
|
53
|
+
program proprietary. To prevent this, we have made it clear that any
|
54
|
+
patent must be licensed for everyone's free use or not licensed at all.
|
55
|
+
|
56
|
+
The precise terms and conditions for copying, distribution and
|
57
|
+
modification follow.
|
58
|
+
|
59
|
+
GNU GENERAL PUBLIC LICENSE
|
60
|
+
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
61
|
+
|
62
|
+
0. This License applies to any program or other work which contains
|
63
|
+
a notice placed by the copyright holder saying it may be distributed
|
64
|
+
under the terms of this General Public License. The "Program", below,
|
65
|
+
refers to any such program or work, and a "work based on the Program"
|
66
|
+
means either the Program or any derivative work under copyright law:
|
67
|
+
that is to say, a work containing the Program or a portion of it,
|
68
|
+
either verbatim or with modifications and/or translated into another
|
69
|
+
language. (Hereinafter, translation is included without limitation in
|
70
|
+
the term "modification".) Each licensee is addressed as "you".
|
71
|
+
|
72
|
+
Activities other than copying, distribution and modification are not
|
73
|
+
covered by this License; they are outside its scope. The act of
|
74
|
+
running the Program is not restricted, and the output from the Program
|
75
|
+
is covered only if its contents constitute a work based on the
|
76
|
+
Program (independent of having been made by running the Program).
|
77
|
+
Whether that is true depends on what the Program does.
|
78
|
+
|
79
|
+
1. You may copy and distribute verbatim copies of the Program's
|
80
|
+
source code as you receive it, in any medium, provided that you
|
81
|
+
conspicuously and appropriately publish on each copy an appropriate
|
82
|
+
copyright notice and disclaimer of warranty; keep intact all the
|
83
|
+
notices that refer to this License and to the absence of any warranty;
|
84
|
+
and give any other recipients of the Program a copy of this License
|
85
|
+
along with the Program.
|
86
|
+
|
87
|
+
You may charge a fee for the physical act of transferring a copy, and
|
88
|
+
you may at your option offer warranty protection in exchange for a fee.
|
89
|
+
|
90
|
+
2. You may modify your copy or copies of the Program or any portion
|
91
|
+
of it, thus forming a work based on the Program, and copy and
|
92
|
+
distribute such modifications or work under the terms of Section 1
|
93
|
+
above, provided that you also meet all of these conditions:
|
94
|
+
|
95
|
+
a) You must cause the modified files to carry prominent notices
|
96
|
+
stating that you changed the files and the date of any change.
|
97
|
+
|
98
|
+
b) You must cause any work that you distribute or publish, that in
|
99
|
+
whole or in part contains or is derived from the Program or any
|
100
|
+
part thereof, to be licensed as a whole at no charge to all third
|
101
|
+
parties under the terms of this License.
|
102
|
+
|
103
|
+
c) If the modified program normally reads commands interactively
|
104
|
+
when run, you must cause it, when started running for such
|
105
|
+
interactive use in the most ordinary way, to print or display an
|
106
|
+
announcement including an appropriate copyright notice and a
|
107
|
+
notice that there is no warranty (or else, saying that you provide
|
108
|
+
a warranty) and that users may redistribute the program under
|
109
|
+
these conditions, and telling the user how to view a copy of this
|
110
|
+
License. (Exception: if the Program itself is interactive but
|
111
|
+
does not normally print such an announcement, your work based on
|
112
|
+
the Program is not required to print an announcement.)
|
113
|
+
|
114
|
+
These requirements apply to the modified work as a whole. If
|
115
|
+
identifiable sections of that work are not derived from the Program,
|
116
|
+
and can be reasonably considered independent and separate works in
|
117
|
+
themselves, then this License, and its terms, do not apply to those
|
118
|
+
sections when you distribute them as separate works. But when you
|
119
|
+
distribute the same sections as part of a whole which is a work based
|
120
|
+
on the Program, the distribution of the whole must be on the terms of
|
121
|
+
this License, whose permissions for other licensees extend to the
|
122
|
+
entire whole, and thus to each and every part regardless of who wrote it.
|
123
|
+
|
124
|
+
Thus, it is not the intent of this section to claim rights or contest
|
125
|
+
your rights to work written entirely by you; rather, the intent is to
|
126
|
+
exercise the right to control the distribution of derivative or
|
127
|
+
collective works based on the Program.
|
128
|
+
|
129
|
+
In addition, mere aggregation of another work not based on the Program
|
130
|
+
with the Program (or with a work based on the Program) on a volume of
|
131
|
+
a storage or distribution medium does not bring the other work under
|
132
|
+
the scope of this License.
|
133
|
+
|
134
|
+
3. You may copy and distribute the Program (or a work based on it,
|
135
|
+
under Section 2) in object code or executable form under the terms of
|
136
|
+
Sections 1 and 2 above provided that you also do one of the following:
|
137
|
+
|
138
|
+
a) Accompany it with the complete corresponding machine-readable
|
139
|
+
source code, which must be distributed under the terms of Sections
|
140
|
+
1 and 2 above on a medium customarily used for software interchange; or,
|
141
|
+
|
142
|
+
b) Accompany it with a written offer, valid for at least three
|
143
|
+
years, to give any third party, for a charge no more than your
|
144
|
+
cost of physically performing source distribution, a complete
|
145
|
+
machine-readable copy of the corresponding source code, to be
|
146
|
+
distributed under the terms of Sections 1 and 2 above on a medium
|
147
|
+
customarily used for software interchange; or,
|
148
|
+
|
149
|
+
c) Accompany it with the information you received as to the offer
|
150
|
+
to distribute corresponding source code. (This alternative is
|
151
|
+
allowed only for noncommercial distribution and only if you
|
152
|
+
received the program in object code or executable form with such
|
153
|
+
an offer, in accord with Subsection b above.)
|
154
|
+
|
155
|
+
The source code for a work means the preferred form of the work for
|
156
|
+
making modifications to it. For an executable work, complete source
|
157
|
+
code means all the source code for all modules it contains, plus any
|
158
|
+
associated interface definition files, plus the scripts used to
|
159
|
+
control compilation and installation of the executable. However, as a
|
160
|
+
special exception, the source code distributed need not include
|
161
|
+
anything that is normally distributed (in either source or binary
|
162
|
+
form) with the major components (compiler, kernel, and so on) of the
|
163
|
+
operating system on which the executable runs, unless that component
|
164
|
+
itself accompanies the executable.
|
165
|
+
|
166
|
+
If distribution of executable or object code is made by offering
|
167
|
+
access to copy from a designated place, then offering equivalent
|
168
|
+
access to copy the source code from the same place counts as
|
169
|
+
distribution of the source code, even though third parties are not
|
170
|
+
compelled to copy the source along with the object code.
|
171
|
+
|
172
|
+
4. You may not copy, modify, sublicense, or distribute the Program
|
173
|
+
except as expressly provided under this License. Any attempt
|
174
|
+
otherwise to copy, modify, sublicense or distribute the Program is
|
175
|
+
void, and will automatically terminate your rights under this License.
|
176
|
+
However, parties who have received copies, or rights, from you under
|
177
|
+
this License will not have their licenses terminated so long as such
|
178
|
+
parties remain in full compliance.
|
179
|
+
|
180
|
+
5. You are not required to accept this License, since you have not
|
181
|
+
signed it. However, nothing else grants you permission to modify or
|
182
|
+
distribute the Program or its derivative works. These actions are
|
183
|
+
prohibited by law if you do not accept this License. Therefore, by
|
184
|
+
modifying or distributing the Program (or any work based on the
|
185
|
+
Program), you indicate your acceptance of this License to do so, and
|
186
|
+
all its terms and conditions for copying, distributing or modifying
|
187
|
+
the Program or works based on it.
|
188
|
+
|
189
|
+
6. Each time you redistribute the Program (or any work based on the
|
190
|
+
Program), the recipient automatically receives a license from the
|
191
|
+
original licensor to copy, distribute or modify the Program subject to
|
192
|
+
these terms and conditions. You may not impose any further
|
193
|
+
restrictions on the recipients' exercise of the rights granted herein.
|
194
|
+
You are not responsible for enforcing compliance by third parties to
|
195
|
+
this License.
|
196
|
+
|
197
|
+
7. If, as a consequence of a court judgment or allegation of patent
|
198
|
+
infringement or for any other reason (not limited to patent issues),
|
199
|
+
conditions are imposed on you (whether by court order, agreement or
|
200
|
+
otherwise) that contradict the conditions of this License, they do not
|
201
|
+
excuse you from the conditions of this License. If you cannot
|
202
|
+
distribute so as to satisfy simultaneously your obligations under this
|
203
|
+
License and any other pertinent obligations, then as a consequence you
|
204
|
+
may not distribute the Program at all. For example, if a patent
|
205
|
+
license would not permit royalty-free redistribution of the Program by
|
206
|
+
all those who receive copies directly or indirectly through you, then
|
207
|
+
the only way you could satisfy both it and this License would be to
|
208
|
+
refrain entirely from distribution of the Program.
|
209
|
+
|
210
|
+
If any portion of this section is held invalid or unenforceable under
|
211
|
+
any particular circumstance, the balance of the section is intended to
|
212
|
+
apply and the section as a whole is intended to apply in other
|
213
|
+
circumstances.
|
214
|
+
|
215
|
+
It is not the purpose of this section to induce you to infringe any
|
216
|
+
patents or other property right claims or to contest validity of any
|
217
|
+
such claims; this section has the sole purpose of protecting the
|
218
|
+
integrity of the free software distribution system, which is
|
219
|
+
implemented by public license practices. Many people have made
|
220
|
+
generous contributions to the wide range of software distributed
|
221
|
+
through that system in reliance on consistent application of that
|
222
|
+
system; it is up to the author/donor to decide if he or she is willing
|
223
|
+
to distribute software through any other system and a licensee cannot
|
224
|
+
impose that choice.
|
225
|
+
|
226
|
+
This section is intended to make thoroughly clear what is believed to
|
227
|
+
be a consequence of the rest of this License.
|
228
|
+
|
229
|
+
8. If the distribution and/or use of the Program is restricted in
|
230
|
+
certain countries either by patents or by copyrighted interfaces, the
|
231
|
+
original copyright holder who places the Program under this License
|
232
|
+
may add an explicit geographical distribution limitation excluding
|
233
|
+
those countries, so that distribution is permitted only in or among
|
234
|
+
countries not thus excluded. In such case, this License incorporates
|
235
|
+
the limitation as if written in the body of this License.
|
236
|
+
|
237
|
+
9. The Free Software Foundation may publish revised and/or new versions
|
238
|
+
of the General Public License from time to time. Such new versions will
|
239
|
+
be similar in spirit to the present version, but may differ in detail to
|
240
|
+
address new problems or concerns.
|
241
|
+
|
242
|
+
Each version is given a distinguishing version number. If the Program
|
243
|
+
specifies a version number of this License which applies to it and "any
|
244
|
+
later version", you have the option of following the terms and conditions
|
245
|
+
either of that version or of any later version published by the Free
|
246
|
+
Software Foundation. If the Program does not specify a version number of
|
247
|
+
this License, you may choose any version ever published by the Free Software
|
248
|
+
Foundation.
|
249
|
+
|
250
|
+
10. If you wish to incorporate parts of the Program into other free
|
251
|
+
programs whose distribution conditions are different, write to the author
|
252
|
+
to ask for permission. For software which is copyrighted by the Free
|
253
|
+
Software Foundation, write to the Free Software Foundation; we sometimes
|
254
|
+
make exceptions for this. Our decision will be guided by the two goals
|
255
|
+
of preserving the free status of all derivatives of our free software and
|
256
|
+
of promoting the sharing and reuse of software generally.
|
257
|
+
|
258
|
+
NO WARRANTY
|
259
|
+
|
260
|
+
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
|
261
|
+
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
|
262
|
+
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
|
263
|
+
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
|
264
|
+
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
265
|
+
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
|
266
|
+
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
|
267
|
+
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
|
268
|
+
REPAIR OR CORRECTION.
|
269
|
+
|
270
|
+
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
271
|
+
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
|
272
|
+
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
|
273
|
+
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
|
274
|
+
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
|
275
|
+
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
|
276
|
+
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
|
277
|
+
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
|
278
|
+
POSSIBILITY OF SUCH DAMAGES.
|
279
|
+
|
280
|
+
END OF TERMS AND CONDITIONS
|
281
|
+
|
282
|
+
How to Apply These Terms to Your New Programs
|
283
|
+
|
284
|
+
If you develop a new program, and you want it to be of the greatest
|
285
|
+
possible use to the public, the best way to achieve this is to make it
|
286
|
+
free software which everyone can redistribute and change under these terms.
|
287
|
+
|
288
|
+
To do so, attach the following notices to the program. It is safest
|
289
|
+
to attach them to the start of each source file to most effectively
|
290
|
+
convey the exclusion of warranty; and each file should have at least
|
291
|
+
the "copyright" line and a pointer to where the full notice is found.
|
292
|
+
|
293
|
+
{description}
|
294
|
+
Copyright (C) {year} {fullname}
|
295
|
+
|
296
|
+
This program is free software; you can redistribute it and/or modify
|
297
|
+
it under the terms of the GNU General Public License as published by
|
298
|
+
the Free Software Foundation; either version 2 of the License, or
|
299
|
+
(at your option) any later version.
|
300
|
+
|
301
|
+
This program is distributed in the hope that it will be useful,
|
302
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
303
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
304
|
+
GNU General Public License for more details.
|
305
|
+
|
306
|
+
You should have received a copy of the GNU General Public License along
|
307
|
+
with this program; if not, write to the Free Software Foundation, Inc.,
|
308
|
+
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
309
|
+
|
310
|
+
Also add information on how to contact you by electronic and paper mail.
|
311
|
+
|
312
|
+
If the program is interactive, make it output a short notice like this
|
313
|
+
when it starts in an interactive mode:
|
314
|
+
|
315
|
+
Gnomovision version 69, Copyright (C) year name of author
|
316
|
+
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
317
|
+
This is free software, and you are welcome to redistribute it
|
318
|
+
under certain conditions; type `show c' for details.
|
319
|
+
|
320
|
+
The hypothetical commands `show w' and `show c' should show the appropriate
|
321
|
+
parts of the General Public License. Of course, the commands you use may
|
322
|
+
be called something other than `show w' and `show c'; they could even be
|
323
|
+
mouse-clicks or menu items--whatever suits your program.
|
324
|
+
|
325
|
+
You should also get your employer (if you work as a programmer) or your
|
326
|
+
school, if any, to sign a "copyright disclaimer" for the program, if
|
327
|
+
necessary. Here is a sample; alter the names:
|
328
|
+
|
329
|
+
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
|
330
|
+
`Gnomovision' (which makes passes at compilers) written by James Hacker.
|
331
|
+
|
332
|
+
{signature of Ty Coon}, 1 April 1989
|
333
|
+
Ty Coon, President of Vice
|
334
|
+
|
335
|
+
This General Public License does not permit incorporating your program into
|
336
|
+
proprietary programs. If your program is a subroutine library, you may
|
337
|
+
consider it more useful to permit linking proprietary applications with the
|
338
|
+
library. If this is what you want to do, use the GNU Lesser General
|
339
|
+
Public License instead of this License.
|
data/bin/zksync
ADDED
@@ -0,0 +1,185 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
|
3
|
+
# zksync
|
4
|
+
# Copyright (C) 2017 Jonas Acres
|
5
|
+
|
6
|
+
# This program is free software; you can redistribute it and/or modify
|
7
|
+
# it under the terms of the GNU General Public License as published by
|
8
|
+
# the Free Software Foundation; either version 2 of the License, or
|
9
|
+
# (at your option) any later version.
|
10
|
+
|
11
|
+
# This program is distributed in the hope that it will be useful,
|
12
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14
|
+
# GNU General Public License for more details.
|
15
|
+
|
16
|
+
# You should have received a copy of the GNU General Public License along
|
17
|
+
# with this program; if not, write to the Free Software Foundation, Inc.,
|
18
|
+
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
19
|
+
|
20
|
+
basepath = File.expand_path(File.dirname(__FILE__) + '/..')
|
21
|
+
lib = File.join(basepath, 'lib')
|
22
|
+
$LOAD_PATH.unshift(lib) if File.directory?(lib) && !$LOAD_PATH.include?(lib)
|
23
|
+
|
24
|
+
require 'fileutils'
|
25
|
+
require 'json'
|
26
|
+
require 'openssl'
|
27
|
+
require 'pathname'
|
28
|
+
require 'trollop'
|
29
|
+
require 'time'
|
30
|
+
require 'etc'
|
31
|
+
|
32
|
+
require 'zksync'
|
33
|
+
|
34
|
+
def die(message, status=1)
|
35
|
+
STDERR.puts message
|
36
|
+
exit status
|
37
|
+
end
|
38
|
+
|
39
|
+
def get_passphrase(pp_arg)
|
40
|
+
return STDIN.read if pp_arg == "-"
|
41
|
+
return pp_arg unless pp_arg.start_with?("@")
|
42
|
+
|
43
|
+
filename = pp_arg[1..-1]
|
44
|
+
die "Cannot open passphrase file: #{filename}" unless File.file?(filename) && File.readable?(filename)
|
45
|
+
IO.read(filename)
|
46
|
+
end
|
47
|
+
|
48
|
+
def flag_for_mode_bits(bits)
|
49
|
+
s = ""
|
50
|
+
s += (bits & 4) > 0 ? "r" : "-"
|
51
|
+
s += (bits & 2) > 0 ? "w" : "-"
|
52
|
+
s += (bits & 1) > 0 ? "x" : "-"
|
53
|
+
s
|
54
|
+
end
|
55
|
+
|
56
|
+
def mode_string(inode)
|
57
|
+
fmode = inode.ftype == "directory" ? "d" : "-"
|
58
|
+
umode = (inode.mode & 0700) >> 6
|
59
|
+
gmode = (inode.mode & 0070) >> 3
|
60
|
+
omode = (inode.mode & 0007)
|
61
|
+
|
62
|
+
fmode + flag_for_mode_bits(umode) + flag_for_mode_bits(gmode) + flag_for_mode_bits(omode)
|
63
|
+
end
|
64
|
+
|
65
|
+
def username(inode)
|
66
|
+
sprintf("%-8s", Etc.getpwuid(inode.uid).name)
|
67
|
+
end
|
68
|
+
|
69
|
+
def groupname(inode)
|
70
|
+
sprintf("%-8s", Etc.getgrgid(inode.gid).name)
|
71
|
+
end
|
72
|
+
|
73
|
+
def size_string(inode)
|
74
|
+
sprintf("%9d", inode.size)
|
75
|
+
end
|
76
|
+
|
77
|
+
def date_string(inode)
|
78
|
+
inode.mtime.strftime("%b %d %H:%M")
|
79
|
+
end
|
80
|
+
|
81
|
+
def name_string(inode)
|
82
|
+
inode.path
|
83
|
+
end
|
84
|
+
|
85
|
+
def hash_string(inode)
|
86
|
+
inode.sha256 ? inode.sha256[0..6] : " "
|
87
|
+
end
|
88
|
+
|
89
|
+
def list_line(inode)
|
90
|
+
"#{mode_string(inode)} #{username(inode)} #{groupname(inode)} #{size_string(inode)} #{date_string(inode)} #{hash_string(inode)} #{name_string(inode)}"
|
91
|
+
end
|
92
|
+
|
93
|
+
def extract_file(archive, archive_path)
|
94
|
+
puts archive_path if $verbose
|
95
|
+
inode = archive.inode_for_path(archive_path)
|
96
|
+
|
97
|
+
if inode.ftype == "directory" then
|
98
|
+
FileUtils.mkdir_p(archive_path) unless File.directory?(archive_path)
|
99
|
+
else
|
100
|
+
FileUtils.mkdir_p(File.dirname(archive_path)) unless File.directory?(File.dirname(archive_path))
|
101
|
+
File.chmod(0700, inode.path) if File.exists?(inode.path)
|
102
|
+
File.open(archive_path, 'w') do |file|
|
103
|
+
archive.file_at_path(archive_path).read do |page|
|
104
|
+
file.write(page)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
File.utime(inode.atime, inode.mtime, inode.path)
|
110
|
+
File.chmod(inode.mode, inode.path)
|
111
|
+
FileUtils.chown(Process.uid.zero? ? inode.uid : nil, inode.gid, inode.path) if $setids
|
112
|
+
end
|
113
|
+
|
114
|
+
opts = Trollop::options do
|
115
|
+
opt :extract, "Extract", :short => "x"
|
116
|
+
opt :create, "Create/update", :short => "c"
|
117
|
+
opt :list, "List archive contents to stdout", :short => "t"
|
118
|
+
opt :integrity, "Check integrity of archive", :short => "i"
|
119
|
+
|
120
|
+
opt :file, "Path to zksync archive directory", :type => :string
|
121
|
+
opt :passphrase, "Encryption passphrase. Prefix with @ to read directory, - to read stdin.", :type => :string, :required => true
|
122
|
+
opt :verbose, "Verbose", :short => "v"
|
123
|
+
opt :version, "Show version on stdout and quit", :short => "V"
|
124
|
+
opt :setids, "Set file uid and gid ownership", :short => "I"
|
125
|
+
banner "Usage:\n\##{$0} [options] plaintext_directory\nwhere [options] are:\n"
|
126
|
+
end
|
127
|
+
|
128
|
+
modes = 0
|
129
|
+
modes += 1 if opts[:extract]
|
130
|
+
modes += 1 if opts[:create]
|
131
|
+
modes += 1 if opts[:list]
|
132
|
+
modes += 1 if opts[:integrity]
|
133
|
+
|
134
|
+
$verbose = opts[:verbose]
|
135
|
+
$setids = opts[:setids]
|
136
|
+
|
137
|
+
Trollop::educate if ARGV.empty? && opts[:create]
|
138
|
+
Trollop::educate unless modes == 1
|
139
|
+
Trollop::educate unless opts[:passphrase]
|
140
|
+
|
141
|
+
if opts[:version] then
|
142
|
+
puts "zksync #{VERSION}"
|
143
|
+
exit 0
|
144
|
+
end
|
145
|
+
|
146
|
+
archive = ZKSync::Archive.new(get_passphrase(opts[:passphrase]), Dir.pwd, opts[:file])
|
147
|
+
|
148
|
+
if opts[:create] then
|
149
|
+
ARGV.each do |path|
|
150
|
+
unless File.exists?(path) then
|
151
|
+
STDERR.puts "#{$0}: no such file or directory: #{path}"
|
152
|
+
next
|
153
|
+
end
|
154
|
+
|
155
|
+
files = File.directory?(path) ? Dir.glob(File.join(path, "**/*"), File::FNM_DOTMATCH) : [path]
|
156
|
+
files.reject! { |fs_path| [".", ".."].include?(File.basename(fs_path)) }
|
157
|
+
files.select! { |fs_path| File.file?(fs_path) || File.directory?(fs_path) }
|
158
|
+
files.each do |fs_path|
|
159
|
+
puts fs_path if $verbose
|
160
|
+
archive.add(fs_path)
|
161
|
+
end
|
162
|
+
|
163
|
+
archive.commit
|
164
|
+
end
|
165
|
+
elsif opts[:extract] then
|
166
|
+
archive_paths = archive.list
|
167
|
+
archive_paths.select! { |archive_path| ARGV.include?(archive_path) } unless ARGV.empty?
|
168
|
+
archive_paths.each { |archive_path| extract_file(archive, archive_path) }
|
169
|
+
elsif opts[:list] then
|
170
|
+
archive.list.each do |archive_path|
|
171
|
+
puts list_line(archive.inode_for_path(archive_path))
|
172
|
+
end
|
173
|
+
elsif opts[:integrity] then
|
174
|
+
failures = 0
|
175
|
+
archive.list.each do |archive_path|
|
176
|
+
print "#{archive_path}... " if $verbose
|
177
|
+
was_valid = archive.file_at_path(archive_path).valid?
|
178
|
+
failures += 1 unless was_valid
|
179
|
+
puts was_valid ? "OK" : "FAIL" if $verbose
|
180
|
+
puts "#{archive_path}... FAIL" if !$verbose && !was_valid
|
181
|
+
end
|
182
|
+
|
183
|
+
puts "#{failures} failures in #{archive.list.count} entries."
|
184
|
+
exit failures > 0 ? 1 : 0
|
185
|
+
end
|
data/lib/version.rb
ADDED
data/lib/zksync.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# zksync
|
2
|
+
# Copyright (C) 2017 Jonas Acres
|
3
|
+
|
4
|
+
# This program is free software; you can redistribute it and/or modify
|
5
|
+
# it under the terms of the GNU General Public License as published by
|
6
|
+
# the Free Software Foundation; either version 2 of the License, or
|
7
|
+
# (at your option) any later version.
|
8
|
+
|
9
|
+
# This program is distributed in the hope that it will be useful,
|
10
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
# GNU General Public License for more details.
|
13
|
+
|
14
|
+
# You should have received a copy of the GNU General Public License along
|
15
|
+
# with this program; if not, write to the Free Software Foundation, Inc.,
|
16
|
+
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
17
|
+
|
18
|
+
require 'version'
|
19
|
+
Dir.glob(File.join(File.dirname(__FILE__), "zksync/**/*.rb")) { |file| require file }
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# zksync
|
2
|
+
# Copyright (C) 2017 Jonas Acres
|
3
|
+
|
4
|
+
# This program is free software; you can redistribute it and/or modify
|
5
|
+
# it under the terms of the GNU General Public License as published by
|
6
|
+
# the Free Software Foundation; either version 2 of the License, or
|
7
|
+
# (at your option) any later version.
|
8
|
+
|
9
|
+
# This program is distributed in the hope that it will be useful,
|
10
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
# GNU General Public License for more details.
|
13
|
+
|
14
|
+
# You should have received a copy of the GNU General Public License along
|
15
|
+
# with this program; if not, write to the Free Software Foundation, Inc.,
|
16
|
+
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
17
|
+
|
18
|
+
module ZKSync
|
19
|
+
class Archive
|
20
|
+
attr_reader :fs_base_path, :keystore, :file_index, :root
|
21
|
+
|
22
|
+
def initialize(passphrase, fs_base_path, root)
|
23
|
+
@root = root
|
24
|
+
@fs_base_path = fs_base_path
|
25
|
+
@keystore = KeyStore.new(Key.new(passphrase, type: :passphrase))
|
26
|
+
@file_index = CryptoFileIndex.new("@@index", self)
|
27
|
+
end
|
28
|
+
|
29
|
+
# add fs-paths to the archive. returns array of paths that were not added successfully.
|
30
|
+
# archive paths are stored relative to basepath
|
31
|
+
def add(fs_paths)
|
32
|
+
[*fs_paths]
|
33
|
+
.map { |fs_path| File.expand_path(fs_path) }
|
34
|
+
.reject { |fs_path| add_file(fs_path) }
|
35
|
+
end
|
36
|
+
|
37
|
+
# list of all archive paths in this archive
|
38
|
+
def list
|
39
|
+
@file_index.list
|
40
|
+
end
|
41
|
+
|
42
|
+
def rm(*archive_paths)
|
43
|
+
archive_paths.each { |archive_path| @file_index.rm(archive_path) }
|
44
|
+
end
|
45
|
+
|
46
|
+
def commit
|
47
|
+
@file_index.write
|
48
|
+
end
|
49
|
+
|
50
|
+
def file_at_path(archive_path)
|
51
|
+
@file_index.file_at_path(archive_path)
|
52
|
+
end
|
53
|
+
|
54
|
+
def inode_for_path(archive_path)
|
55
|
+
@file_index.inode_for_path(archive_path)
|
56
|
+
end
|
57
|
+
|
58
|
+
###
|
59
|
+
|
60
|
+
def add_file(fs_path)
|
61
|
+
archive_path = Pathname.new(fs_path).relative_path_from(Pathname.new(fs_base_path)).to_s
|
62
|
+
@file_index.add(archive_path, fs_path)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,139 @@
|
|
1
|
+
# zksync
|
2
|
+
# Copyright (C) 2017 Jonas Acres
|
3
|
+
|
4
|
+
# This program is free software; you can redistribute it and/or modify
|
5
|
+
# it under the terms of the GNU General Public License as published by
|
6
|
+
# the Free Software Foundation; either version 2 of the License, or
|
7
|
+
# (at your option) any later version.
|
8
|
+
|
9
|
+
# This program is distributed in the hope that it will be useful,
|
10
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
# GNU General Public License for more details.
|
13
|
+
|
14
|
+
# You should have received a copy of the GNU General Public License along
|
15
|
+
# with this program; if not, write to the Free Software Foundation, Inc.,
|
16
|
+
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
17
|
+
|
18
|
+
module ZKSync
|
19
|
+
class CryptoFile
|
20
|
+
attr_reader :archive_path, :archive
|
21
|
+
|
22
|
+
def initialize(archive_path, archive)
|
23
|
+
@archive = archive
|
24
|
+
@archive_path = archive_path
|
25
|
+
end
|
26
|
+
|
27
|
+
def page_physical_size
|
28
|
+
65536
|
29
|
+
end
|
30
|
+
|
31
|
+
def page_size
|
32
|
+
page_physical_size - Key.write_size - 1
|
33
|
+
end
|
34
|
+
|
35
|
+
def size
|
36
|
+
inode.size
|
37
|
+
end
|
38
|
+
|
39
|
+
def page_count
|
40
|
+
(size.to_f/page_size).ceil
|
41
|
+
end
|
42
|
+
|
43
|
+
def inode
|
44
|
+
@inode ||= archive.file_index.inode_for_path(archive_path)
|
45
|
+
end
|
46
|
+
|
47
|
+
def file_key
|
48
|
+
@file_key ||= archive.keystore.file_key(archive_path)
|
49
|
+
end
|
50
|
+
|
51
|
+
def page_path(page_num)
|
52
|
+
index_hash = Digest::SHA256.hexdigest(file_key.to_s + ":" + archive_path + ":#{page_num}-")
|
53
|
+
File.join(archive.root, "#{index_hash[0..1]}/#{index_hash[2..3]}/#{index_hash[4..-1]}")
|
54
|
+
end
|
55
|
+
|
56
|
+
def valid?
|
57
|
+
return true if inode.ftype == "directory"
|
58
|
+
raise "No inode" unless inode
|
59
|
+
|
60
|
+
digest = Digest::SHA256.new
|
61
|
+
read { |page| digest << page }
|
62
|
+
digest.hexdigest == inode.sha256
|
63
|
+
end
|
64
|
+
|
65
|
+
def read
|
66
|
+
page_count.times do |n|
|
67
|
+
yield read_page(n)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def __read_page_keep_padding(page_num)
|
72
|
+
page_encrypted = IO.read(page_path(page_num), page_physical_size)
|
73
|
+
page_key = Key.new(file_key.decrypt(page_encrypted[0...Key.write_size]), type: :literal)
|
74
|
+
page_key.decrypt(page_encrypted[Key.write_size..-1])
|
75
|
+
end
|
76
|
+
|
77
|
+
def read_page(page_num)
|
78
|
+
plaintext = __read_page_keep_padding(page_num)
|
79
|
+
plaintext = plaintext[0...(size % page_size)] if page_num == page_count-1
|
80
|
+
plaintext
|
81
|
+
end
|
82
|
+
|
83
|
+
def make_parent_directories(path)
|
84
|
+
FileUtils.mkdir_p(File.dirname(path))
|
85
|
+
end
|
86
|
+
|
87
|
+
def page_matches?(pagefile_path, key)
|
88
|
+
return false unless File.exists?(pagefile_path)
|
89
|
+
IO.read(pagefile_path, Key.size) == key.to_b
|
90
|
+
end
|
91
|
+
|
92
|
+
def write(in_stream, mtime)
|
93
|
+
page_num = 0
|
94
|
+
bytes = 0
|
95
|
+
|
96
|
+
while page_plaintext = in_stream.read(page_size) do
|
97
|
+
write_page(page_num, page_plaintext)
|
98
|
+
bytes += page_plaintext.length
|
99
|
+
page_num += 1
|
100
|
+
end
|
101
|
+
|
102
|
+
trim_to_size(bytes)
|
103
|
+
inode.size = bytes
|
104
|
+
inode.mtime = mtime
|
105
|
+
end
|
106
|
+
|
107
|
+
def write_page(page_num, page_plaintext)
|
108
|
+
page_hash = Digest::SHA256.hexdigest(page_plaintext)
|
109
|
+
pagefile_path = page_path(page_num)
|
110
|
+
page_key = archive.keystore.page_key(archive_path, page_num, page_hash)
|
111
|
+
return if page_matches?(pagefile_path, page_key)
|
112
|
+
|
113
|
+
encrypted_key = file_key.encrypt(page_key.to_b)
|
114
|
+
padding = "\0" * (page_size - page_plaintext.length)
|
115
|
+
encrypted_data = page_key.encrypt(page_plaintext + padding)
|
116
|
+
|
117
|
+
make_parent_directories(pagefile_path)
|
118
|
+
File.write(pagefile_path, encrypted_key + encrypted_data)
|
119
|
+
|
120
|
+
ts_digest = "TIMESTAMP:" + page_hash
|
121
|
+
16.times { ts_digest = Digest::SHA256.digest(ts_digest) }
|
122
|
+
ts = ts_digest[0..3].unpack("L").first & 0x7FFFFFFF
|
123
|
+
File.utime(Time.at(ts), Time.at(ts), pagefile_path)
|
124
|
+
end
|
125
|
+
|
126
|
+
# call me before updating inode size
|
127
|
+
# ensure excess pages are pruned if a file shrinks
|
128
|
+
def trim_to_size(size)
|
129
|
+
old_page_count = (inode.size/page_size).ceil
|
130
|
+
new_page_count = (size/page_size).ceil
|
131
|
+
return if new_page_count >= old_page_count
|
132
|
+
|
133
|
+
(new_page_count .. old_page_count).to_a.each do |page_num|
|
134
|
+
pagefile = page_path(page_num)
|
135
|
+
File.unlink(pagefile) if File.exists? pagefile
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
@@ -0,0 +1,130 @@
|
|
1
|
+
# zksync
|
2
|
+
# Copyright (C) 2017 Jonas Acres
|
3
|
+
|
4
|
+
# This program is free software; you can redistribute it and/or modify
|
5
|
+
# it under the terms of the GNU General Public License as published by
|
6
|
+
# the Free Software Foundation; either version 2 of the License, or
|
7
|
+
# (at your option) any later version.
|
8
|
+
|
9
|
+
# This program is distributed in the hope that it will be useful,
|
10
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
# GNU General Public License for more details.
|
13
|
+
|
14
|
+
# You should have received a copy of the GNU General Public License along
|
15
|
+
# with this program; if not, write to the Free Software Foundation, Inc.,
|
16
|
+
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
17
|
+
|
18
|
+
module ZKSync
|
19
|
+
class CryptoFileIndex < CryptoFile
|
20
|
+
attr_reader :inode_table, :archive
|
21
|
+
|
22
|
+
def self.inode_width
|
23
|
+
256
|
24
|
+
end
|
25
|
+
|
26
|
+
def initialize(archive_path, archive)
|
27
|
+
super
|
28
|
+
|
29
|
+
@files = {}
|
30
|
+
@inode_table = InodeTable.new(self.inode_width)
|
31
|
+
parse
|
32
|
+
end
|
33
|
+
|
34
|
+
def inode_width
|
35
|
+
@inode_width ||= self.class.inode_width
|
36
|
+
end
|
37
|
+
|
38
|
+
def parse
|
39
|
+
content = ""
|
40
|
+
read { |page| content += page }
|
41
|
+
@inode_width, length = content[0...64].strip.split("|")[0..1].map { |x| x.to_i }
|
42
|
+
offset = inode_width
|
43
|
+
|
44
|
+
until offset >= content.length do
|
45
|
+
next_inode_bytes = content[offset .. offset + inode_width].split("|").first.to_i
|
46
|
+
header_len = next_inode_bytes.to_s.length + 1
|
47
|
+
inode_data = content[offset + header_len ... offset + header_len + next_inode_bytes].strip
|
48
|
+
index = ((offset-inode_width).to_f/inode_width).ceil
|
49
|
+
blocks = ((next_inode_bytes + header_len).to_f/inode_width).ceil
|
50
|
+
|
51
|
+
inode = CryptoInode.new(JSON.parse(inode_data).merge(index:index))
|
52
|
+
@files[inode.path] = { inode:inode, file:CryptoFile.new(inode.path, archive) }
|
53
|
+
inode_table.add_inode(inode)
|
54
|
+
|
55
|
+
offset += blocks*inode_width
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# don't use this to get size of what's in memory; only for what is already stored.
|
60
|
+
# need this to get read to work.
|
61
|
+
def size
|
62
|
+
return @size if @size
|
63
|
+
begin
|
64
|
+
inode_width_s, table_size_s, remainder = __read_page_keep_padding(0).split("|")
|
65
|
+
@size = table_size_s.to_i + inode_width_s.to_i
|
66
|
+
rescue Errno::ENOENT
|
67
|
+
0
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def list
|
72
|
+
@files.keys
|
73
|
+
end
|
74
|
+
|
75
|
+
def file_at_path(archive_path)
|
76
|
+
return nil unless @files.has_key?(archive_path)
|
77
|
+
@files[archive_path][:file]
|
78
|
+
end
|
79
|
+
|
80
|
+
def inode_for_path(archive_path)
|
81
|
+
return nil unless @files.has_key?(archive_path)
|
82
|
+
@files[archive_path][:inode]
|
83
|
+
end
|
84
|
+
|
85
|
+
def write
|
86
|
+
plaintext = inode_table.to_s
|
87
|
+
table_size = plaintext.length
|
88
|
+
|
89
|
+
length_str = inode_width.to_s + "|" + table_size.to_s + "|"
|
90
|
+
length_str += " " * (inode_width - length_str.length)
|
91
|
+
plaintext = length_str + plaintext
|
92
|
+
|
93
|
+
num_pages = (plaintext.length.to_f/page_size).ceil
|
94
|
+
num_pages.times do |page_num|
|
95
|
+
page_plaintext = plaintext[page_num*page_size ... (page_num+1)*page_size]
|
96
|
+
write_page(page_num, page_plaintext)
|
97
|
+
end
|
98
|
+
|
99
|
+
@size = table_size
|
100
|
+
end
|
101
|
+
|
102
|
+
def add(archive_path, fs_path)
|
103
|
+
inode = CryptoInode.new(archive_path)
|
104
|
+
existing_inode = inode_for_path(archive_path)
|
105
|
+
return if existing_inode == inode
|
106
|
+
|
107
|
+
if existing_inode then
|
108
|
+
inode = existing_inode
|
109
|
+
slots_previously_used = inode.slots_used
|
110
|
+
end
|
111
|
+
|
112
|
+
file = CryptoFile.new(archive_path, archive)
|
113
|
+
inode.set_from_fs(fs_path)
|
114
|
+
|
115
|
+
@files[archive_path] = { inode:inode, file:file, fs_path:fs_path }
|
116
|
+
file.write(open(fs_path), File.mtime(fs_path)) if inode.ftype != "directory"
|
117
|
+
|
118
|
+
if existing_inode.nil? || inode.slots_used > slots_previously_used then
|
119
|
+
inode_table.delete_inode(inode) if existing_inode
|
120
|
+
inode_table.add_inode(inode)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
def rm(file)
|
125
|
+
return unless @files[archive_path]
|
126
|
+
file_at_path(archive_path).trim_to_size(0)
|
127
|
+
inode_table.delete_inode(inode_for_path(archive_path).index)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
# zksync
|
2
|
+
# Copyright (C) 2017 Jonas Acres
|
3
|
+
|
4
|
+
# This program is free software; you can redistribute it and/or modify
|
5
|
+
# it under the terms of the GNU General Public License as published by
|
6
|
+
# the Free Software Foundation; either version 2 of the License, or
|
7
|
+
# (at your option) any later version.
|
8
|
+
|
9
|
+
# This program is distributed in the hope that it will be useful,
|
10
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
# GNU General Public License for more details.
|
13
|
+
|
14
|
+
# You should have received a copy of the GNU General Public License along
|
15
|
+
# with this program; if not, write to the Free Software Foundation, Inc.,
|
16
|
+
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
17
|
+
|
18
|
+
module ZKSync
|
19
|
+
class CryptoInode
|
20
|
+
attr_accessor :path, :mtime, :ctime, :atime, :mode, :uid, :gid, :size, :ftype, :sha256, :index
|
21
|
+
|
22
|
+
def initialize(arg)
|
23
|
+
if arg.is_a?(String)
|
24
|
+
@path = arg
|
25
|
+
else
|
26
|
+
arg.each do |k,v|
|
27
|
+
v = Time.parse(v) if [:atime, :mtime].include?(k.to_sym)
|
28
|
+
self.send(:"#{k}=", v)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def inode_width
|
34
|
+
CryptoFileIndex.inode_width
|
35
|
+
end
|
36
|
+
|
37
|
+
def slots_used
|
38
|
+
(to_s.length.to_f/inode_width).ceil
|
39
|
+
end
|
40
|
+
|
41
|
+
def set_from_fs(fs_path, opts={})
|
42
|
+
stat = File.stat(fs_path)
|
43
|
+
|
44
|
+
@fs_path = fs_path
|
45
|
+
@atime = stat.atime
|
46
|
+
@mtime = stat.mtime
|
47
|
+
@ctime = stat.ctime
|
48
|
+
@ftype = stat.ftype
|
49
|
+
@gid = stat.gid
|
50
|
+
@mode = stat.mode
|
51
|
+
@size = stat.ftype == "directory" ? 0 : stat.size
|
52
|
+
@uid = stat.uid
|
53
|
+
sha256 if opts[:hash]
|
54
|
+
end
|
55
|
+
|
56
|
+
def sha256
|
57
|
+
return nil if @ftype == "directory"
|
58
|
+
@sha256 ||= Digest::SHA256.file(@fs_path).hexdigest if @fs_path
|
59
|
+
@sha256
|
60
|
+
end
|
61
|
+
|
62
|
+
def equals(other)
|
63
|
+
[ :path, :mtime, :ctime, :atime, :mode, :uid, :gid, :size, :ftype, :sha256 ].each do |key|
|
64
|
+
return false unless self.send(key) == other.send(key)
|
65
|
+
end
|
66
|
+
|
67
|
+
true
|
68
|
+
end
|
69
|
+
|
70
|
+
def to_h
|
71
|
+
{ path:path, mtime:mtime, ctime:ctime, atime:atime, mode:mode, uid:uid, gid:gid, size:size, ftype:ftype, sha256:sha256 }
|
72
|
+
end
|
73
|
+
|
74
|
+
def to_json
|
75
|
+
to_h.to_json
|
76
|
+
end
|
77
|
+
|
78
|
+
# string is json right-space-padded to a multiple of inode_width, so we can reuse lines in @@index
|
79
|
+
def to_s
|
80
|
+
json = to_json
|
81
|
+
json.length.to_s + "|" + json
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# zksync
|
2
|
+
# Copyright (C) 2017 Jonas Acres
|
3
|
+
|
4
|
+
# This program is free software; you can redistribute it and/or modify
|
5
|
+
# it under the terms of the GNU General Public License as published by
|
6
|
+
# the Free Software Foundation; either version 2 of the License, or
|
7
|
+
# (at your option) any later version.
|
8
|
+
|
9
|
+
# This program is distributed in the hope that it will be useful,
|
10
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
# GNU General Public License for more details.
|
13
|
+
|
14
|
+
# You should have received a copy of the GNU General Public License along
|
15
|
+
# with this program; if not, write to the Free Software Foundation, Inc.,
|
16
|
+
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
17
|
+
|
18
|
+
module ZKSync
|
19
|
+
class InodeTable
|
20
|
+
attr_reader :inode_width
|
21
|
+
|
22
|
+
def initialize(inode_width)
|
23
|
+
@inode_width = inode_width
|
24
|
+
@table = []
|
25
|
+
end
|
26
|
+
|
27
|
+
def add_inode(inode)
|
28
|
+
num_slots_needed = inode.slots_used
|
29
|
+
n = find_free(num_slots_needed)
|
30
|
+
num_slots_needed.times { |i| @table[n+i] = i == 0 ? inode : :placeholder }
|
31
|
+
inode.index = n
|
32
|
+
end
|
33
|
+
|
34
|
+
def delete_inode(index)
|
35
|
+
return unless inode = @table[index]
|
36
|
+
|
37
|
+
while index < @table.length && [:placeholder, inode].include?(@table[i]) do
|
38
|
+
@table[i] = nil
|
39
|
+
index += 1
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def find_free(size)
|
44
|
+
(0..@table.length-size).to_a.each do |n|
|
45
|
+
return n if free_at_n?(n)
|
46
|
+
end
|
47
|
+
|
48
|
+
@table.length
|
49
|
+
end
|
50
|
+
|
51
|
+
def free_at_n?(n)
|
52
|
+
(0..n).to_a.each { |m| return false unless @table[m].nil? }
|
53
|
+
true
|
54
|
+
end
|
55
|
+
|
56
|
+
def to_s
|
57
|
+
# TODO: redo writing of InodeTable with a stream, so we don't have to have the whole serialized table in memory
|
58
|
+
@table.map do |entry|
|
59
|
+
case entry
|
60
|
+
when nil
|
61
|
+
" "*inode_width
|
62
|
+
when :placeholder
|
63
|
+
""
|
64
|
+
else
|
65
|
+
text = entry.to_s
|
66
|
+
text.to_s + " "*(inode_width - (text.length%inode_width)) unless text.length > 0 && text.length % inode_width == 0
|
67
|
+
end
|
68
|
+
end.join("")
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
data/lib/zksync/key.rb
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
# zksync
|
2
|
+
# Copyright (C) 2017 Jonas Acres
|
3
|
+
|
4
|
+
# This program is free software; you can redistribute it and/or modify
|
5
|
+
# it under the terms of the GNU General Public License as published by
|
6
|
+
# the Free Software Foundation; either version 2 of the License, or
|
7
|
+
# (at your option) any later version.
|
8
|
+
|
9
|
+
# This program is distributed in the hope that it will be useful,
|
10
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
# GNU General Public License for more details.
|
13
|
+
|
14
|
+
# You should have received a copy of the GNU General Public License along
|
15
|
+
# with this program; if not, write to the Free Software Foundation, Inc.,
|
16
|
+
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
17
|
+
|
18
|
+
module ZKSync
|
19
|
+
class Key
|
20
|
+
def self.size
|
21
|
+
32 # size in bytes of the key
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.write_size
|
25
|
+
return @write_size if @write_size
|
26
|
+
|
27
|
+
k = Key.new("", type: :key)
|
28
|
+
@write_size = k.encrypt(k.to_b).length
|
29
|
+
end
|
30
|
+
|
31
|
+
def initialize(passphrase, opts={})
|
32
|
+
@raw_key = case opts[:type]
|
33
|
+
when :literal
|
34
|
+
passphrase
|
35
|
+
when :passphrase
|
36
|
+
kdf(passphrase)
|
37
|
+
else
|
38
|
+
Digest::SHA256.digest(passphrase)
|
39
|
+
end
|
40
|
+
|
41
|
+
@mode = opts[:mode] || :CBC
|
42
|
+
@iv = Digest::SHA256.digest("iv" + @raw_key)
|
43
|
+
end
|
44
|
+
|
45
|
+
def kdf(passphrase)
|
46
|
+
OpenSSL::PKCS5.pbkdf2_hmac_sha1(passphrase, "ZKSync", 2**16, self.class.size)
|
47
|
+
end
|
48
|
+
|
49
|
+
def child_key(string, opts={})
|
50
|
+
Key.new(to_s + "::" + string, {type: :key}.merge(opts))
|
51
|
+
end
|
52
|
+
|
53
|
+
def write_size
|
54
|
+
to_b.length
|
55
|
+
end
|
56
|
+
|
57
|
+
def encrypt(plaintext, padding=0)
|
58
|
+
cipher = OpenSSL::Cipher::AES.new(8*self.class.size, @mode)
|
59
|
+
cipher.encrypt
|
60
|
+
cipher.key = to_b
|
61
|
+
cipher.iv = @iv
|
62
|
+
cipher.update(plaintext) + cipher.final
|
63
|
+
end
|
64
|
+
|
65
|
+
def decrypt(ciphertext)
|
66
|
+
decipher = OpenSSL::Cipher::AES.new(8*self.class.size, @mode)
|
67
|
+
decipher.decrypt
|
68
|
+
decipher.key = @raw_key
|
69
|
+
decipher.iv = @iv
|
70
|
+
decipher.update(ciphertext) + decipher.final
|
71
|
+
end
|
72
|
+
|
73
|
+
def to_s
|
74
|
+
@raw_key.unpack("H*").first
|
75
|
+
end
|
76
|
+
|
77
|
+
def to_b
|
78
|
+
@raw_key
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# zksync
|
2
|
+
# Copyright (C) 2017 Jonas Acres
|
3
|
+
|
4
|
+
# This program is free software; you can redistribute it and/or modify
|
5
|
+
# it under the terms of the GNU General Public License as published by
|
6
|
+
# the Free Software Foundation; either version 2 of the License, or
|
7
|
+
# (at your option) any later version.
|
8
|
+
|
9
|
+
# This program is distributed in the hope that it will be useful,
|
10
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
# GNU General Public License for more details.
|
13
|
+
|
14
|
+
# You should have received a copy of the GNU General Public License along
|
15
|
+
# with this program; if not, write to the Free Software Foundation, Inc.,
|
16
|
+
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
17
|
+
|
18
|
+
module ZKSync
|
19
|
+
class KeyStore
|
20
|
+
def initialize(root_key)
|
21
|
+
@root_key = root_key
|
22
|
+
end
|
23
|
+
|
24
|
+
def top_key
|
25
|
+
@top_key ||= @root_key.child_key(".topkey")
|
26
|
+
end
|
27
|
+
|
28
|
+
def fs_key
|
29
|
+
@fs_key ||= top_key.child_key(".fskey")
|
30
|
+
end
|
31
|
+
|
32
|
+
def file_key(archive_path)
|
33
|
+
fs_key.child_key(".file:#{archive_path}")
|
34
|
+
end
|
35
|
+
|
36
|
+
def page_key(archive_path, page_num, page_hash)
|
37
|
+
file_key(archive_path).child_key(".page:#{page_num}::#{page_hash}")
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/zksync.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'zksync'
|
7
|
+
s.version = ZKSync::VERSION
|
8
|
+
s.date = '2017-05-16'
|
9
|
+
s.summary = "Zero-knowledge file storage and synchronization"
|
10
|
+
s.description = "A utility for making zero-knowledge storage of sensitive data easy and rsync-friendly."
|
11
|
+
s.authors = ["Jonas Acres"]
|
12
|
+
s.email = 'opensourcejonas@gmail.com'
|
13
|
+
s.homepage = 'http://github.com/jonasacres/zksync'
|
14
|
+
s.license = 'GPLv2'
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split($/)
|
17
|
+
s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
s.test_files = s.files.grep(%r{^(test|s|features)/})
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_runtime_dependency 'trollop', "~> 2.0"
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: zksync
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jonas Acres
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-05-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: trollop
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
description: A utility for making zero-knowledge storage of sensitive data easy and
|
28
|
+
rsync-friendly.
|
29
|
+
email: opensourcejonas@gmail.com
|
30
|
+
executables:
|
31
|
+
- zksync
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- CHANGELOG
|
36
|
+
- Gemfile
|
37
|
+
- LICENSE
|
38
|
+
- bin/zksync
|
39
|
+
- lib/version.rb
|
40
|
+
- lib/zksync.rb
|
41
|
+
- lib/zksync/archive.rb
|
42
|
+
- lib/zksync/crypto_file.rb
|
43
|
+
- lib/zksync/crypto_file_index.rb
|
44
|
+
- lib/zksync/crypto_inode.rb
|
45
|
+
- lib/zksync/inode_table.rb
|
46
|
+
- lib/zksync/key.rb
|
47
|
+
- lib/zksync/key_store.rb
|
48
|
+
- zksync.gemspec
|
49
|
+
homepage: http://github.com/jonasacres/zksync
|
50
|
+
licenses:
|
51
|
+
- GPLv2
|
52
|
+
metadata: {}
|
53
|
+
post_install_message:
|
54
|
+
rdoc_options: []
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
requirements: []
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 2.0.14.1
|
70
|
+
signing_key:
|
71
|
+
specification_version: 4
|
72
|
+
summary: Zero-knowledge file storage and synchronization
|
73
|
+
test_files: []
|