secure_string 1.1.2 → 1.2.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/README.rdoc CHANGED
@@ -79,7 +79,7 @@ Additionally, you can get at the byte data in various ways:
79
79
 
80
80
  ss.to_hex --> "48656c6c6f20576f726c6421"
81
81
  ss.to_i --> 22405534230753928650781647905
82
- ss.to_base64 --> "SGVsbG8gV29ybGQh"
82
+ ss.to_base64 --> "SGVsbG8gV29ybGQh\n"
83
83
 
84
84
  One can initialize a SecureString from any of these types like so:
85
85
 
@@ -99,7 +99,7 @@ All of these create equal-valued strings to <tt>"HelloWorld!"</tt>.
99
99
 
100
100
  The SecureString::Base64Methods module adds +to_base64+, which we've seen:
101
101
 
102
- SecureString.new("Hello World!").to_base64 --> "SGVsbG8gV29ybGQh"
102
+ SecureString.new("Hello World!").to_base64 --> "SGVsbG8gV29ybGQh\n"
103
103
 
104
104
  It also adds +from_base64+, which can decode a Base64 encoded string. The
105
105
  following example shows the various ways of decoding Bas64 data:
@@ -234,7 +234,12 @@ or directly via e-mail at:
234
234
  mailto:jeff@paploo.net
235
235
 
236
236
  = Version History
237
- [1.1.2 - 2010-Nov-08] Minor gemspec file change for compatability with bundler.
237
+ [1.2.0 - 2010-May-17] Re-wrote Base64 module to address problems with RFC 2045 vs. RFC 4648 compatibility.
238
+ * Added testing against source strings that generate incompatible encodings between RFC 2045 and RFC 4648.
239
+ * (FEATURE) added option to strip newlines from the returned base64 string.
240
+ * (CHANGE) to_base64 defaults to the standard Base64 instead of websafe Base64.
241
+ * (FIX) added option to decode as websafe Base64.
242
+ [1.1.2 - 2010-Nov-08] Minor gemspec file change for compatibility with bundler.
238
243
  [1.1.1 - 2010-Nov-05] Backed down requirements to ruby 1.9.x; Bugfixes and minor changes.
239
244
  * Tested in 1.9.1 and 1.9.2.
240
245
  * Added some documentation and tests on String encodings.
@@ -13,17 +13,33 @@ module SecurizeString
13
13
  # SecurizeString::Base64Methods to a class.
14
14
  module InstanceMethods
15
15
 
16
- # Encodes to Base64. By default, the output is made URL safe, which means all
17
- # newlines are stripped out. If you want standard formatted Base64 with
18
- # newlines, then call this method with url_safe as false.
19
- def to_base64(url_safe = true)
20
- encoded_data = (url_safe ? Base64.urlsafe_encode64(self) : Base64.encode64(self))
21
- return self.class.new( encoded_data )
16
+ # Encodes to Base64.
17
+ #
18
+ # By deault, this is the normal RFC 2045 Base64 encoding.
19
+ #
20
+ # If the <tt>url_safe</tt> option is set to true, then the RFC 4648
21
+ # encoding is used, which uses a slightly different encoding mechanism
22
+ # which is sometimes compatible, but often incompatible with RFC 2045.
23
+ #
24
+ # If the <tt>nobreak</tt> option is set to true, all line feeds are
25
+ # removed from the input.
26
+ def to_base64(opts={})
27
+ raise ArgumentError, "__method__ expects an argument hash but got #{opts.class.name}" unless opts.kind_of?(Hash)
28
+ data = (opts[:url_safe] ? Base64.urlsafe_encode64(self) : Base64.encode64(self))
29
+ data.delete!("\n\r") if opts[:no_break] # Delete on \n and \r is about 3x faster than gsub on /\s+/.
30
+ return data
22
31
  end
23
32
 
24
- # Decode self as a Base64 data string and return the result.
25
- def from_base64
26
- return self.class.new( Base64.decode64(self) )
33
+ # Decodes from base64.
34
+ #
35
+ # By default, this decodes the normal RFC 2045 Base64.
36
+ #
37
+ # If the <tt>url_safe</tt> option is set to true, then it decodes the
38
+ # RFC 4648 encoding, which uses a slightly different encoding mechanism
39
+ # which is sometimes compatible, but often incompatible with RFC 2045.
40
+ def from_base64(opts={})
41
+ raise ArgumentError, "__method__ expects an argument hash but got #{opts.class.name}" unless opts.kind_of?(Hash)
42
+ return (opts[:url_safe] ? Base64.urlsafe_decode64(self) : Base64.decode64(self))
27
43
  end
28
44
 
29
45
  end
@@ -11,27 +11,30 @@ describe "SecureString" do
11
11
  it 'should convert self to Base64; not URL safe' do
12
12
  @messages.each do |message|
13
13
  ss = SecureString.new(message[:string])
14
- # First make sure that line feeds are being put in somewhere.
15
- ss.to_base64(false).should include("\n")
16
- # Now, compare the data itself (no line-feeds).
17
- ss.to_base64(false).delete("\n").should == message[:base64].delete("\n")
14
+ # Compare the data with the base ruby methods.
15
+ ss.to_base64.should == Base64.encode64(message[:string])
18
16
  end
19
17
  end
20
18
 
21
19
  it 'should convert self to Base64; URL safe' do
22
20
  @messages.each do |message|
23
21
  ss = SecureString.new(message[:string])
