ttcrypt 0.0.7 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -21,7 +21,11 @@
21
21
 
22
22
  #include "ttcrypt.h"
23
23
  #include "sha1.h"
24
+ #include "sph_sha2.h"
25
+
26
+ extern "C" {
24
27
  #include "sha256.h"
28
+ }
25
29
 
26
30
  using namespace thrift;
27
31
 
@@ -40,6 +44,15 @@ byte_buffer ttcrypt::sha256(const thrift::byte_buffer &data) noexcept {
40
44
  return res;
41
45
  }
42
46
 
47
+ byte_buffer ttcrypt::sha512(const thrift::byte_buffer &data) noexcept {
48
+ byte_buffer res(64);
49
+ sph_sha512_context cxt;
50
+ sph_sha512_init(&cxt);
51
+ sph_sha512(&cxt, data.data().get(), (size_t) data.size());
52
+ sph_sha512_close(&cxt, res.data().get());
53
+ return res;
54
+ }
55
+
43
56
  byte_buffer ttcrypt::i2osp(const big_integer& i, size_t block_size) noexcept {
44
57
  byte_buffer res = i.to_byte_buffer();
45
58
  if( block_size > 0 && res.size() != block_size ) {
@@ -32,7 +32,8 @@ namespace ttcrypt {
32
32
 
33
33
  byte_buffer sha1(const byte_buffer& data) noexcept;
34
34
  byte_buffer sha256(const byte_buffer& data) noexcept;
35
-
35
+ byte_buffer sha512(const byte_buffer& data) noexcept;
36
+
36
37
  byte_buffer i2osp(const big_integer& i, size_t block_size=0) noexcept;
37
38
 
38
39
  inline big_integer os2ip(const byte_buffer& buffer) noexcept {
@@ -101,6 +101,20 @@ static VALUE rsa_decrypt(VALUE self, VALUE rb_data) {
101
101
  });
102
102
  }
103
103
 
104
+ static VALUE ttcrypt_sha256(VALUE self,VALUE rb_data) {
105
+ return wrap_exceptions([=] {
106
+ byte_buffer src = value_to_byte_buffer(rb_data);
107
+ return to_rb_string(sha256(src));
108
+ });
109
+ }
110
+
111
+ static VALUE ttcrypt_sha512(VALUE self,VALUE rb_data) {
112
+ return wrap_exceptions([=] {
113
+ byte_buffer src = value_to_byte_buffer(rb_data);
114
+ return to_rb_string(sha512(src));
115
+ });
116
+ }
117
+
104
118
  static VALUE factorize(VALUE self, VALUE composite) {
105
119
  return wrap_exceptions([=] {
106
120
  string s = value_to_string(composite);
@@ -118,6 +132,26 @@ static VALUE factorize(VALUE self, VALUE composite) {
118
132
  });
119
133
  }
120
134
 
135
+ static VALUE factorize2(VALUE self, VALUE composite) {
136
+ return wrap_exceptions([=] {
137
+ string s = value_to_string(composite);
138
+ byte_buffer buffer(RSTRING_PTR(composite), RSTRING_LEN(composite));
139
+
140
+ vector<big_integer> factors;
141
+ ruby_unblock([&buffer,&factors] {
142
+ factors = pollard_rho::factorize(big_integer(buffer));
143
+ });
144
+
145
+
146
+ VALUE result = rb_ary_new();
147
+ for (auto factor : factors) {
148
+ byte_buffer b = factor.to_byte_buffer();
149
+ rb_ary_push(result, rb_str_new( (const char*)b.data().get(), (size_t)b.size()) );
150
+ }
151
+ return result;
152
+ });
153
+ }
154
+
121
155
  static VALUE _generate_prime(VALUE self, VALUE bits) {
122
156
  return wrap_exceptions([=] {
123
157
  unsigned nbits = FIX2INT(bits);
@@ -143,6 +177,8 @@ static hash_t hash_provider(VALUE name) {
143
177
  string n = value_to_string(name);
144
178
  if (n == "sha256")
145
179
  return sha256;
180
+ else if (n == "sha512")
181
+ return sha512;
146
182
  else if (n == "sha1")
147
183
  return sha1;
148
184
  else
@@ -232,7 +268,10 @@ void Init_ttcrypt(void) {
232
268
  VALUE ttcrypt_module = rb_define_module("TTCrypt");
233
269
 
234
270
  rb_define_method(ttcrypt_module, "_factorize", (ruby_method) factorize, 1);
271
+ rb_define_method(ttcrypt_module, "_factorize2", (ruby_method) factorize2, 1);
235
272
  rb_define_method(ttcrypt_module, "_generate_prime", (ruby_method) _generate_prime, 1);
273
+ rb_define_method(ttcrypt_module, "sha256", (ruby_method) ttcrypt_sha256, 1);
274
+ rb_define_method(ttcrypt_module, "sha512", (ruby_method) ttcrypt_sha512, 1);
236
275
 
237
276
  rsa_class = rb_define_class_under(ttcrypt_module, "RsaKey", rb_cObject);
238
277
  rb_define_alloc_func(rsa_class, rsa_alloc);
@@ -248,6 +287,7 @@ void Init_ttcrypt(void) {
248
287
  rb_define_method(rsa_class, "_components", (ruby_method) rsa_components, 0);
249
288
  rb_define_method(rsa_class, "_set_params", (ruby_method) rsa_set_params, 1);
250
289
 
290
+
251
291
  rsa_exception = rb_define_class_under(rsa_class, "Error",
252
292
  rb_eStandardError);
253
293
  }
data/lib/ttcrypt.rb CHANGED
@@ -1,16 +1,54 @@
1
1
  # Thrift cryptographics primitives: fast c++ implementation, only strong schemes,
2
2
  # releases GVL on long operations so other threads can be executed in parallel.
3
+
4
+
5
+ class Numeric
6
+
7
+ # Convert an integer non-negative number that to bytes array using specified endianness. if it is
8
+ # float, it will be converted to an integer first.
9
+ #
10
+ # @return [Symbol] either :BE or :LE
11
+ def to_bytes order: :BE
12
+ order == :BE || order == :LE or raise ArgimentError, "unkown order, should be either :BE or :LE"
13
+ (value = self.to_i) < 0 and raise ArgumentError, 'value must not be negative'
14
+ result = ''
15
+ result.force_encoding 'binary'
16
+ while value != 0
17
+ byte = value & 0xFF
18
+ value >>= 8
19
+ result << byte.chr
20
+ end
21
+ result == '' ? "\x0" : (order == :BE ? result.reverse : result)
22
+ end
23
+
24
+ end
25
+
26
+ class String
27
+
28
+ # Convert string that is supposed to be binary data to integer value
29
+ # using specified bytes order
30
+ # @return [Symbol] either :BE or :LE
31
+ def bytes_to_integer order: :BE
32
+ order == :BE || order == :LE or raise ArgimentError, "unkown order, should be either :BE or :LE"
33
+ result = 0
34
+ (order == :BE ? self.bytes : self.bytes.reverse).each { |b|
35
+ result = (result << 8) | b.ord
36
+ }
37
+ result
38
+ end
39
+ end
40
+
3
41
  module TTCrypt
4
- # Your code goes here...
5
42
 
6
43
  # Pollard 'rho' prime factorization. Allows execution of other ruby
7
44
  # threads in parallel (releases GVL)
8
45
  #
9
46
  # @return [int] array of prime factors
10
47
  def factorize composite
11
- hex = composite.to_i.to_s(16)
12
- hex = '0' + hex if (hex.length & 1) == 1
13
- _factorize(hex).map { |x| x.to_i(16) }
48
+ _factorize2(composite.to_bytes).map { |f| f.bytes_to_integer }
49
+ # hex = composite.to_i.to_s(16)
50
+ # hex = '0' + hex if (hex.length & 1) == 1
51
+ # _factorize(hex).map { |x| x.to_i(16) }
14
52
  end
15
53
 
16
54
  # Generate random probable prime number with a given bits length. This implementation will generate
@@ -20,6 +58,22 @@ module TTCrypt
20
58
  _generate_prime(bits).to_i(16)
21
59
  end
22
60
 
61
+ # Generate fast SHA512 hash of a source string and return it in the binary form
62
+ #
63
+ # @param [String] source binary string
64
+ # @return [String] binary string with calculated hash code
65
+ def sha512(source)
66
+ # stub for documentation, real finction is in the native code
67
+ end
68
+
69
+ # Generate fast SHA256 hash of a source string and return it in the binary form
70
+ #
71
+ # @param [String] source binary string
72
+ # @return [String] binary string with calculated hash code
73
+ def sha256(source)
74
+ # stub for documentation, real finction is in the native code
75
+ end
76
+
23
77
  # Implementation of RSAES-OAEP encryption and RSASSA-PSS signing
24
78
  # accroding to pkcs#1 v2.2 specification. Does NOT implement any previous cryptographically
25
79
  # weak shcemes (like 1.5 signature) - go use openssl for itm but it does compromise private
@@ -88,7 +142,7 @@ module TTCrypt
88
142
  message.force_encoding Encoding::BINARY
89
143
  _sign message, hash_name.to_s.downcase
90
144
  end
91
-
145
+
92
146
  # Check message signature signed with pkcs#1 v2.2 RSASSA-PSS
93
147
  # process
94
148
  #
@@ -144,6 +198,6 @@ end
144
198
  require 'ttcrypt/ttcrypt'
145
199
 
146
200
  module TTCrypt
147
- module_function :factorize, :_factorize, :generate_prime, :_generate_prime
201
+ module_function :factorize, :_factorize, :_factorize2, :generate_prime, :_generate_prime, :sha256, :sha512
148
202
  end
149
203
 
@@ -1,5 +1,5 @@
1
1
  module TTCrypt
2
2
 
3
- VERSION = '0.0.7'
3
+ VERSION = '0.1.0'
4
4
 
5
5
  end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'bigint-tools' do
4
+
5
+ it 'should conver big integer to byte array and back' do
6
+
7
+ value = 0x111122223333444455556666777788889999aaaabbbbccccddddeeeeffff
8
+ value_le =0xffffeeeeddddccccbbbbaaaa999988887777666655554444333322221111
9
+ value.to_bytes(order: :BE).bytes_to_integer.should == value
10
+
11
+ value.to_bytes(order: :LE).bytes_to_integer.should == value_le
12
+ value.to_bytes(order: :LE).bytes_to_integer(order: :LE).should == value
13
+ end
14
+
15
+ end
data/spec/spec_helper.rb CHANGED
@@ -4,11 +4,27 @@
4
4
  # loaded once.
5
5
  #
6
6
  # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ require 'ttcrypt'
8
+
7
9
  RSpec.configure do |config|
8
- config.treat_symbols_as_metadata_keys_with_true_values = true
10
+ # config.treat_symbols_as_metadata_keys_with_true_values = true
9
11
  config.run_all_when_everything_filtered = true
10
12
  config.filter_run :focus
11
13
 
14
+ config.expect_with :rspec do |expectations|
15
+ # This option will default to `true` in RSpec 4. It makes the `description`
16
+ # and `failure_message` of custom matchers include text for helper methods
17
+ # defined using `chain`, e.g.:
18
+ # be_bigger_than(2).and_smaller_than(4).description
19
+ # # => "be bigger than 2 and smaller than 4"
20
+ # ...rather than:
21
+ # # => "be bigger than 2"
22
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
23
+ expectations.syntax = [:should, :expect]
24
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
25
+ end
26
+
27
+
12
28
  # Run specs in random order to surface order dependencies. If you find an
13
29
  # order dependency and want to debug it, you can fix the order by providing
14
30
  # the seed, which is printed after each run.
data/spec/ttcrypt_spec.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  require 'spec_helper'
2
- require 'ttcrypt'
3
2
  require 'securerandom'
4
3
  require 'base64'
5
4
  require 'openssl'
@@ -22,7 +21,7 @@ describe 'rsa-oaep' do
22
21
  end
23
22
 
24
23
  it 'should generate primes' do
25
- bits = 35
24
+ bits = 35
26
25
  primes = 2.times.map {
27
26
  x = TTCrypt.generate_prime bits
28
27
  x.should > (1<<(bits-1))
@@ -66,13 +65,13 @@ describe 'rsa-oaep' do
66
65
  decrypted.encoding.should == Encoding::BINARY
67
66
 
68
67
  -> { @key.encrypt 'way too long message to encrypt it!!!!!!!'*12 }
69
- .should raise_error(TTCrypt::RsaKey::Error)
68
+ .should raise_error(TTCrypt::RsaKey::Error)
70
69
 
71
70
  end
72
71
 
73
72
  it 'should round trip signatures' do
74
73
  message = 'che bella cosa'
75
- %i|sha1 sha256|.each { |hash_name|
74
+ %i|sha1 sha256 sha512|.each { |hash_name|
76
75
  signature = @key.sign(message, hash_name)
77
76
  signature.length.should == 128
78
77
  signature.encoding.should == Encoding::BINARY
@@ -83,8 +82,8 @@ describe 'rsa-oaep' do
83
82
  @key.verify(message, bad_signature, hash_name).should be_falsey
84
83
  @key.verify(message, signature, hash_name).should be_truthy
85
84
  }
86
- -> { @key.sign(message, :wrong_hash) }.should raise_error
87
- -> { @key.verify(message, 'no matter', :wrong_hash) }.should raise_error
85
+ -> { @key.sign(message, :wrong_hash) }.should raise_error(StandardError)
86
+ -> { @key.verify(message, 'no matter', :wrong_hash) }.should raise_error(StandardError)
88
87
  end
89
88
 
90
89
  it 'should extract public key' do
@@ -131,6 +130,12 @@ describe 'rsa-oaep' do
131
130
  key.decrypt(key.encrypt(@message)).should == @message
132
131
  key.decrypt(key.extract_public.encrypt(@message)).should == @message
133
132
  end
133
+
134
+ it 'should provide fast sha256' do
135
+ source = "Hello everybody! We're so glad to have you all right hère!"
136
+ TTCrypt.sha256(source).should == Digest::SHA256.new.digest(source)
137
+ TTCrypt.sha512(source).should == Digest::SHA512.new.digest(source)
138
+ end
134
139
 
135
140
  it 'should properly sign'
136
141
 
@@ -176,157 +181,157 @@ describe 'rsa-oaep' do
176
181
  end
177
182
 
178
183
 
179
- # include Ttcrypt::NumUtils
180
- #
181
- # before :all do
182
- # # test vectors
183
- # init_test_vectors
184
- # end
185
- #
186
- # it 'should convert long to bytes and back' do
187
- # 30.times {
188
- # n = SecureRandom.random_number (17+SecureRandom.random_number(157))
189
- # k = SecureRandom.random_number(5) + 2
190
- # bytes = long_to_bytes n, k
191
- # (bytes.length % k).should == 0
192
- # bytes_to_long(bytes).should == n
193
- # }
194
- #
195
- # src = "\x00\v\x9DtX\xA2\xAB\xAF%\xD4\xE9Xz\x9F\x9C\xC4\b\r\xDE\x14\xD8\x17\x01\xE1\x04\x04\x92\x16\xCD\x1D\x17+\xB1\xA0&6\xF9'\x8FsK\x95\xCC\x161\xAD3\xBB\x8F\xBE\x11\xBDP\xE4Z\x8E\x8Cz\xD7\x95\xC8\xA5(\x8E"
196
- # long_to_bytes(bytes_to_long(src), src.length).should == src
197
- #
198
- # long_to_bytes(0, 5).should == "\x00\x00\x00\x00\x00".force_encoding(Encoding::BINARY)
199
- # long_to_bytes(1, 2).should == "\x00\x01".force_encoding(Encoding::BINARY)
200
- # end
201
- #
202
- # it 'it should run gmp' do
203
- # a = GMP.Z((_a=11098707803864973769487639874))
204
- # b = GMP.Z((_b=23456))
205
- # c = GMP.Z((_c=803947509837450987038475))
206
- # r = a.powmod(b, c)
207
- # r1 = (_a ** _b) % _c
208
- # r.should == r1
209
- # end
210
- #
211
- # it 'should properly pad' do
212
- # k = (bitlength(@n)+7)/8
213
- # Ttcrypt::RsaKey.set_debug_oaep_seed @seed
214
- # p k
215
- # res = Ttcrypt::RsaKey.eme_oaep_encode(long_to_bytes(@message), k-1)
216
- # bytes_to_long(res).should == @em
217
- # end
218
- #
219
- # it 'should properly depad' do
220
- # src = Ttcrypt::RsaKey.eme_oaep_decode long_to_bytes(@em)
221
- # bytes_to_long(src).should == @message
222
- # end
223
- #
224
- # it 'should properly public encrypt' do
225
- # em = test_key.public_encrypt long_to_bytes(@message)
226
- # bytes_to_long(em).should == @encrypted_m
227
- # end
228
- #
229
- # it 'should properly private decrypt' do
230
- # m = test_key(restrict: true).private_decrypt long_to_bytes(@encrypted_m)
231
- # bytes_to_long(m).should == @message
232
- # m = test_key.private_decrypt long_to_bytes(@encrypted_m)
233
- # bytes_to_long(m).should == @message
234
- #
235
- # # a = 123101010122
236
- # # b = 778901
237
- # # puts "Inverse #{a}, #{b}-> #{inverse(a,b)}"
238
- #
239
- # puts "Sha1 empty "+Digest::SHA1.digest('').to_hex
240
- # puts "Sha1 sergeych forever "+Digest::SHA1.digest('sergeych forever').to_hex
241
- # end
242
- #
243
- # it 'should properly private encrypt and public decrypt'
244
- #
245
- # it 'should generate keys'
246
- #
247
- # it 'should construct crypstie keys'
248
- # it 'should serialize crypstie keys'
249
- #
250
- # def h2s hex_string
251
- # hex_string.gsub(/\s+/, '').to_i(16)
252
- # end
253
- #
254
- # def test_key restrict: false
255
- # Ttcrypt::RsaKey.set_debug_oaep_seed @seed
256
- # if restrict
257
- # Ttcrypt::RsaKey.new n: @n, e: @e, d: inverse(@e, lcm(@p - 1, @q - 1))
258
- # else
259
- # Ttcrypt::RsaKey.new n: @n, e: @e, p: @p, q: @q
260
- # end
261
- # end
262
- #
263
- # def init_test_vectors
264
- # @n = h2s <<-End
265
- # bb f8 2f 09 06 82 ce 9c 23 38 ac 2b 9d a8 71 f7 36 8d 07 ee d4 10 43 a4
266
- # 40 d6 b6 f0 74 54 f5 1f b8 df ba af 03 5c 02 ab 61 ea 48 ce eb 6f cd 48
267
- # 76 ed 52 0d 60 e1 ec 46 19 71 9d 8a 5b 8b 80 7f af b8 e0 a3 df c7 37 72
268
- # 3e e6 b4 b7 d9 3a 25 84 ee 6a 64 9d 06 09 53 74 88 34 b2 45 45 98 39 4e
269
- # e0 aa b1 2d 7b 61 a5 1f 52 7a 9a 41 f6 c1 68 7f e2 53 72 98 ca 2a 8f 59
270
- # 46 f8 e5 fd 09 1d bd cb
271
- # End
272
- #
273
- # @e = 0x11
274
- #
275
- # @p = h2s <<-End
276
- # ee cf ae 81 b1 b9 b3 c9 08 81 0b 10 a1 b5 60 01 99 eb 9f 44 ae f4 fd a4
277
- # 93 b8 1a 9e 3d 84 f6 32 12 4e f0 23 6e 5d 1e 3b 7e 28 fa e7 aa 04 0a 2d
278
- # 5b 25 21 76 45 9d 1f 39 75 41 ba 2a 58 fb 65 99
279
- # End
280
- #
281
- # @q = h2s <<-End
282
- # c9 7f b1 f0 27 f4 53 f6 34 12 33 ea aa d1 d9 35 3f 6c 42 d0 88 66 b1 d0
283
- # 5a 0f 20 35 02 8b 9d 86 98 40 b4 16 66 b4 2e 92 ea 0d a3 b4 32 04 b5 cf
284
- # ce 33 52 52 4d 04 16 a5 a4 41 e7 00 af 46 15 03
285
- # End
286
- #
287
- # @dP = h2s <<-End
288
- # 54 49 4c a6 3e ba 03 37 e4 e2 40 23 fc d6 9a 5a eb 07 dd dc 01 83 a4 d0
289
- # ac 9b 54 b0 51 f2 b1 3e d9 49 09 75 ea b7 74 14 ff 59 c1 f7 69 2e 9a 2e
290
- # 20 2b 38 fc 91 0a 47 41 74 ad c9 3c 1f 67 c9 81
291
- # End
292
- #
293
- # @dQ = h2s <<-End
294
- # 47 1e 02 90 ff 0a f0 75 03 51 b7 f8 78 86 4c a9 61 ad bd 3a 8a 7e 99 1c
295
- # 5c 05 56 a9 4c 31 46 a7 f9 80 3f 8f 6f 8a e3 42 e9 31 fd 8a e4 7a 22 0d
296
- # 1b 99 a4 95 84 98 07 fe 39 f9 24 5a 98 36 da 3d
297
- # End
298
- #
299
- # @qInv = h2s <<-End
300
- # b0 6c 4f da bb 63 01 19 8d 26 5b db ae 94 23 b3 80 f2 71 f7 34 53 88 50
301
- # 93 07 7f cd 39 e2 11 9f c9 86 32 15 4f 58 83 b1 67 a9 67 bf 40 2b 4e 9e
302
- # 2e 0f 96 56 e6 98 ea 36 66 ed fb 25 79 80 39 f7
303
- # End
304
- #
305
- # @message = h2s 'd4 36 e9 95 69 fd 32 a7 c8 a0 5b bc 90 d3 2c 49'
306
- #
307
- # @pHash = h2s 'da 39 a3 ee 5e 6b 4b 0d 32 55 bf ef 95 60 18 90 af d8 07 09'
308
- #
309
- # @seed = h2s 'aa fd 12 f6 59 ca e6 34 89 b4 79 e5 07 6d de c2 f0 6c b5 8f'
310
- #
311
- # @em = h2s <<-End
312
- # eb 7a 19 ac e9 e3 00 63 50 e3 29 50 4b 45 e2 ca 82 31 0b 26 dc d8 7d 5c 68
313
- # f1 ee a8 f5 52 67 c3 1b 2e 8b b4 25 1f 84 d7 e0 b2 c0 46 26 f5 af f9 3e dc
314
- # fb 25 c9 c2 b3 ff 8a e1 0e 83 9a 2d db 4c dc fe 4f f4 77 28 b4 a1 b7 c1 36
315
- # 2b aa d2 9a b4 8d 28 69 d5 02 41 21 43 58 11 59 1b e3 92 f9 82 fb 3e 87 d0
316
- # 95 ae b4 04 48 db 97 2f 3a c1 4f 7b c2 75 19 52 81 ce 32 d2 f1 b7 6d 4d 35
317
- # 3e 2d
318
- # End
319
- #
320
- # @encrypted_m = h2s <<-End
321
- # 12 53 e0 4d c0 a5 39 7b b4 4a 7a b8 7e 9b f2 a0 39 a3 3d 1e 99 6f c8 2a 94
322
- # cc d3 00 74 c9 5d f7 63 72 20 17 06 9e 52 68 da 5d 1c 0b 4f 87 2c f6 53 c1
323
- # 1d f8 23 14 a6 79 68 df ea e2 8d ef 04 bb 6d 84 b1 c3 1d 65 4a 19 70 e5 78
324
- # 3b d6 eb 96 a0 24 c2 ca 2f 4a 90 fe 9f 2e f5 c9 c1 40 e5 bb 48 da 95 36 ad
325
- # 87 00 c8 4f c9 13 0a de a7 4e 55 8d 51 a7 4d df 85 d8 b5 0d e9 68 38 d6 06
326
- # 3e 09 55
327
- # End
328
- # end
329
- #
184
+ # include Ttcrypt::NumUtils
185
+ #
186
+ # before :all do
187
+ # # test vectors
188
+ # init_test_vectors
189
+ # end
190
+ #
191
+ # it 'should convert long to bytes and back' do
192
+ # 30.times {
193
+ # n = SecureRandom.random_number (17+SecureRandom.random_number(157))
194
+ # k = SecureRandom.random_number(5) + 2
195
+ # bytes = long_to_bytes n, k
196
+ # (bytes.length % k).should == 0
197
+ # bytes_to_long(bytes).should == n
198
+ # }
199
+ #
200
+ # src = "\x00\v\x9DtX\xA2\xAB\xAF%\xD4\xE9Xz\x9F\x9C\xC4\b\r\xDE\x14\xD8\x17\x01\xE1\x04\x04\x92\x16\xCD\x1D\x17+\xB1\xA0&6\xF9'\x8FsK\x95\xCC\x161\xAD3\xBB\x8F\xBE\x11\xBDP\xE4Z\x8E\x8Cz\xD7\x95\xC8\xA5(\x8E"
201
+ # long_to_bytes(bytes_to_long(src), src.length).should == src
202
+ #
203
+ # long_to_bytes(0, 5).should == "\x00\x00\x00\x00\x00".force_encoding(Encoding::BINARY)
204
+ # long_to_bytes(1, 2).should == "\x00\x01".force_encoding(Encoding::BINARY)
205
+ # end
206
+ #
207
+ # it 'it should run gmp' do
208
+ # a = GMP.Z((_a=11098707803864973769487639874))
209
+ # b = GMP.Z((_b=23456))
210
+ # c = GMP.Z((_c=803947509837450987038475))
211
+ # r = a.powmod(b, c)
212
+ # r1 = (_a ** _b) % _c
213
+ # r.should == r1
214
+ # end
215
+ #
216
+ # it 'should properly pad' do
217
+ # k = (bitlength(@n)+7)/8
218
+ # Ttcrypt::RsaKey.set_debug_oaep_seed @seed
219
+ # p k
220
+ # res = Ttcrypt::RsaKey.eme_oaep_encode(long_to_bytes(@message), k-1)
221
+ # bytes_to_long(res).should == @em
222
+ # end
223
+ #
224
+ # it 'should properly depad' do
225
+ # src = Ttcrypt::RsaKey.eme_oaep_decode long_to_bytes(@em)
226
+ # bytes_to_long(src).should == @message
227
+ # end
228
+ #
229
+ # it 'should properly public encrypt' do
230
+ # em = test_key.public_encrypt long_to_bytes(@message)
231
+ # bytes_to_long(em).should == @encrypted_m
232
+ # end
233
+ #
234
+ # it 'should properly private decrypt' do
235
+ # m = test_key(restrict: true).private_decrypt long_to_bytes(@encrypted_m)
236
+ # bytes_to_long(m).should == @message
237
+ # m = test_key.private_decrypt long_to_bytes(@encrypted_m)
238
+ # bytes_to_long(m).should == @message
239
+ #
240
+ # # a = 123101010122
241
+ # # b = 778901
242
+ # # puts "Inverse #{a}, #{b}-> #{inverse(a,b)}"
243
+ #
244
+ # puts "Sha1 empty "+Digest::SHA1.digest('').to_hex
245
+ # puts "Sha1 sergeych forever "+Digest::SHA1.digest('sergeych forever').to_hex
246
+ # end
247
+ #
248
+ # it 'should properly private encrypt and public decrypt'
249
+ #
250
+ # it 'should generate keys'
251
+ #
252
+ # it 'should construct crypstie keys'
253
+ # it 'should serialize crypstie keys'
254
+ #
255
+ # def h2s hex_string
256
+ # hex_string.gsub(/\s+/, '').to_i(16)
257
+ # end
258
+ #
259
+ # def test_key restrict: false
260
+ # Ttcrypt::RsaKey.set_debug_oaep_seed @seed
261
+ # if restrict
262
+ # Ttcrypt::RsaKey.new n: @n, e: @e, d: inverse(@e, lcm(@p - 1, @q - 1))
263
+ # else
264
+ # Ttcrypt::RsaKey.new n: @n, e: @e, p: @p, q: @q
265
+ # end
266
+ # end
267
+ #
268
+ # def init_test_vectors
269
+ # @n = h2s <<-End
270
+ # bb f8 2f 09 06 82 ce 9c 23 38 ac 2b 9d a8 71 f7 36 8d 07 ee d4 10 43 a4
271
+ # 40 d6 b6 f0 74 54 f5 1f b8 df ba af 03 5c 02 ab 61 ea 48 ce eb 6f cd 48
272
+ # 76 ed 52 0d 60 e1 ec 46 19 71 9d 8a 5b 8b 80 7f af b8 e0 a3 df c7 37 72
273
+ # 3e e6 b4 b7 d9 3a 25 84 ee 6a 64 9d 06 09 53 74 88 34 b2 45 45 98 39 4e
274
+ # e0 aa b1 2d 7b 61 a5 1f 52 7a 9a 41 f6 c1 68 7f e2 53 72 98 ca 2a 8f 59
275
+ # 46 f8 e5 fd 09 1d bd cb
276
+ # End
277
+ #
278
+ # @e = 0x11
279
+ #
280
+ # @p = h2s <<-End
281
+ # ee cf ae 81 b1 b9 b3 c9 08 81 0b 10 a1 b5 60 01 99 eb 9f 44 ae f4 fd a4
282
+ # 93 b8 1a 9e 3d 84 f6 32 12 4e f0 23 6e 5d 1e 3b 7e 28 fa e7 aa 04 0a 2d
283
+ # 5b 25 21 76 45 9d 1f 39 75 41 ba 2a 58 fb 65 99
284
+ # End
285
+ #
286
+ # @q = h2s <<-End
287
+ # c9 7f b1 f0 27 f4 53 f6 34 12 33 ea aa d1 d9 35 3f 6c 42 d0 88 66 b1 d0
288
+ # 5a 0f 20 35 02 8b 9d 86 98 40 b4 16 66 b4 2e 92 ea 0d a3 b4 32 04 b5 cf
289
+ # ce 33 52 52 4d 04 16 a5 a4 41 e7 00 af 46 15 03
290
+ # End
291
+ #
292
+ # @dP = h2s <<-End
293
+ # 54 49 4c a6 3e ba 03 37 e4 e2 40 23 fc d6 9a 5a eb 07 dd dc 01 83 a4 d0
294
+ # ac 9b 54 b0 51 f2 b1 3e d9 49 09 75 ea b7 74 14 ff 59 c1 f7 69 2e 9a 2e
295
+ # 20 2b 38 fc 91 0a 47 41 74 ad c9 3c 1f 67 c9 81
296
+ # End
297
+ #
298
+ # @dQ = h2s <<-End
299
+ # 47 1e 02 90 ff 0a f0 75 03 51 b7 f8 78 86 4c a9 61 ad bd 3a 8a 7e 99 1c
300
+ # 5c 05 56 a9 4c 31 46 a7 f9 80 3f 8f 6f 8a e3 42 e9 31 fd 8a e4 7a 22 0d
301
+ # 1b 99 a4 95 84 98 07 fe 39 f9 24 5a 98 36 da 3d
302
+ # End
303
+ #
304
+ # @qInv = h2s <<-End
305
+ # b0 6c 4f da bb 63 01 19 8d 26 5b db ae 94 23 b3 80 f2 71 f7 34 53 88 50
306
+ # 93 07 7f cd 39 e2 11 9f c9 86 32 15 4f 58 83 b1 67 a9 67 bf 40 2b 4e 9e
307
+ # 2e 0f 96 56 e6 98 ea 36 66 ed fb 25 79 80 39 f7
308
+ # End
309
+ #
310
+ # @message = h2s 'd4 36 e9 95 69 fd 32 a7 c8 a0 5b bc 90 d3 2c 49'
311
+ #
312
+ # @pHash = h2s 'da 39 a3 ee 5e 6b 4b 0d 32 55 bf ef 95 60 18 90 af d8 07 09'
313
+ #
314
+ # @seed = h2s 'aa fd 12 f6 59 ca e6 34 89 b4 79 e5 07 6d de c2 f0 6c b5 8f'
315
+ #
316
+ # @em = h2s <<-End
317
+ # eb 7a 19 ac e9 e3 00 63 50 e3 29 50 4b 45 e2 ca 82 31 0b 26 dc d8 7d 5c 68
318
+ # f1 ee a8 f5 52 67 c3 1b 2e 8b b4 25 1f 84 d7 e0 b2 c0 46 26 f5 af f9 3e dc
319
+ # fb 25 c9 c2 b3 ff 8a e1 0e 83 9a 2d db 4c dc fe 4f f4 77 28 b4 a1 b7 c1 36
320
+ # 2b aa d2 9a b4 8d 28 69 d5 02 41 21 43 58 11 59 1b e3 92 f9 82 fb 3e 87 d0
321
+ # 95 ae b4 04 48 db 97 2f 3a c1 4f 7b c2 75 19 52 81 ce 32 d2 f1 b7 6d 4d 35
322
+ # 3e 2d
323
+ # End
324
+ #
325
+ # @encrypted_m = h2s <<-End
326
+ # 12 53 e0 4d c0 a5 39 7b b4 4a 7a b8 7e 9b f2 a0 39 a3 3d 1e 99 6f c8 2a 94
327
+ # cc d3 00 74 c9 5d f7 63 72 20 17 06 9e 52 68 da 5d 1c 0b 4f 87 2c f6 53 c1
328
+ # 1d f8 23 14 a6 79 68 df ea e2 8d ef 04 bb 6d 84 b1 c3 1d 65 4a 19 70 e5 78
329
+ # 3b d6 eb 96 a0 24 c2 ca 2f 4a 90 fe 9f 2e f5 c9 c1 40 e5 bb 48 da 95 36 ad
330
+ # 87 00 c8 4f c9 13 0a de a7 4e 55 8d 51 a7 4d df 85 d8 b5 0d e9 68 38 d6 06
331
+ # 3e 09 55
332
+ # End
333
+ # end
334
+ #
330
335
  end
331
336
  #
332
337
  # class String