diff --git a/README.rdoc b/README.rdoc index da5a361..bef2a08 100644 --- a/README.rdoc +++ b/README.rdoc @@ -236,6 +236,7 @@ mailto:jeff@paploo.net = Version History [1.3.3 - 2011-Jul-19] Added new functionality * (FEATURE) Added PKCS5 v2 keygen from a passphrase support. + * (FEATURE) Addition of escaped hex string literal support for pasting into code. * (FIX) Hex conversion is more consistently faster now." [1.3.2 - 2011-Jun-15] Changed minimum requirements from 1.8.6 to 1.8.7. * (CHANGE) Minimum requirements are 1.8.7 due to too many bugs in 1.8.6. @@ -283,8 +284,6 @@ mailto:jeff@paploo.net * See what happens when including SecurizeString into an object that is not a String. What are the expected root methods? +to_s+, self.new(string) are two that I know of. -* Create a method that returns a "\x48\x69" style string, and one that can - parse it back into a string. = License diff --git a/lib/secure_string.rb b/lib/secure_string.rb index 49db984..9c5c17c 100644 --- a/lib/secure_string.rb +++ b/lib/secure_string.rb @@ -30,6 +30,12 @@ def to_hex return data_to_hex end + # Add a method to convert the internal binary dat into an escaped hex string + # which is suitable for pasting into Ruby and Javascript source files. + def to_escaped_hex + return data_to_escaped_hex + end + # Override the default inspect to return the hexidecimal # representation of the data contained in this string. def inspect diff --git a/lib/securize_string/binary_string_data_methods.rb b/lib/securize_string/binary_string_data_methods.rb index 98a1c96..613ee0f 100644 --- a/lib/securize_string/binary_string_data_methods.rb +++ b/lib/securize_string/binary_string_data_methods.rb @@ -56,6 +56,26 @@ def data_to_i return (self.to_s.empty? ? 0 : self.data_to_hex.hex) end + # Returns an escaped hex string representation of the data. + # + # This hex string is compatible with Ruby and Javascript. + def data_to_escaped_hex + # First we convert the string into a packed hex string. + hex_string = self.unpack('H*')[0] + + # Now we grab two elements at a time, prefix it, and add it to the buffer. + ptr = 0 + len = self.bytesize + outbuf = "" + while(ptr<(len*2)) + outbuf << '\x' + hex_string[ptr,2] + ptr+=2 + end + + # Now we return the buffer + return outbuf + end + end end diff --git a/secure_string.gemspec b/secure_string.gemspec index b35a0fa..371304e 100644 --- a/secure_string.gemspec +++ b/secure_string.gemspec @@ -7,7 +7,7 @@ end Gem::Specification.new do |s| s.name = 'secure_string' - s.version = '1.3.2' + s.version = '1.3.3' s.required_ruby_version = '>= 1.8.7' diff --git a/spec/binary_string_data_methods_spec.rb b/spec/binary_string_data_methods_spec.rb index fac6734..1ac4e76 100644 --- a/spec/binary_string_data_methods_spec.rb +++ b/spec/binary_string_data_methods_spec.rb @@ -47,6 +47,14 @@ ss.data_to_i.should == 0 end + it 'should be able to convert to an escaped hex string' do + @messages.each do |message| + ss = SecureString.new(message[:string]) + ss.data_to_escaped_hex.delete('^0-9A-Fa-f').should == message[:hex] + ss.data_to_escaped_hex.should == message[:string].each_byte.map {|b| '\x' + ('%02x' % b)}.join + end + end + end end \ No newline at end of file diff --git a/spec/secure_string_spec.rb b/spec/secure_string_spec.rb index 30fc7e5..d036d35 100644 --- a/spec/secure_string_spec.rb +++ b/spec/secure_string_spec.rb @@ -84,8 +84,20 @@ end end - it 'should parse hex data with spaces' do + it 'should implement to_escaped_hex' do + if( RUBY_VERSION >= '1.9.0' ) + SecureString.instance_methods.should include(:to_escaped_hex) + else + SecureString.instance_methods.should include('to_escaped_hex') + end + @messages.each do |message| + ss = SecureString.new(message[:string]) + ss.to_escaped_hex.should == ss.data_to_escaped_hex + end + end + + it 'should parse hex data with spaces' do data = <<-DATA a766a602 b65cffe7 73bcf258 26b322b3 d01b1a97 2684ef53 3e3b4b7f 53fe3762 24c08e47 e959b2bc 3b519880 b9286568 247d110f 70f5c5e2 b4590ca3 f55f52fe @@ -105,6 +117,18 @@ OpenSSL::Digest::SHA.hexdigest(ss).should == "c9f160777d4086fe8095fba58b7e20c228a4006b" end + it 'should parse hex data with escape characters' do + # Create the string with escape characters in there. + data = '\x48\x65\x6c\x6c\x6f' + + # Make sure Ruby didn't convert them to the actual string. + data.should_not == 'Hello' + + # Now see if SecureString can convert it as hex data. + ss = SecureString.new(data, :type => :hex) + ss.should == 'Hello' + end + if( RUBY_VERSION >= '1.9.0' ) describe 'Encodings' do