24
- # First make sure that there are no line feeds.
25
- ss.to_base64(true).should_not include("\n")
26
- # Now compare the result with the line-feed less expected value.
27
- ss.to_base64(true).should == message[:base64].delete("\n")
22
+ # Compare the result with the base ruby methods.
23
+ ss.to_base64(:url_safe => true).should == Base64.urlsafe_encode64(message[:string])
28
24
  end
29
25
  end
30
26
 
31
- it 'should default to URL safe' do
27
+ it 'should convert base64 with no line breaks' do
32
28
  @messages.each do |message|
33
29
  ss = SecureString.new(message[:string])
34
- ss.to_base64.should == ss.to_base64(true)
30
+ ss.to_base64(:no_break => true).should == message[:base64].delete("\n")
31
+ end
32
+ end
33
+
34
+ it 'should default to NOT URL safe' do
35
+ @messages.each do |message|
36
+ ss = SecureString.new(message[:string])
37
+ ss.to_base64.should == ss.to_base64(:url_safe => false)
35
38
  end
36
39
  end
37
40
 
@@ -42,6 +45,13 @@ describe "SecureString" do
42
45
  end
43
46
  end
44
47
 
48
+ it 'should gracefully error if you provide something other than a hash to options.' do
49
+ @messages.each do |message|
50
+ ss = SecureString.new(message[:string])
51
+ (lambda {ss.to_base64(true)}).should raise_error(ArgumentError)
52
+ (lambda {ss.from_base64(true)}).should raise_error(ArgumentError)
53
+ end
54
+ end
45
55
  end
46
56
 
47
57
  end
data/spec/example_spec.rb CHANGED
@@ -10,7 +10,7 @@ describe "Examples" do
10
10
 
11
11
  ss.to_hex.should == "48656c6c6f20576f726c6421"
12
12
  ss.to_i.should == 22405534230753928650781647905
13
- ss.to_base64.should == "SGVsbG8gV29ybGQh"
13
+ ss.to_base64.should == "SGVsbG8gV29ybGQh\n"
14
14
 
15
15
  ss1 = SecureString.new(:data, "Hello World!")
16
16
  ss2 = SecureString.new(:hex, "48656c6c6f20576f726c6421")
@@ -24,7 +24,7 @@ describe "Examples" do
24
24
  end
25
25
 
26
26
  it 'should perform the base64 example' do
27
- SecureString.new("Hello World!").to_base64.should == "SGVsbG8gV29ybGQh"
27
+ SecureString.new("Hello World!").to_base64.should == "SGVsbG8gV29ybGQh\n"
28
28
 
29
29
  (SecureString.new(:base64, "SGVsbG8gV29ybGQh") == "Hello World!" ).should be_true
30
30
  (SecureString.new("SGVsbG8gV29ybGQh") == "Hello World!" ).should be_false
data/spec/spec_helper.rb CHANGED
@@ -8,7 +8,7 @@ MESSAGES = [
8
8
  :string => "Hello",
9
9
  :hex => "48656c6c6f",
10
10
  :int => 310939249775,
11
- :base64 => "SGVsbG8="
11
+ :base64 => "SGVsbG8=" # Leave off the \n on purpose so that we can make sure it handles that case appropriately when instantiating using base64.
12
12
  },
13
13
 
14
14
  {
@@ -16,5 +16,13 @@ MESSAGES = [
16
16
  :hex => "5468697320697320612074657374206f662074686520656d657267656e63792062726f6164636173742073797374656d3b2074686973206973206f6e6c79206120746573742e",
17
17
  :int => 1244344095146357680190496293767338268850834164562379171846588371816488740307922111470765515885864931093899586331709567338989540039042962957732585272476408412061178229806,
18
18
  :base64 => "VGhpcyBpcyBhIHRlc3Qgb2YgdGhlIGVtZXJnZW5jeSBicm9hZGNhc3Qgc3lz\ndGVtOyB0aGlzIGlzIG9ubHkgYSB0ZXN0Lg==\n"
19
+ },
20
+
21
+ # This one tests a special case where url safe encodes differently than the standard.
22
+ {
23
+ :string => "HI?",
24
+ :hex => "48493f",
25
+ :int => 4737343,
26
+ :base64 => "SEk/\n"
19
27
  }
20
28
  ].freeze
metadata CHANGED
@@ -1,12 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: secure_string
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 1
7
- - 1
8
- - 2
9
- version: 1.1.2
4
+ prerelease:
5
+ version: 1.2.0
10
6
  platform: ruby
11
7
  authors:
12
8
  - Jeff Reinecke
@@ -14,7 +10,7 @@ autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
12
 
17
- date: 2010-11-08 00:00:00 -08:00
13
+ date: 2011-05-17 00:00:00 -07:00
18
14
  default_executable:
19
15
  dependencies: []
20
16
 
@@ -61,23 +57,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
61
57
  requirements:
62
58
  - - ">="
63
59
  - !ruby/object:Gem::Version
64
- segments:
65
- - 1
66
- - 9
67
- - 0
68
60
  version: 1.9.0
69
61
  required_rubygems_version: !ruby/object:Gem::Requirement
70
62
  none: false
71
63
  requirements:
72
64
  - - ">="
73
65
  - !ruby/object:Gem::Version
74
- segments:
75
- - 0
76
66
  version: "0"
77
67
  requirements: []
78
68
 
79
69
  rubyforge_project:
80
- rubygems_version: 1.3.7
70
+ rubygems_version: 1.5.0
81
71
  signing_key:
82
72
  specification_version: 3
83
73
  summary: A String subclass for simple handling of binary data and encryption.