secure_string 1.0.0 → 1.1.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 +39 -3
- data/lib/secure_string.rb +4 -43
- data/lib/securize_string.rb +21 -0
- data/lib/{secure_string → securize_string}/base64_methods.rb +2 -2
- data/lib/securize_string/binary_string_data_methods.rb +68 -0
- data/lib/{secure_string → securize_string}/cipher_methods.rb +3 -3
- data/lib/{secure_string → securize_string}/digest_methods.rb +2 -2
- data/lib/{secure_string → securize_string}/rsa_methods.rb +3 -3
- data/spec/binary_string_data_methods_spec.rb +44 -0
- data/spec/secure_string_spec.rb +0 -31
- metadata +9 -6
data/README.rdoc
CHANGED
@@ -21,6 +21,43 @@ measure of a byte string's length, as depending on the encoding, it may count
|
|
21
21
|
multibyte characters as a single element. To ensure that you get the byte
|
22
22
|
length, use the standard string method +bytesize+.
|
23
23
|
|
24
|
+
= Installation & Configuration
|
25
|
+
|
26
|
+
== Installation
|
27
|
+
|
28
|
+
Install from gem, and add the following to your script:
|
29
|
+
|
30
|
+
require 'secure_string'
|
31
|
+
|
32
|
+
By default, this creates the SecureString class, which is completely configured
|
33
|
+
and ready to go like so:
|
34
|
+
|
35
|
+
SecureString.new("foo")
|
36
|
+
|
37
|
+
== Optional Configuration
|
38
|
+
|
39
|
+
Some people like to monkey patch String to translate to a
|
40
|
+
SecureString like so:
|
41
|
+
|
42
|
+
# This is an optional configuration:
|
43
|
+
class String
|
44
|
+
def to_ss
|
45
|
+
return SecureString.new(self)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
Alternatively, if you would like to add the SecureString methods directly onto
|
50
|
+
all Strings, simply add the following code to your program:
|
51
|
+
|
52
|
+
# This is an optional configuration:
|
53
|
+
class String
|
54
|
+
include SecurizeString
|
55
|
+
end
|
56
|
+
|
57
|
+
Unless you already have code that modifies String in a conflicting way--or code
|
58
|
+
that depends on the value of +inspect+, this should not change the behavior of
|
59
|
+
your existing code.
|
60
|
+
|
24
61
|
= Examples
|
25
62
|
|
26
63
|
== Basic Usage
|
@@ -142,6 +179,8 @@ or directly via e-mail at:
|
|
142
179
|
mailto:jeff@paploo.net
|
143
180
|
|
144
181
|
= Version History
|
182
|
+
[1.1.0 - 2030-Nov-04] Extracted methods into a module that can be easily included
|
183
|
+
on any String class.
|
145
184
|
[1.0.0 - 2010-Nov-04] Added Tests, Examples, and Bugfixes
|
146
185
|
* Added a full suite of spec tests.
|
147
186
|
* (FEATURE) Can get a list of supported ciphers.
|
@@ -154,9 +193,6 @@ mailto:jeff@paploo.net
|
|
154
193
|
|
155
194
|
= TODO List
|
156
195
|
|
157
|
-
* Add complete spec tests.
|
158
|
-
* Add examples.
|
159
|
-
* Pull out all methods into modules so that it can be an extension of all Strings.
|
160
196
|
* Add a +to_ss+ or +to_secure+ method to String for easy conversion.
|
161
197
|
* to_digest should be able to take a string that is the algorithm name.
|
162
198
|
* Explore how encodings affect the data. What about when finding length? What
|
data/lib/secure_string.rb
CHANGED
@@ -1,19 +1,11 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require_relative 'secure_string/digest_methods'
|
4
|
-
require_relative 'secure_string/base64_methods'
|
5
|
-
require_relative 'secure_string/cipher_methods'
|
6
|
-
require_relative 'secure_string/rsa_methods'
|
1
|
+
require_relative 'securize_string'
|
7
2
|
|
8
3
|
# SecureString is a String subclass whose emphasis is on byte data rather than
|
9
4
|
# human readable strings. class gives a number of conveniences, such
|
10
5
|
# as easier viewing of the byte data as hex, digest methods, and encryption
|
11
6
|
# and decryption methods.
|
12
7
|
class SecureString < String
|
13
|
-
include
|
14
|
-
include DigestMethods
|
15
|
-
include RSAMethods
|
16
|
-
include CipherMethods
|
8
|
+
include SecurizeString
|
17
9
|
|
18
10
|
# Creates the string from one many kinds of values:
|
19
11
|
# [:data] (default) The passed string value is directly used.
|
@@ -21,39 +13,8 @@ class SecureString < String
|
|
21
13
|
# [:int] Initialize using the numeric value of the hexidecimal string.
|
22
14
|
# [:base64] Initialize using the given base64 encoded data.
|
23
15
|
def initialize(mode = :data, value)
|
24
|
-
|
25
|
-
|
26
|
-
hex_string = value.to_s
|
27
|
-
data = [hex_string].pack('H' + hex_string.length.to_s)
|
28
|
-
when :data
|
29
|
-
data = value.to_s
|
30
|
-
when :int
|
31
|
-
data = self.send(__method__, :hex, value.to_i.to_s(16))
|
32
|
-
when :base64
|
33
|
-
data = Base64.decode64(value.to_s)
|
34
|
-
end
|
35
|
-
|
36
|
-
self.replace(data)
|
37
|
-
end
|
38
|
-
|
39
|
-
# Override the default String inspect to return the hexidecimal
|
40
|
-
# representation of the data contained in this string.
|
41
|
-
def inspect
|
42
|
-
return "<#{to_hex}>"
|
43
|
-
end
|
44
|
-
|
45
|
-
# Returns the hexidecimal string representation of the data.
|
46
|
-
def to_hex
|
47
|
-
return (self.empty? ? '' : self.unpack('H' + (self.length*2).to_s)[0])
|
48
|
-
end
|
49
|
-
|
50
|
-
# Returns the data converted from hexidecimal into an integer.
|
51
|
-
# This is usually as a BigInt.
|
52
|
-
#
|
53
|
-
# WARNING: If the data string is empty, then this returns -1, as there is no
|
54
|
-
# integer representation of the absence of data.
|
55
|
-
def to_i
|
56
|
-
return (self.empty? ? -1 : to_hex.hex)
|
16
|
+
data_string = self.class.parse_data(mode, value)
|
17
|
+
self.replace( data_string )
|
57
18
|
end
|
58
19
|
|
59
20
|
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require_relative 'securize_string/binary_string_data_methods'
|
2
|
+
require_relative 'securize_string/digest_methods'
|
3
|
+
require_relative 'securize_string/base64_methods'
|
4
|
+
require_relative 'securize_string/cipher_methods'
|
5
|
+
require_relative 'securize_string/rsa_methods'
|
6
|
+
|
7
|
+
module SecurizeString
|
8
|
+
|
9
|
+
def self.included(mod)
|
10
|
+
[
|
11
|
+
BinaryStringDataMethods,
|
12
|
+
Base64Methods,
|
13
|
+
DigestMethods,
|
14
|
+
RSAMethods,
|
15
|
+
CipherMethods
|
16
|
+
].each do |mixin|
|
17
|
+
mod.send(:include, mixin)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'base64'
|
2
2
|
|
3
|
-
|
3
|
+
module SecurizeString
|
4
4
|
# Adds methods for Base64 conversion.
|
5
5
|
# See Base64Methods::InstanceMethods for more details.
|
6
6
|
module Base64Methods
|
@@ -10,7 +10,7 @@ class SecureString < String
|
|
10
10
|
end
|
11
11
|
|
12
12
|
# Adds instance methods for Base64 support via inclusion of
|
13
|
-
#
|
13
|
+
# SecurizeString::Base64Methods to a class.
|
14
14
|
module InstanceMethods
|
15
15
|
|
16
16
|
# Encodes to Base64. By default, the output is made URL safe, which means all
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'base64'
|
2
|
+
|
3
|
+
module SecurizeString
|
4
|
+
# Adds the base methods necessary to make String or a String subclass handle
|
5
|
+
# binary data better.
|
6
|
+
# See BinaryStringDataMethods::ClassMethods and BinaryStringDataMethods::InstanceMethods for more deatils.
|
7
|
+
module BinaryStringDataMethods
|
8
|
+
|
9
|
+
def self.included(mod)
|
10
|
+
mod.send(:extend, ClassMethods)
|
11
|
+
mod.send(:include, InstanceMethods)
|
12
|
+
end
|
13
|
+
|
14
|
+
# Adds basic binary data class methods to String or a String subclass, via
|
15
|
+
# an include of SecurizeString::BinaryStringDataMethods
|
16
|
+
module ClassMethods
|
17
|
+
|
18
|
+
# Creates a data string from one many kinds of values:
|
19
|
+
# [:data] (default) The passed string value is directly used.
|
20
|
+
# [:hex] Initialize using a hexidecimal string.
|
21
|
+
# [:int] Initialize using the numeric value of the hexidecimal string.
|
22
|
+
# [:base64] Initialize using the given base64 encoded data.
|
23
|
+
def parse_data(mode = :data, value)
|
24
|
+
case mode
|
25
|
+
when :hex
|
26
|
+
hex_string = value.to_s
|
27
|
+
data_string = [hex_string].pack('H' + hex_string.bytesize.to_s)
|
28
|
+
when :data
|
29
|
+
data_string = value.to_s
|
30
|
+
when :int
|
31
|
+
data_string = self.send(__method__, :hex, value.to_i.to_s(16))
|
32
|
+
when :base64
|
33
|
+
data_string = Base64.decode64(value.to_s)
|
34
|
+
end
|
35
|
+
|
36
|
+
return data_string
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
# Adds basic binary data instance methods to String or a String subclass, via
|
42
|
+
# an include of SecurizeString::BinaryStringDataMethods.
|
43
|
+
module InstanceMethods
|
44
|
+
|
45
|
+
# Override the default inspect to return the hexidecimal
|
46
|
+
# representation of the data contained in this string.
|
47
|
+
def inspect
|
48
|
+
return "<#{to_hex}>"
|
49
|
+
end
|
50
|
+
|
51
|
+
# Returns the hexidecimal string representation of the data.
|
52
|
+
def to_hex
|
53
|
+
return (self.to_s.empty? ? '' : self.to_s.unpack('H' + (self.to_s.bytesize*2).to_s)[0])
|
54
|
+
end
|
55
|
+
|
56
|
+
# Returns the data converted from hexidecimal into an integer.
|
57
|
+
# This is usually as a BigInt.
|
58
|
+
#
|
59
|
+
# WARNING: If the data string is empty, then this returns -1, as there is no
|
60
|
+
# integer representation of the absence of data.
|
61
|
+
def to_i
|
62
|
+
return (self.to_s.empty? ? -1 : to_hex.hex)
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'openssl'
|
2
2
|
|
3
|
-
|
3
|
+
module SecurizeString
|
4
4
|
# Adds methods for OpenSSL::Cipher support including AES encryption.
|
5
5
|
# See CipherMethods::ClassMethods and CipherMethods::InstanceMethods for more details.
|
6
6
|
module CipherMethods
|
@@ -11,7 +11,7 @@ class SecureString < String
|
|
11
11
|
end
|
12
12
|
|
13
13
|
# Adds class methods for OpenSSL::Cipher support, including AES encryption,
|
14
|
-
# via inclusion of
|
14
|
+
# via inclusion of SecurizeString::CipherMethods into a class.
|
15
15
|
module ClassMethods
|
16
16
|
|
17
17
|
# Returns a list of supported ciphers. These can be passed directly into
|
@@ -37,7 +37,7 @@ class SecureString < String
|
|
37
37
|
end
|
38
38
|
|
39
39
|
# Adds instance methods for OpenSSL::Cipher support, including AES encryption,
|
40
|
-
# via inclusion of
|
40
|
+
# via inclusion of SecurizeString::CipherMethods into a class.
|
41
41
|
module InstanceMethods
|
42
42
|
|
43
43
|
# Given an OpenSSL cipher name, a key, and initialization vector,
|
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'openssl'
|
2
2
|
|
3
|
-
|
3
|
+
module SecurizeString
|
4
4
|
# Adds methods for OpenSSL::Digest support.
|
5
5
|
# See DigestMethods::ClassMethods and DigestMethods::InstanceMethods for more details.
|
6
6
|
module DigestMethods
|
@@ -10,7 +10,7 @@ class SecureString < String
|
|
10
10
|
end
|
11
11
|
|
12
12
|
# Adds instance methods for OpenSSL::Digest support via inclusion of
|
13
|
-
#
|
13
|
+
# SecurizeString::DigestMethods to a class.
|
14
14
|
module InstanceMethods
|
15
15
|
|
16
16
|
# Returns the digest of the byte string as a SecureString, using the passed OpenSSL object.
|
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'openssl'
|
2
2
|
|
3
|
-
|
3
|
+
module SecurizeString
|
4
4
|
# Adds methods for OpenSSL::PKey::RSA support.
|
5
5
|
# See RSAMethods::ClassMethods and RSAMethods::InstanceMethods for more details.
|
6
6
|
module RSAMethods
|
@@ -11,7 +11,7 @@ class SecureString < String
|
|
11
11
|
end
|
12
12
|
|
13
13
|
# Adds class methods for OpenSSL::PKey::RSA support via inclusion of
|
14
|
-
#
|
14
|
+
# SecurizeString::RSAMethods to a class.
|
15
15
|
module ClassMethods
|
16
16
|
|
17
17
|
# A convenience method for generating random public/private RSA key pairs.
|
@@ -35,7 +35,7 @@ class SecureString < String
|
|
35
35
|
end
|
36
36
|
|
37
37
|
# Adds instance methods for OpenSSL::PKey::RSA support via inclusion of
|
38
|
-
#
|
38
|
+
# SecurizeString::RSAMethods to a class.
|
39
39
|
module InstanceMethods
|
40
40
|
|
41
41
|
# Given an RSA public key, it RSA encrypts the data string.
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
+
|
3
|
+
describe "SecurityString" do
|
4
|
+
|
5
|
+
before(:all) do
|
6
|
+
@messages = MESSAGES
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "Binary String Data Methods" do
|
10
|
+
|
11
|
+
it 'should be able to convert to a hex string' do
|
12
|
+
@messages.each do |message|
|
13
|
+
ss = SecureString.new(message[:string])
|
14
|
+
ss.to_hex.should == message[:hex]
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should be able to convert to an int value' do
|
19
|
+
@messages.each do |message|
|
20
|
+
ss = SecureString.new(message[:string])
|
21
|
+
ss.to_i.should == message[:int]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should output like a string for to_s' do
|
26
|
+
@messages.each do |message|
|
27
|
+
s = String.new(message[:string])
|
28
|
+
ss = SecureString.new(message[:string])
|
29
|
+
ss.to_s.should == s.to_s
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'should output the hex value with inspect' do
|
34
|
+
@messages.each do |message|
|
35
|
+
s = String.new(message[:string])
|
36
|
+
ss = SecureString.new(message[:string])
|
37
|
+
ss.inspect.should include(ss.to_hex)
|
38
|
+
ss.inspect.should_not include(s.to_s)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
data/spec/secure_string_spec.rb
CHANGED
@@ -56,35 +56,4 @@ describe "SecureString" do
|
|
56
56
|
newline_count.select {|nl_count| nl_count > 1}.should_not be_empty
|
57
57
|
end
|
58
58
|
|
59
|
-
it 'should be able to convert to a hex string' do
|
60
|
-
@messages.each do |message|
|
61
|
-
ss = SecureString.new(message[:string])
|
62
|
-
ss.to_hex.should == message[:hex]
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
it 'should be able to convert to an int value' do
|
67
|
-
@messages.each do |message|
|
68
|
-
ss = SecureString.new(message[:string])
|
69
|
-
ss.to_i.should == message[:int]
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
it 'should output like a string for to_s' do
|
74
|
-
@messages.each do |message|
|
75
|
-
s = String.new(message[:string])
|
76
|
-
ss = SecureString.new(message[:string])
|
77
|
-
ss.to_s.should == s.to_s
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
it 'should output the hex value with inspect' do
|
82
|
-
@messages.each do |message|
|
83
|
-
s = String.new(message[:string])
|
84
|
-
ss = SecureString.new(message[:string])
|
85
|
-
ss.inspect.should include(ss.to_hex)
|
86
|
-
ss.inspect.should_not include(s.to_s)
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
59
|
end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 1
|
7
|
+
- 1
|
7
8
|
- 0
|
8
|
-
|
9
|
-
version: 1.0.0
|
9
|
+
version: 1.1.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Jeff Reinecke
|
@@ -30,12 +30,15 @@ files:
|
|
30
30
|
- README.rdoc
|
31
31
|
- LICENSE.txt
|
32
32
|
- Rakefile
|
33
|
-
- lib/secure_string/base64_methods.rb
|
34
|
-
- lib/secure_string/cipher_methods.rb
|
35
|
-
- lib/secure_string/digest_methods.rb
|
36
|
-
- lib/secure_string/rsa_methods.rb
|
37
33
|
- lib/secure_string.rb
|
34
|
+
- lib/securize_string/base64_methods.rb
|
35
|
+
- lib/securize_string/binary_string_data_methods.rb
|
36
|
+
- lib/securize_string/cipher_methods.rb
|
37
|
+
- lib/securize_string/digest_methods.rb
|
38
|
+
- lib/securize_string/rsa_methods.rb
|
39
|
+
- lib/securize_string.rb
|
38
40
|
- spec/base64_methods_spec.rb
|
41
|
+
- spec/binary_string_data_methods_spec.rb
|
39
42
|
- spec/cipher_methods_spec.rb
|
40
43
|
- spec/digest_methods_spec.rb
|
41
44
|
- spec/rsa_methods_spec.rb
|