secure_string 1.3.0 → 1.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -234,6 +234,9 @@ or directly via e-mail at:
234
234
  mailto:jeff@paploo.net
235
235
 
236
236
  = Version History
237
+ [1.3.1 - 2011-Jun-15] Added public key extraction from private keys.
238
+ * (FEATURE) Public keys can be extracted from private keys using either the
239
+ separate_keys class method or the extract_public_key instance method.
237
240
  [1.3.0 - 2011-Jun-15] Ruby 1.8 compatibility.
238
241
  * (FEATURE) Emulation urlsafe base64 encodings in ruby 1.8.
239
242
  * (CHANGE) SecureString.new arguments changed to work with ruby 1.8.
@@ -274,7 +277,7 @@ mailto:jeff@paploo.net
274
277
  * See what happens when including SecurizeString into an object that is not a String.
275
278
  What are the expected root methods? +to_s+, <tt>self.new(string)</tt> are two
276
279
  that I know of.
277
-
280
+
278
281
 
279
282
  = License
280
283
 
@@ -32,6 +32,18 @@ module SecurizeString
32
32
  return [private_key_obj, public_key_obj].map {|k| self.new( k.send(formatting_method) )}
33
33
  end
34
34
 
35
+ # A convenience method for extracting the private, public keypair from
36
+ # a private key.
37
+ #
38
+ # Returns the same format as +rsa_keygen+, but takes the private key as
39
+ # a string as a first argument.
40
+ def separate_keys(pvt_key, format = :pem)
41
+ private_key_obj = OpenSSL::PKey::RSA.new(pvt_key.to_s)
42
+ public_key_obj = private_key_obj.public_key
43
+ formatting_method = (format == :der ? :to_der : :to_pem)
44
+ return [private_key_obj, public_key_obj].map {|k| self.new( k.send(formatting_method) )}
45
+ end
46
+
35
47
  end
36
48
 
37
49
  # Adds instance methods for OpenSSL::PKey::RSA support via inclusion of
@@ -94,6 +106,14 @@ module SecurizeString
94
106
  return key.private?
95
107
  end
96
108
 
109
+ # Interpret the contents of hte string asn a RSA private key, and extract
110
+ # the public key from it. If the contents are not a private key, then it
111
+ # will raise an exception.
112
+ def extract_public_key(format = :pem)
113
+ pvt, pub = self.class.separate_keys(self, format)
114
+ return pub
115
+ end
116
+
97
117
  end
98
118
 
99
119
  end
@@ -71,6 +71,31 @@ describe "SecureString" do
71
71
  pub_key.private_rsa_key?.should be_false
72
72
  end
73
73
 
74
+ it 'should be able to separate a private key into a public and private key' do
75
+ [:pem, :der].each do |format|
76
+ pvt_key, pub_key = SecureString.rsa_keygen(2048, format)
77
+
78
+ extracted_pvt_key, extracted_pub_key = SecureString.separate_keys(pvt_key.to_s, format)
79
+
80
+ extracted_pvt_key.should be_kind_of(SecureString)
81
+ extracted_pub_key.should be_kind_of(SecureString)
82
+
83
+ extracted_pvt_key.to_s.should == pvt_key.to_s
84
+ extracted_pub_key.to_s.should == pub_key.to_s
85
+ end
86
+ end
87
+
88
+ it 'should be able to extract the public key from a private key string' do
89
+ [:pem, :der].each do |format|
90
+ pvt_key, pub_key = SecureString.rsa_keygen(2048, format)
91
+
92
+ extracted_pub_key = pvt_key.extract_public_key(format)
93
+
94
+ extracted_pub_key.should be_kind_of(SecureString)
95
+ extracted_pub_key.to_s.should == pub_key.to_s
96
+ end
97
+ end
98
+
74
99
  end
75
100
 
76
101
  describe "Encryption" do
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: secure_string
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.3.0
5
+ version: 1.3.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jeff Reinecke