Skip to content

Commit

Permalink
Hex: Added escaped hex string support.
Browse files Browse the repository at this point in the history
  • Loading branch information
paploo committed Jul 19, 2011
1 parent beb175b commit 65ecaeb
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 4 deletions.
3 changes: 1 addition & 2 deletions README.rdoc
Expand Up @@ -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.
Expand Down Expand Up @@ -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+, <tt>self.new(string)</tt> 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

Expand Down
6 changes: 6 additions & 0 deletions lib/secure_string.rb
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions lib/securize_string/binary_string_data_methods.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion secure_string.gemspec
Expand Up @@ -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'

Expand Down
8 changes: 8 additions & 0 deletions spec/binary_string_data_methods_spec.rb
Expand Up @@ -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
26 changes: 25 additions & 1 deletion spec/secure_string_spec.rb
Expand Up @@ -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
Expand All @@ -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

Expand Down

0 comments on commit 65ecaeb

Please sign in to comment.