loyal3-sentry 0.4.4 → 0.5.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.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.4
1
+ 0.5.0
@@ -21,7 +21,41 @@ module Sentry
21
21
  def encrypt(data)
22
22
  raise NoPublicKeyError unless public?
23
23
  rsa = public_rsa
24
- rsa.public_encrypt(data)
24
+ return rsa.public_encrypt(data)
25
+ end
26
+
27
+ def decrypt_large_from_base64(data, key=nil)
28
+ padding_length = data[0]
29
+ chunk_length = public_rsa.max_encryptable_length + 11 # 11 is magic padding for RSA encoding
30
+ data = Base64.decode64(data[1,data.length])
31
+ return (0...data.length).step(chunk_length).inject("") { |accum, idx| accum + decrypt_with_padding(data.slice(idx, chunk_length), padding_length, key)}
32
+ end
33
+
34
+ def chunk_size(padding_length)
35
+ return public_rsa.max_encryptable_length - padding_length
36
+ end
37
+
38
+ def encrypt_large_to_base64(data)
39
+ padding_length = 8
40
+ chunk_length = chunk_size(padding_length)
41
+ return padding_length.chr + Base64.encode64( (0...data.length).step(chunk_length).inject("") {|accum, idx| accum + encrypt_with_padding( data.slice(idx, chunk_length), padding_length)} )
42
+ end
43
+
44
+ def decrypt_with_padding(data, padding_length, key=nil)
45
+ decrypted = decrypt(data, key)
46
+ return decrypted[0, decrypted.length - padding_length]
47
+ end
48
+
49
+ def encrypt_with_padding(data, padding_length)
50
+ encrypt(data + rand_string(padding_length))
51
+ end
52
+
53
+ @@CHARS = ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a
54
+
55
+ def rand_string(length=8)
56
+ s=''
57
+ length.times{ s << @@CHARS[rand(@@CHARS.length)] }
58
+ s
25
59
  end
26
60
 
27
61
  def encrypt_to_base64(data)
@@ -31,7 +65,7 @@ module Sentry
31
65
  def decrypt(data, key = nil)
32
66
  raise NoPrivateKeyError unless private?
33
67
  rsa = private_rsa(key)
34
- rsa.private_decrypt(data)
68
+ return rsa.private_decrypt(data)
35
69
  end
36
70
 
37
71
  def decrypt_from_base64(data, key = nil)
data/lib/sentry.rb CHANGED
@@ -45,3 +45,27 @@ begin
45
45
  rescue NameError
46
46
  nil
47
47
  end
48
+
49
+ class OpenSSL::PKey::RSA
50
+ def max_encryptable_length
51
+ @max_encryption_length ||= calc_max_encrypted_length
52
+ end
53
+
54
+ private
55
+
56
+ def calc_max_encrypted_length
57
+ upper_bound = 4*1024
58
+ test_length = upper_bound / 2
59
+ while test_length != (upper_bound - 1)
60
+ probe = "a" * test_length
61
+ begin
62
+ self.public_encrypt(probe)
63
+ test_length = test_length + ((upper_bound - test_length) / 2)
64
+ rescue Exception => e
65
+ upper_bound = test_length
66
+ test_length = test_length / 2
67
+ end
68
+ end
69
+ return test_length
70
+ end
71
+ end
data/sentry.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{sentry}
8
- s.version = "0.4.4"
8
+ s.version = "0.5.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["John Pelly", "David Stevenson"]
@@ -43,6 +43,7 @@ Gem::Specification.new do |s|
43
43
  "test/keys/encrypted_public",
44
44
  "test/keys/private",
45
45
  "test/keys/public",
46
+ "test/rsa_key_test.rb",
46
47
  "test/schema.rb",
47
48
  "test/sha_sentry_test.rb",
48
49
  "test/symmetric_sentry_callback_test.rb",
@@ -59,6 +60,7 @@ Gem::Specification.new do |s|
59
60
  "test/asymmetric_sentry_callback_test.rb",
60
61
  "test/asymmetric_sentry_test.rb",
61
62
  "test/fixtures/user.rb",
63
+ "test/rsa_key_test.rb",
62
64
  "test/schema.rb",
63
65
  "test/sha_sentry_test.rb",
64
66
  "test/symmetric_sentry_callback_test.rb",
@@ -9,19 +9,19 @@ class AsymmetricSentryTest < Test::Unit::TestCase
9
9
  @encrypted_public_key_file = File.dirname(__FILE__) + '/keys/encrypted_public'
10
10
  @encrypted_private_key_file = File.dirname(__FILE__) + '/keys/encrypted_private'
11
11
  @sentry = Sentry::AsymmetricSentry.new
12
-
12
+
13
13
  @orig = 'sentry'
14
14
  @data = "vYfMxtVB8ezXmQKSNqTC9sPgi8TbsYRxWd7DVbpprzyuEdZ7gftJ/0IXsbXm\nXCU08bTAl0uEFm7dau+eJMXEJg==\n"
15
15
  @encrypted_data = "q2obYAITmK93ylzVS01mJx1jSlnmylMX15nFpb4uKesVgnqvtzBRHZ/SK+Nm\nEzceIoAcJc3DHosVa4VUE/aK/A==\n"
16
16
  Sentry::AsymmetricSentry.default_public_key_file = nil
