scrypty 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -42,6 +42,21 @@ Example:
42
42
  decrypted = Scrypty.decrypt(encrypted, password, maxmem, maxmemfrac, maxtime)
43
43
  puts "Decrypted data: #{decrypted.inspect}"
44
44
 
45
+ orig_fn = "foo.txt"
46
+ enc_fn = "foo.dat"
47
+ dec_fn = "foo-dec.txt"
48
+
49
+ File.open(orig_fn, "w") { |f| f.print("my data") }
50
+ Scrypty.encrypt_file(orig_fn, enc_fn, password, maxmem, maxmemfrac, maxtime)
51
+ puts "Encrypted file: #{File.read(enc_fn).inspect}"
52
+ Scrypty.decrypt_file(enc_fn, dec_fn, password, maxmem, maxmemfrac, maxtime)
53
+ puts "Decrypted file: #{File.read(dec_fn).inspect}"
54
+
55
+ ## See also
56
+
57
+ * [scrypt by Colin Percival](http://www.tarsnap.com/scrypt.html)
58
+ * [libscrypt (ChromiumOS source tree)](http://git.chromium.org/gitweb/?p=chromiumos/third_party/libscrypt.git;a=summary)
59
+
45
60
  ## Contributing
46
61
 
47
62
  1. Fork it
data/Rakefile CHANGED
@@ -1 +1,20 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rake/clean"
3
+
4
+ file "ext/Makefile" => "ext/extconf.rb" do
5
+ Dir.chdir("ext") do
6
+ ruby "extconf.rb"
7
+ end
8
+ end
9
+
10
+ file "ext/scrypty_ext.so" => FileList["ext/*.c", "ext/*.h", "ext/Makefile"] do
11
+ Dir.chdir("ext") do
12
+ sh "make"
13
+ end
14
+ end
15
+
16
+ desc "Compile the scrypty extension"
17
+ task :compile => "ext/scrypty_ext.so"
18
+
19
+ CLEAN.clear
20
+ CLEAN.include(["ext/*.o", "ext/*.so", "ext/extconf.h", "ext/Makefile"])
data/ext/ruby_ext.c CHANGED
@@ -1,4 +1,5 @@
1
1
  #include <ruby.h>
2
+ #include <ruby/io.h>
2
3
  #include <stdio.h>
3
4
  #include "scryptenc.h"
4
5
 
@@ -83,14 +84,15 @@ raise_scrypty_error(errorcode)
83
84
  }
84
85
  }
85
86
 
86
- VALUE
87
- scrypty_encrypt(rb_obj, rb_data, rb_password, rb_maxmem, rb_maxmemfrac, rb_maxtime)
87
+ static VALUE
88
+ scrypty_buffer(rb_obj, rb_data, rb_password, rb_maxmem, rb_maxmemfrac, rb_maxtime, encrypt)
88
89
  VALUE rb_obj;
89
90
  VALUE rb_data;
90
91
  VALUE rb_password;
91
92
  VALUE rb_maxmem;
92
93
  VALUE rb_maxmemfrac;
93
94
  VALUE rb_maxtime;
