dionysus 0.2.0 → 0.2.1

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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.2.1
data/dionysus.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{dionysus}
8
- s.version = "0.2.0"
8
+ s.version = "0.2.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Travis D. Warlick, Jr."]
12
- s.date = %q{2010-03-15}
12
+ s.date = %q{2010-03-21}
13
13
  s.email = %q{warlickt@operissystems.com}
14
14
  s.extra_rdoc_files = [
15
15
  "LICENSE",
@@ -41,11 +41,11 @@ Gem::Specification.new do |s|
41
41
  s.rubygems_version = %q{1.3.6}
42
42
  s.summary = %q{A helpful set of utility classes, generators, and command-line tools.}
43
43
  s.test_files = [
44
- "spec/configuration_spec.rb",
45
- "spec/dionysus_spec.rb",
44
+ "spec/string_spec.rb",
46
45
  "spec/spec_helper.rb",
46
+ "spec/configuration_spec.rb",
47
47
  "spec/string_security_spec.rb",
48
- "spec/string_spec.rb"
48
+ "spec/dionysus_spec.rb"
49
49
  ]
50
50
 
51
51
  if s.respond_to? :specification_version then
@@ -8,76 +8,48 @@ require 'dionysus/string'
8
8
  # Adds String hashing and salting convenience methods.
9
9
  #
10
10
  # require 'dionysus/security/string'
11
+ #
12
+ # The hash methods may be accessed by the <tt>digest</tt> method or by the
13
+ # dynamic convenience methods.
14
+ #
15
+ # Examples:
16
+ #
17
+ # 'my password'.digest(:sha1)
18
+ # 'my password'.sha1
11
19
  class String
12
20
  ##
13
21
  # Generate a random, binary salt with the given length (default 16)
14
22
  def self.salt( length = 16 )
15
- length.times.to_a.collect {|v| rand(255).chr}.join[0...length]
23
+ length.times.to_a.collect {|val| rand(255).chr}.join[0...length]
16
24
  end
17
25
 
18
26
  ##
19
- # Generate the SHA 512 HASh, with the given salt appended
27
+ # Generate the given digest hash.
20
28
  #
21
29
  # Options:
22
30
  # [salt] Salt to append to the string.
23
31
  # Default: ''
24
32
  # [encoding] Encoding for the hash: <tt>:binary</tt>, <tt>:hex</tt>, <tt>:hexidecimal</tt>, <tt>:base64</tt>
25
33
  # Default: <tt>:base64</tt>
26
- def sha512( options = {} )
27
- d = Digest::SHA512.digest(_salted(options[:salt]))
28
- _encode(d, options[:encoding] || :base64)
29
- end
30
-
31
- ##
32
- # Generate the SHA 256 HASh, with the given salt appended
33
- #
34
- # Options:
35
- # [salt] Salt to append to the string.
36
- # Default: ''
37
- # [encoding] Encoding for the hash: <tt>:binary</tt>, <tt>:hex</tt>, <tt>:hexidecimal</tt>, <tt>:base64</tt>
38
- # Default: <tt>:base64</tt>
39
- def sha256( options = {} )
40
- d = Digest::SHA256.digest(_salted(options[:salt]))
41
- _encode(d, options[:encoding] || :base64)
42
- end
43
-
44
- ##
45
- # Generate the SHA 2 HASh, with the given salt appended
46
- #
47
- # Options:
48
- # [salt] Salt to append to the string.
49
- # Default: ''
50
- # [encoding] Encoding for the hash: <tt>:binary</tt>, <tt>:hex</tt>, <tt>:hexidecimal</tt>, <tt>:base64</tt>
51
- # Default: <tt>:base64</tt>
52
- def sha2( options = {} )
53
- d = Digest::SHA2.digest(_salted(options[:salt]))
54
- _encode(d, options[:encoding] || :base64)
55
- end
56
-
57
- ##
58
- # Generate the SHA 1 hash, with the given salt appended
59
- #
60
- # Options:
61
- # [salt] Salt to append to the string.
62
- # Default: ''
63
- # [encoding] Encoding for the hash: <tt>:binary</tt>, <tt>:hex</tt>, <tt>:hexidecimal</tt>, <tt>:base64</tt>
64
- # Default: <tt>:base64</tt>
65
- def sha1( options = {} )
66
- d = Digest::SHA1.digest(_salted(options[:salt]))
67
- _encode(d, options[:encoding] || :base64)
34
+ def digest( klass, options = {} )
35
+ unless klass.is_a?(Class)
36
+ klass = Digest.const_get(klass.to_s.upcase)
37
+ end
38
+
39
+ digest = klass.digest(_salted(options[:salt]))
40
+ _encode(digest, options[:encoding] || :base64)
68
41
  end
69
42
 
70
43
  ##
71
- # Generate the MD5 hash, with the given salt appended.
72
- #
73
- # Options:
74
- # [salt] Salt to append to the string.
75
- # Default: ''
76
- # [encoding] Encoding for the hash: <tt>:binary</tt>, <tt>:hex</tt>, <tt>:hexidecimal</tt>, <tt>:base64</tt>
77
- # Default: <tt>:base64</tt>
78
- def md5( options = {} )
79
- d = Digest::MD5.digest(_salted(options[:salt]))
80
- _encode(d, options[:encoding] || :base64)
44
+ # Call the appropriate digest method.
45
+ def method_missing( method, *args )
46
+ super if block_given?
47
+
48
+ begin
49
+ return self.digest(method, *args)
50
+ rescue NameError
51
+ end
52
+ super
81
53
  end
82
54
 
83
55
  private
@@ -97,7 +69,7 @@ class String
97
69
  when :base64
98
70
  value.encode64s
99
71
  when :hex, :hexidecimal
100
- value.encodeHex
72
+ value.encode_hex
101
73
  else
102
74
  raise ArgumentError, "Invalid encoding: #{encoding}"
103
75
  end
@@ -20,11 +20,11 @@ class String
20
20
 
21
21
  ##
22
22
  # Encode to hexidecimal
23
- def encodeHexidecimal() self.unpack('H*').first; end
24
- alias_method :encodeHex, :encodeHexidecimal
23
+ def encode_hexidecimal() self.unpack('H*').first; end
24
+ alias_method :encode_hex, :encode_hexidecimal
25
25
 
26
26
  ##
27
27
  # Decode from hexidecimal
28
- def decodeHexidecimal() [self].pack('H*'); end
29
- alias_method :decodeHex, :decodeHexidecimal
28
+ def decode_hexidecimal() [self].pack('H*'); end
29
+ alias_method :decode_hex, :decode_hexidecimal
30
30
  end
@@ -13,26 +13,38 @@ describe String do
13
13
 
14
14
  it "should calculate sha512 in base64" do
15
15
  digest = "KHErTuI/5urCF3vi6yTDxvAHBydW1CN3zVfBWDP/ItbmgeVi5IFq47ew0hPHzyK3AuMDh+Z9MBBlf3/mACgVMQ=="
16
+ "abcde\240z405".digest(:sha512).should == digest
16
17
  "abcde\240z405".sha512.should == digest
17
18
  "abcde\240z405".sha512(:encoding => :base64).should == digest
18
19
  end
19
20
 
21
+ it "should calculate sha384 in base64" do
22
+ digest = "PcQPzxc6tAaom+DG31XCi/A/7ZB6N0PgkBK9e1NMOadc7VxYSWoGq03f2+DiflT5"
23
+ "abcde\240z405".digest(:sha384).should == digest
24
+ "abcde\240z405".sha384.should == digest
25
+ "abcde\240z405".sha384(:encoding => :base64).should == digest
26
+ end
27
+
20
28
  it "should calculate sha256/sha2 in base64" do
21
29
  digest = "vGeWpAMcqj2XuE8P4FirA2oUt1S78c6puQk03EOdnQY="
30
+ "abcde\240z405".digest(:sha256).should == digest
22
31
  "abcde\240z405".sha256.should == digest
23
32
  "abcde\240z405".sha256(:encoding => :base64).should == digest
33
+ "abcde\240z405".digest(:sha2).should == digest
24
34
  "abcde\240z405".sha2.should == digest
25
35
  "abcde\240z405".sha2(:encoding => :base64).should == digest
26
36
  end
27
37
 
28
38
  it "should calculate sha1 in base64" do
29
39
  digest = "wxeCFXPVXePFcpwuFDjonyn1G/w="
40
+ "abcde\240z405".digest(:sha1).should == digest
30
41
  "abcde\240z405".sha1.should == digest
31
42
  "abcde\240z405".sha1(:encoding => :base64).should == digest
32
43
  end
33
44
 
34
45
  it "should calculate md5 in base64" do
35
46
  digest = "ThT0y7KbAinjLpuChqsRuQ=="
47
+ "abcde\240z405".digest(:md5).should == digest
36
48
  "abcde\240z405".md5.should == digest
37
49
  "abcde\240z405".md5(:encoding => :base64).should == digest
38
50
  end
@@ -40,36 +52,62 @@ describe String do
40
52
  it "should calculate sha512 with a salt" do
41
53
  salt = 'abcdefg'
42
54
  digest = "oxoD5Mwq6ck4B1P2tfsrCQnNHzBYQRKOFGTj1T0EFPWeNuy1/YPtsKGiz2os9ljt0/OwXEt/l8LsK/ktbwEHbg=="
55
+ "abcde\240z405".digest(:sha512, :salt => salt).should == digest
43
56
  "abcde\240z405".sha512(:salt => salt).should == digest
44
57
  end
45
58
 
59
+ it "should calculate sha384 with a salt" do
60
+ salt = 'abcdefg'
61
+ digest = "XVHvPSZDdUNC8zY0oriwlXfwhMPeJusx59kWkGq3bEqrz1tlbmMJ+UGUOqYKbdYK"
62
+ "abcde\240z405".digest(:sha384, :salt => salt).should == digest
63
+ "abcde\240z405".sha384(:salt => salt).should == digest
64
+ end
65
+
46
66
  it "should calculate sha256/sha2 with a salt" do
47
67
  salt = 'abcdefg'
48
68
  digest = "fWzU2g2GFAS5EAgTS09djo2eDDQdNp1xSTdrRO7m9v4="
69
+ "abcde\240z405".digest(:sha256, :salt => salt).should == digest
49
70
  "abcde\240z405".sha256(:salt => salt).should == digest
71
+ "abcde\240z405".digest(:sha2, :salt => salt).should == digest
50
72
  "abcde\240z405".sha2(:salt => salt).should == digest
51
73
  end
52
74
 
53
75
  it "should calculate sha1 with a salt" do
54
76
  salt = 'abcdefg'
55
77
  digest = "JFdwlGl341qtbqX6mPQbjs8d1/U="
78
+ "abcde\240z405".digest(:sha1, :salt => salt).should == digest
56
79
  "abcde\240z405".sha1(:salt => salt).should == digest
57
80
  end
58
81
 
59
82
  it "should calculate md5 with a salt" do
60
83
  salt = 'abcdefg'
61
84
  digest = "m59029ys/bc2HpeuZqcQ3w=="
85
+ "abcde\240z405".digest(:md5, :salt => salt).should == digest
62
86
  "abcde\240z405".md5(:salt => salt).should == digest
63
87
  end
64
88
 
65
89
  it "should calculate sha512 in hex/hexidecimal" do
66
90
  digest = "28712b4ee23fe6eac2177be2eb24c3c6f007072756d42377cd57c15833ff22d6e681e562e4816ae3b7b0d213c7cf22b702e30387e67d3010657f7fe600281531"
91
+ "abcde\240z405".digest(:sha512, :encoding => :hex).should == digest
92
+ "abcde\240z405".digest(:sha512, :encoding => :hexidecimal).should == digest
67
93
  "abcde\240z405".sha512(:encoding => :hex).should == digest
68
94
  "abcde\240z405".sha512(:encoding => :hexidecimal).should == digest
69
95
  end
70
96
 
97
+ it "should calculate sha384 in hex/hexidecimal" do
98
+ digest = "3dc40fcf173ab406a89be0c6df55c28bf03fed907a3743e09012bd7b534c39a75ced5c58496a06ab4ddfdbe0e27e54f9"
99
+ "abcde\240z405".digest(:sha384, :encoding => :hex).should == digest
100
+ "abcde\240z405".digest(:sha384, :encoding => :hexidecimal).should == digest
101
+ "abcde\240z405".sha384(:encoding => :hex).should == digest
102
+ "abcde\240z405".sha384(:encoding => :hexidecimal).should == digest
103
+ end
104
+
71
105
  it "should calculate sha256/sha2 in hex/hexidecimal" do
72
106
  digest = "bc6796a4031caa3d97b84f0fe058ab036a14b754bbf1cea9b90934dc439d9d06"
107
+ "abcde\240z405".digest(:sha256, :encoding => :hex).should == digest
108
+ "abcde\240z405".digest(:sha256, :encoding => :hexidecimal).should == digest
109
+ "abcde\240z405".digest(:sha2, :encoding => :hex).should == digest
110
+ "abcde\240z405".digest(:sha2, :encoding => :hexidecimal).should == digest
73
111
  "abcde\240z405".sha256(:encoding => :hex).should == digest
74
112
  "abcde\240z405".sha2(:encoding => :hex).should == digest
75
113
  "abcde\240z405".sha256(:encoding => :hexidecimal).should == digest
@@ -78,34 +116,49 @@ describe String do
78
116
 
79
117
  it "should calculate sha1 in hex/hexidecimal" do
80
118
  digest = "c317821573d55de3c5729c2e1438e89f29f51bfc"
119
+ "abcde\240z405".digest(:sha1, :encoding => :hex).should == digest
120
+ "abcde\240z405".digest(:sha1, :encoding => :hexidecimal).should == digest
81
121
  "abcde\240z405".sha1(:encoding => :hex).should == digest
82
122
  "abcde\240z405".sha1(:encoding => :hexidecimal).should == digest
83
123
  end
84
124
 
85
125
  it "should calculate md5 in hex/hexidecimal" do
86
126
  digest = "4e14f4cbb29b0229e32e9b8286ab11b9"
127
+ "abcde\240z405".digest(:md5, :encoding => :hex).should == digest
128
+ "abcde\240z405".digest(:md5, :encoding => :hexidecimal).should == digest
87
129
  "abcde\240z405".md5(:encoding => :hex).should == digest
88
130
  "abcde\240z405".md5(:encoding => :hexidecimal).should == digest
89
131
  end
90
132
 
91
133
  it "should calculate sha512 in binary" do
92
134
  digest = "(q+N\342?\346\352\302\027{\342\353$\303\306\360\a\a'V\324#w\315W\301X3\377\"\326\346\201\345b\344\201j\343\267\260\322\023\307\317\"\267\002\343\003\207\346}0\020e\177\177\346\000(\0251"
135
+ "abcde\240z405".digest(:sha512, :encoding => :binary).should == digest
93
136
  "abcde\240z405".sha512(:encoding => :binary).should == digest
94
137
  end
95
138
 
139
+ it "should calculate sha384 in binary" do
140
+ digest = "=\304\017\317\027:\264\006\250\233\340\306\337U\302\213\360?\355\220z7C\340\220\022\275{SL9\247\\\355\\XIj\006\253M\337\333\340\342~T\371"
141
+ "abcde\240z405".digest(:sha384, :encoding => :binary).should == digest
142
+ "abcde\240z405".sha384(:encoding => :binary).should == digest
143
+ end
144
+
96
145
  it "should calculate sha256/sha2 in binary" do
97
146
  digest = "\274g\226\244\003\034\252=\227\270O\017\340X\253\003j\024\267T\273\361\316\251\271\t4\334C\235\235\006"
147
+ "abcde\240z405".digest(:sha256, :encoding => :binary).should == digest
148
+ "abcde\240z405".digest(:sha2, :encoding => :binary).should == digest
98
149
  "abcde\240z405".sha256(:encoding => :binary).should == digest
99
150
  "abcde\240z405".sha2(:encoding => :binary).should == digest
100
151
  end
101
152
 
102
153
  it "should calculate sha1 in binary" do
103
154
  digest = "\303\027\202\025s\325]\343\305r\234.\0248\350\237)\365\e\374"
155
+ "abcde\240z405".digest(:sha1, :encoding => :binary).should == digest
104
156
  "abcde\240z405".sha1(:encoding => :binary).should == digest
105
157
  end
106
158
 
107
159
  it "should calculate md5 in binary" do
108
160
  digest = "N\024\364\313\262\233\002)\343.\233\202\206\253\021\271"
161
+ "abcde\240z405".digest(:md5, :encoding => :binary).should == digest
109
162
  "abcde\240z405".md5(:encoding => :binary).should == digest
110
163
  end
111
164
  end
data/spec/string_spec.rb CHANGED
@@ -15,12 +15,13 @@ describe String do
15
15
  "YWJjZGWgejQwNQ==\n".decode64.should == "abcde\240z405"
16
16
  end
17
17
 
18
- it "should encodeHex" do
19
- "abcde\240z405".encodeHex.should == "6162636465a07a343035"
20
- "abcde\240z405".encodeHexidecimal.should == "6162636465a07a343035"
18
+ it "should encode_hex" do
19
+ "abcde\240z405".encode_hex.should == "6162636465a07a343035"
20
+ "abcde\240z405".encode_hexidecimal.should == "6162636465a07a343035"
21
21
  end
22
22
 
23
- it "should decodeHex" do
24
- '6162636465a07a343035'.decodeHexidecimal.should == "abcde\240z405"
23
+ it "should decode_hex" do
24
+ '6162636465a07a343035'.decode_hex.should == "abcde\240z405"
25
+ '6162636465a07a343035'.decode_hexidecimal.should == "abcde\240z405"
25
26
  end
26
27
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 0
9
- version: 0.2.0
8
+ - 1
9
+ version: 0.2.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Travis D. Warlick, Jr.
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-15 00:00:00 -05:00
17
+ date: 2010-03-21 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -105,8 +105,8 @@ signing_key:
105
105
  specification_version: 3
106
106
  summary: A helpful set of utility classes, generators, and command-line tools.
107
107
  test_files:
108
- - spec/configuration_spec.rb
109
- - spec/dionysus_spec.rb
108
+ - spec/string_spec.rb
110
109
  - spec/spec_helper.rb
110
+ - spec/configuration_spec.rb
111
111
  - spec/string_security_spec.rb
112
- - spec/string_spec.rb
112
+ - spec/dionysus_spec.rb