17
17
  Sentry::AsymmetricSentry.default_private_key_file = nil
18
18
  end
19
-
19
+
20
20
  def test_should_decrypt_files
21
21
  set_key_files @public_key_file, @private_key_file
22
22
  assert_equal @orig, @sentry.decrypt_from_base64(@data)
23
23
  end
24
-
24
+
25
25
  def test_should_decrypt_files_with_encrypted_key
26
26
  set_key_files @encrypted_public_key_file, @encrypted_private_key_file
27
27
  assert_equal @orig, @sentry.decrypt_from_base64(@encrypted_data, @key)
@@ -32,7 +32,7 @@ class AsymmetricSentryTest < Test::Unit::TestCase
32
32
  assert !@sentry.private?
33
33
  set_key_files @public_key_file, @private_key_file
34
34
  end
35
-
35
+
36
36
  def test_should_read_encrypted_key_files
37
37
  assert !@sentry.public?
38
38
  assert !@sentry.private?
@@ -43,17 +43,32 @@ class AsymmetricSentryTest < Test::Unit::TestCase
43
43
  set_default_key_files @public_key_file, @private_key_file
44
44
  assert_equal @orig, @sentry.decrypt_from_base64(@data)
45
45
  end
46
-
46
+
47
47
  def test_should_decrypt_files_with_default_encrypted_key
48
48
  set_default_key_files @encrypted_public_key_file, @encrypted_private_key_file
49
49
  assert_equal @orig, @sentry.decrypt_from_base64(@encrypted_data, @key)
50
50
  end
51
51
 
52
+ def test_should_decrypt_block_by_block_for_large_data
53
+ set_default_key_files @encrypted_public_key_file, @encrypted_private_key_file
54
+ large_data = "asdf" * 2048
55
+ encrypted = @sentry.encrypt_large_to_base64(large_data)
56
+ assert_not_equal large_data, encrypted
57
+ assert_equal large_data, @sentry.decrypt_large_from_base64(encrypted, @key)
58
+ end
59
+
52
60
  def test_should_decrypt_files_with_default_key_using_class_method
53
61
  set_default_key_files @public_key_file, @private_key_file
54
62
  assert_equal @orig, Sentry::AsymmetricSentry.decrypt_from_base64(@data)
55
63
  end
56
-
64
+
65
+ def test_foo
66
+ puts "^^^^^^^"
67
+ puts `ruby -v`
68
+ puts (1...20).step(3).inspect
69
+
70
+ end
71
+
57
72
  def test_should_decrypt_files_with_default_encrypted_key_using_class_method
58
73
  set_default_key_files @encrypted_public_key_file, @encrypted_private_key_file
59
74
  assert_equal @orig, Sentry::AsymmetricSentry.decrypt_from_base64(@encrypted_data, @key)
@@ -64,25 +79,26 @@ class AsymmetricSentryTest < Test::Unit::TestCase
64
79
  assert !@sentry.private?
65
80
  set_default_key_files @public_key_file, @private_key_file
66
81
  end
67
-
82
+
68
83
  def test_should_read_encrypted_key_files_with_default_key
69
84
  assert !@sentry.public?
70
85
  assert !@sentry.private?
71
86
  set_default_key_files @encrypted_public_key_file, @encrypted_private_key_file
72
87
  end
73
88
 
74
- private
89
+ private
90
+
75
91
  def set_key_files(public_key, private_key)
76
92
  @sentry.public_key_file = public_key
77
93
  @sentry.private_key_file = private_key
78
94
  assert @sentry.private?
79
95
  assert @sentry.public?
80
96
  end
81
-
97
+
82
98
  def set_default_key_files(public_key, private_key)
83
99
  Sentry::AsymmetricSentry.default_public_key_file = public_key
84
100
  Sentry::AsymmetricSentry.default_private_key_file = private_key
85
101
  assert @sentry.private?
86
102
  assert @sentry.public?
87
103
  end
88
- end
104
+ end
@@ -0,0 +1,11 @@
1
+ require 'abstract_unit'
2
+
3
+ class RsaKeyTest < Test::Unit::TestCase
4
+ def setup
5
+ @public_key = OpenSSL::PKey::RSA.new(File.open(File.dirname(__FILE__) + '/keys/encrypted_public') { |f| f.read })
6
+ end
7
+
8
+ def test_can_find_max_encoded_length_for_key
9
+ assert_equal 53, @public_key.max_encryptable_length
10
+ end
11
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: loyal3-sentry
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.4
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Pelly
@@ -50,6 +50,7 @@ files:
50
50
  - test/keys/encrypted_public
51
51
  - test/keys/private
52
52
  - test/keys/public
53
+ - test/rsa_key_test.rb
53
54
  - test/schema.rb
54
55
  - test/sha_sentry_test.rb
55
56
  - test/symmetric_sentry_callback_test.rb
@@ -87,6 +88,7 @@ test_files:
87
88
  - test/asymmetric_sentry_callback_test.rb
88
89
  - test/asymmetric_sentry_test.rb
89
90
  - test/fixtures/user.rb
91
+ - test/rsa_key_test.rb
90
92
  - test/schema.rb
91
93
  - test/sha_sentry_test.rb
92
94
  - test/symmetric_sentry_callback_test.rb