95
+ int encrypt;
94
96
  {
95
97
  VALUE rb_out;
96
98
  char *data, *password, *out;
@@ -135,15 +137,26 @@ scrypty_encrypt(rb_obj, rb_data, rb_password, rb_maxmem, rb_maxmemfrac, rb_maxti
135
137
  rb_raise(rb_eTypeError, "fifth argument (maxtime) must be a Fixnum or Float");
136
138
  }
137
139
 
138
- out_len = data_len + 128;
139
- rb_out = rb_str_new(NULL, out_len);
140
- out = RSTRING_PTR(rb_out);
140
+ if (encrypt) {
141
+ out_len = data_len + 128;
142
+ rb_out = rb_str_new(NULL, out_len);
143
+ out = RSTRING_PTR(rb_out);
144
+
145
+ errorcode = scryptenc_buf((const uint8_t *) data, data_len,
146
+ (uint8_t *) out, (const uint8_t *) password, password_len,
147
+ maxmem, maxmemfrac, maxtime);
148
+ }
149
+ else {
150
+ rb_out = rb_str_new(NULL, data_len);
151
+ out = RSTRING_PTR(rb_out);
152
+
153
+ errorcode = scryptdec_buf((const uint8_t *) data, data_len,
154
+ (uint8_t *) out, &out_len, (const uint8_t *) password, password_len,
155
+ maxmem, maxmemfrac, maxtime);
156
+ }
141
157
 
142
- errorcode = scryptenc_buf((const uint8_t *) data, data_len,
143
- (uint8_t *) out, (const uint8_t *) password, password_len,
144
- maxmem, maxmemfrac, maxtime);
145
158
  if (errorcode) {
146
- raise_scrypt_error(errorcode);
159
+ raise_scrypty_error(errorcode);
147
160
  }
148
161
  rb_str_set_len(rb_out, out_len);
149
162
 
@@ -151,7 +164,7 @@ scrypty_encrypt(rb_obj, rb_data, rb_password, rb_maxmem, rb_maxmemfrac, rb_maxti
151
164
  }
152
165
 
153
166
  VALUE
154
- scrypty_decrypt(rb_obj, rb_data, rb_password, rb_maxmem, rb_maxmemfrac, rb_maxtime)
167
+ scrypty_encrypt_buffer(rb_obj, rb_data, rb_password, rb_maxmem, rb_maxmemfrac, rb_maxtime)
155
168
  VALUE rb_obj;
156
169
  VALUE rb_data;
157
170
  VALUE rb_password;
@@ -159,69 +172,133 @@ scrypty_decrypt(rb_obj, rb_data, rb_password, rb_maxmem, rb_maxmemfrac, rb_maxti
159
172
  VALUE rb_maxmemfrac;
160
173
  VALUE rb_maxtime;
161
174
  {
162
- VALUE rb_out;
163
- char *data, *password, *out;
164
- size_t data_len, password_len, out_len, maxmem;
175
+ return scrypty_buffer(rb_obj, rb_data, rb_password, rb_maxmem, rb_maxmemfrac,
176
+ rb_maxtime, 1);
177
+ }
178
+
179
+ VALUE
180
+ scrypty_decrypt_buffer(rb_obj, rb_data, rb_password, rb_maxmem, rb_maxmemfrac, rb_maxtime)
181
+ VALUE rb_obj;
182
+ VALUE rb_data;
183
+ VALUE rb_password;
184
+ VALUE rb_maxmem;
185
+ VALUE rb_maxmemfrac;
186
+ VALUE rb_maxtime;
187
+ {
188
+ return scrypty_buffer(rb_obj, rb_data, rb_password, rb_maxmem, rb_maxmemfrac,
189
+ rb_maxtime, 0);
190
+ }
191
+
192
+ VALUE
193
+ scrypty_file(rb_obj, rb_infn, rb_outfn, rb_password, rb_maxmem, rb_maxmemfrac, rb_maxtime, encrypt)
194
+ VALUE rb_obj;
195
+ VALUE rb_infn;
196
+ VALUE rb_outfn;
197
+ VALUE rb_password;
198
+ VALUE rb_maxmem;
199
+ VALUE rb_maxmemfrac;
200
+ VALUE rb_maxtime;
201
+ int encrypt;
202
+ {
203
+ VALUE rb_infile, rb_outfile;
204
+ FILE *in, *out;
205
+ rb_io_t *in_p, *out_p;
206
+ char *password;
207
+ size_t password_len;
208
+ int errorcode, maxmem;
165
209
  double maxmemfrac, maxtime;
166
- int errorcode;
167
210
 
168
- if (TYPE(rb_data) == T_STRING) {
169
- data = RSTRING_PTR(rb_data);
170
- data_len = (size_t) RSTRING_LEN(rb_data);
171
- }
172
- else {
173
- rb_raise(rb_eTypeError, "first argument (data) must be a String");
174
- }
211
+ rb_infile = rb_file_open_str(rb_infn, "rb");
212
+ rb_outfile = rb_file_open_str(rb_outfn, "wb");
175
213
 
176
214
  if (TYPE(rb_password) == T_STRING) {
177
215
  password = RSTRING_PTR(rb_password);
178
216
  password_len = (size_t) RSTRING_LEN(rb_password);
179
217
  }
180
218
  else {
181
- rb_raise(rb_eTypeError, "second argument (password) must be a String");
219
+ rb_raise(rb_eTypeError, "third argument (password) must be a String");
182
220
  }
183
221
 
184
222
  if (TYPE(rb_maxmem) == T_FIXNUM) {
185
223
  maxmem = FIX2INT(rb_maxmem);
186
224
  }
187
225
  else {
188
- rb_raise(rb_eTypeError, "third argument (maxmem) must be a Fixnum");
226
+ rb_raise(rb_eTypeError, "fourth argument (maxmem) must be a Fixnum");
189
227
  }
190
228
 
191
229
  if (FIXNUM_P(rb_maxmemfrac) || TYPE(rb_maxmemfrac) == T_FLOAT) {
192
230
  maxmemfrac = NUM2DBL(rb_maxmemfrac);
193
231
  }
194
232
  else {
195
- rb_raise(rb_eTypeError, "fourth argument (maxmemfrac) must be a Fixnum or Float");
233
+ rb_raise(rb_eTypeError, "fifth argument (maxmemfrac) must be a Fixnum or Float");
196
234
  }
197
235
 
198
236
  if (FIXNUM_P(rb_maxtime) || TYPE(rb_maxtime) == T_FLOAT) {
199
237
  maxtime = NUM2DBL(rb_maxtime);
200
238
  }
201
239
  else {
202
- rb_raise(rb_eTypeError, "fifth argument (maxtime) must be a Fixnum or Float");
240
+ rb_raise(rb_eTypeError, "sixth argument (maxtime) must be a Fixnum or Float");
203
241
  }
204
242
 
205
- rb_out = rb_str_new(NULL, data_len);
206
- out = RSTRING_PTR(rb_out);
243
+ GetOpenFile(rb_infile, in_p);
244
+ in = rb_io_stdio_file(in_p);
245
+ GetOpenFile(rb_outfile, out_p);
246
+ out = rb_io_stdio_file(out_p);
247
+
248
+ if (encrypt) {
249
+ errorcode = scryptenc_file(in, out, (const uint8_t *) password,
250
+ password_len, maxmem, maxmemfrac, maxtime);
251
+ }
252
+ else {
253
+ errorcode = scryptdec_file(in, out, (const uint8_t *) password,
254
+ password_len, maxmem, maxmemfrac, maxtime);
255
+ }
256
+ rb_io_close(rb_infile);
257
+ rb_io_close(rb_outfile);
207
258
 
208
- errorcode = scryptdec_buf((const uint8_t *) data, data_len,
209
- (uint8_t *) out, &out_len, (const uint8_t *) password, password_len,
210
- maxmem, maxmemfrac, maxtime);
211
259
  if (errorcode) {
212
260
  raise_scrypty_error(errorcode);
213
261
  }
214
- rb_str_set_len(rb_out, out_len);
215
262
 
216
- return rb_out;
263
+ return Qnil;
264
+ }
265
+
266
+ VALUE
267
+ scrypty_encrypt_file(rb_obj, rb_infn, rb_outfn, rb_password, rb_maxmem, rb_maxmemfrac, rb_maxtime)
268
+ VALUE rb_obj;
269
+ VALUE rb_infn;
270
+ VALUE rb_outfn;
271
+ VALUE rb_password;
272
+ VALUE rb_maxmem;
273
+ VALUE rb_maxmemfrac;
274
+ VALUE rb_maxtime;
275
+ {
276
+ return scrypty_file(rb_obj, rb_infn, rb_outfn, rb_password, rb_maxmem,
277
+ rb_maxmemfrac, rb_maxtime, 1);
278
+ }
279
+
280
+ VALUE
281
+ scrypty_decrypt_file(rb_obj, rb_infn, rb_outfn, rb_password, rb_maxmem, rb_maxmemfrac, rb_maxtime)
282
+ VALUE rb_obj;
283
+ VALUE rb_infn;
284
+ VALUE rb_outfn;
285
+ VALUE rb_password;
286
+ VALUE rb_maxmem;
287
+ VALUE rb_maxmemfrac;
288
+ VALUE rb_maxtime;
289
+ {
290
+ return scrypty_file(rb_obj, rb_infn, rb_outfn, rb_password, rb_maxmem,
291
+ rb_maxmemfrac, rb_maxtime, 0);
217
292
  }
218
293
 
219
294
  void
220
295
  Init_scrypty_ext(void)
221
296
  {
222
297
  mScrypty = rb_define_module("Scrypty");
223
- rb_define_singleton_method(mScrypty, "encrypt", scrypty_encrypt, 5);
224
- rb_define_singleton_method(mScrypty, "decrypt", scrypty_decrypt, 5);
298
+ rb_define_singleton_method(mScrypty, "encrypt", scrypty_encrypt_buffer, 5);
299
+ rb_define_singleton_method(mScrypty, "decrypt", scrypty_decrypt_buffer, 5);
300
+ rb_define_singleton_method(mScrypty, "encrypt_file", scrypty_encrypt_file, 6);
301
+ rb_define_singleton_method(mScrypty, "decrypt_file", scrypty_decrypt_file, 6);
225
302
 
226
303
  eScryptyError = rb_define_class_under(mScrypty, "Exception", rb_eException);
227
304
  eMemoryLimitError = rb_define_class_under(mScrypty, "MemoryLimitError", eScryptyError);
@@ -1,3 +1,3 @@
1
1
  module Scrypty
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/scrypty.gemspec CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |gem|
10
10
  gem.email = ["viking@pillageandplunder.net"]
11
11
  gem.description = %q{Uses the scrypt algorithm by Colin Percival to encrypt/decrypt data}
12
12
  gem.summary = %q{Utility to encrypt/decrypt data with the scrypt algorithm}
13
- gem.homepage = "https://github.com/viking/scrypt-full"
13
+ gem.homepage = "https://github.com/viking/scrypty"
14
14
 
15
15
  gem.files = `git ls-files`.split($/)
16
16
  gem.extensions = gem.files.grep(%r{extconf.rb})
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scrypty
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -45,7 +45,7 @@ files:
45
45
  - lib/scrypty.rb
46
46
  - lib/scrypty/version.rb
47
47
  - scrypty.gemspec
48
- homepage: https://github.com/viking/scrypt-full
48
+ homepage: https://github.com/viking/scrypty
49
49
  licenses: []
50
50
  post_install_message:
51
51
  rdoc_options: []
@@ -70,4 +70,3 @@ signing_key:
70
70
  specification_version: 3
71
71
  summary: Utility to encrypt/decrypt data with the scrypt algorithm
72
72
  test_files: []
73
- has_rdoc: