dionysus 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
data/dionysus.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{dionysus}
8
- s.version = "0.1.1"
8
+ s.version = "0.2.0"
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."]
@@ -25,10 +25,15 @@ Gem::Specification.new do |s|
25
25
  "dionysus.gemspec",
26
26
  "lib/dionysus.rb",
27
27
  "lib/dionysus/configuration.rb",
28
+ "lib/dionysus/security.rb",
29
+ "lib/dionysus/security/string.rb",
30
+ "lib/dionysus/string.rb",
28
31
  "spec/configuration_spec.rb",
29
32
  "spec/dionysus_spec.rb",
30
33
  "spec/spec.opts",
31
- "spec/spec_helper.rb"
34
+ "spec/spec_helper.rb",
35
+ "spec/string_security_spec.rb",
36
+ "spec/string_spec.rb"
32
37
  ]
33
38
  s.homepage = %q{http://github.com/tekwiz/dionysus}
34
39
  s.rdoc_options = ["--charset=UTF-8"]
@@ -38,7 +43,9 @@ Gem::Specification.new do |s|
38
43
  s.test_files = [
39
44
  "spec/configuration_spec.rb",
40
45
  "spec/dionysus_spec.rb",
41
- "spec/spec_helper.rb"
46
+ "spec/spec_helper.rb",
47
+ "spec/string_security_spec.rb",
48
+ "spec/string_spec.rb"
42
49
  ]
43
50
 
44
51
  if s.respond_to? :specification_version then
@@ -1,3 +1,5 @@
1
+ require 'dionysus'
2
+
1
3
  ##
2
4
  # = Configuration
3
5
  #
@@ -0,0 +1 @@
1
+ require 'dionysus/security/string'
@@ -0,0 +1,105 @@
1
+ require 'digest/sha1'
2
+ require 'digest/sha2'
3
+ require 'digest/md5'
4
+
5
+ require 'dionysus'
6
+ require 'dionysus/string'
7
+ ##
8
+ # Adds String hashing and salting convenience methods.
9
+ #
10
+ # require 'dionysus/security/string'
11
+ class String
12
+ ##
13
+ # Generate a random, binary salt with the given length (default 16)
14
+ def self.salt( length = 16 )
15
+ length.times.to_a.collect {|v| rand(255).chr}.join[0...length]
16
+ end
17
+
18
+ ##
19
+ # Generate the SHA 512 HASh, with the given salt appended
20
+ #
21
+ # Options:
22
+ # [salt] Salt to append to the string.
23
+ # Default: ''
24
+ # [encoding] Encoding for the hash: <tt>:binary</tt>, <tt>:hex</tt>, <tt>:hexidecimal</tt>, <tt>:base64</tt>
25
+ # 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)
68
+ end
69
+
70
+ ##
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)
81
+ end
82
+
83
+ private
84
+
85
+ ##
86
+ # Salt the string by appending it to the end.
87
+ def _salted( salt )
88
+ self + (salt || '')
89
+ end
90
+
91
+ ##
92
+ # Encode the value to the given encoding
93
+ def _encode( value, encoding )
94
+ case encoding
95
+ when :binary
96
+ value
97
+ when :base64
98
+ value.encode64s
99
+ when :hex, :hexidecimal
100
+ value.encodeHex
101
+ else
102
+ raise ArgumentError, "Invalid encoding: #{encoding}"
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,30 @@
1
+ require 'dionysus'
2
+ require 'active_support/base64'
3
+
4
+ ##
5
+ # Adds string encoding convenience methods.
6
+ #
7
+ # require 'dionysus/string'
8
+ class String
9
+ ##
10
+ # Encode the Base64 (without newlines)
11
+ def encode64s() Base64.encode64s(self); end
12
+
13
+ ##
14
+ # Encode to Base 64
15
+ def encode64() Base64.encode64(self); end
16
+
17
+ ##
18
+ # Decode from Base 64
19
+ def decode64() Base64.decode64(self); end
20
+
21
+ ##
22
+ # Encode to hexidecimal
23
+ def encodeHexidecimal() self.unpack('H*').first; end
24
+ alias_method :encodeHex, :encodeHexidecimal
25
+
26
+ ##
27
+ # Decode from hexidecimal
28
+ def decodeHexidecimal() [self].pack('H*'); end
29
+ alias_method :decodeHex, :decodeHexidecimal
30
+ end
@@ -1,5 +1,6 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
+ require 'dionysus'
3
4
  describe Dionysus do
4
5
  it "should have a version number" do
5
6
  Dionysus::VERSION.should match(/\A\d+(\.\d+)+(\.\w+)??\Z/)
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  $LOAD_PATH.unshift(File.dirname(__FILE__))
2
2
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
- require 'dionysus'
4
3
  require 'spec'
5
4
  require 'spec/autorun'
6
5
 
@@ -0,0 +1,111 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ require 'dionysus/security/string'
4
+ describe String do
5
+ it "should generate a salt 16 bytes long" do
6
+ String.salt.length.should == 16
7
+ end
8
+
9
+ it "should generate a salt arbitrary nubmer of bytes long" do
10
+ String.salt(5).length.should == 5
11
+ String.salt(1).length.should == 1
12
+ end
13
+
14
+ it "should calculate sha512 in base64" do
15
+ digest = "KHErTuI/5urCF3vi6yTDxvAHBydW1CN3zVfBWDP/ItbmgeVi5IFq47ew0hPHzyK3AuMDh+Z9MBBlf3/mACgVMQ=="
16
+ "abcde\240z405".sha512.should == digest
17
+ "abcde\240z405".sha512(:encoding => :base64).should == digest
18
+ end
19
+
20
+ it "should calculate sha256/sha2 in base64" do
21
+ digest = "vGeWpAMcqj2XuE8P4FirA2oUt1S78c6puQk03EOdnQY="
22
+ "abcde\240z405".sha256.should == digest
23
+ "abcde\240z405".sha256(:encoding => :base64).should == digest
24
+ "abcde\240z405".sha2.should == digest
25
+ "abcde\240z405".sha2(:encoding => :base64).should == digest
26
+ end
27
+
28
+ it "should calculate sha1 in base64" do
29
+ digest = "wxeCFXPVXePFcpwuFDjonyn1G/w="
30
+ "abcde\240z405".sha1.should == digest
31
+ "abcde\240z405".sha1(:encoding => :base64).should == digest
32
+ end
33
+
34
+ it "should calculate md5 in base64" do
35
+ digest = "ThT0y7KbAinjLpuChqsRuQ=="
36
+ "abcde\240z405".md5.should == digest
37
+ "abcde\240z405".md5(:encoding => :base64).should == digest
38
+ end
39
+
40
+ it "should calculate sha512 with a salt" do
41
+ salt = 'abcdefg'
42
+ digest = "oxoD5Mwq6ck4B1P2tfsrCQnNHzBYQRKOFGTj1T0EFPWeNuy1/YPtsKGiz2os9ljt0/OwXEt/l8LsK/ktbwEHbg=="
43
+ "abcde\240z405".sha512(:salt => salt).should == digest
44
+ end
45
+
46
+ it "should calculate sha256/sha2 with a salt" do
47
+ salt = 'abcdefg'
48
+ digest = "fWzU2g2GFAS5EAgTS09djo2eDDQdNp1xSTdrRO7m9v4="
49
+ "abcde\240z405".sha256(:salt => salt).should == digest
50
+ "abcde\240z405".sha2(:salt => salt).should == digest
51
+ end
52
+
53
+ it "should calculate sha1 with a salt" do
54
+ salt = 'abcdefg'
55
+ digest = "JFdwlGl341qtbqX6mPQbjs8d1/U="
56
+ "abcde\240z405".sha1(:salt => salt).should == digest
57
+ end
58
+
59
+ it "should calculate md5 with a salt" do
60
+ salt = 'abcdefg'
61
+ digest = "m59029ys/bc2HpeuZqcQ3w=="
62
+ "abcde\240z405".md5(:salt => salt).should == digest
63
+ end
64
+
65
+ it "should calculate sha512 in hex/hexidecimal" do
66
+ digest = "28712b4ee23fe6eac2177be2eb24c3c6f007072756d42377cd57c15833ff22d6e681e562e4816ae3b7b0d213c7cf22b702e30387e67d3010657f7fe600281531"
67
+ "abcde\240z405".sha512(:encoding => :hex).should == digest
68
+ "abcde\240z405".sha512(:encoding => :hexidecimal).should == digest
69
+ end
70
+
71
+ it "should calculate sha256/sha2 in hex/hexidecimal" do
72
+ digest = "bc6796a4031caa3d97b84f0fe058ab036a14b754bbf1cea9b90934dc439d9d06"
73
+ "abcde\240z405".sha256(:encoding => :hex).should == digest
74
+ "abcde\240z405".sha2(:encoding => :hex).should == digest
75
+ "abcde\240z405".sha256(:encoding => :hexidecimal).should == digest
76
+ "abcde\240z405".sha2(:encoding => :hexidecimal).should == digest
77
+ end
78
+
79
+ it "should calculate sha1 in hex/hexidecimal" do
80
+ digest = "c317821573d55de3c5729c2e1438e89f29f51bfc"
81
+ "abcde\240z405".sha1(:encoding => :hex).should == digest
82
+ "abcde\240z405".sha1(:encoding => :hexidecimal).should == digest
83
+ end
84
+
85
+ it "should calculate md5 in hex/hexidecimal" do
86
+ digest = "4e14f4cbb29b0229e32e9b8286ab11b9"
87
+ "abcde\240z405".md5(:encoding => :hex).should == digest
88
+ "abcde\240z405".md5(:encoding => :hexidecimal).should == digest
89
+ end
90
+
91
+ it "should calculate sha512 in binary" do
92
+ 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"
93
+ "abcde\240z405".sha512(:encoding => :binary).should == digest
94
+ end
95
+
96
+ it "should calculate sha256/sha2 in binary" do
97
+ 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"
98
+ "abcde\240z405".sha256(:encoding => :binary).should == digest
99
+ "abcde\240z405".sha2(:encoding => :binary).should == digest
100
+ end
101
+
102
+ it "should calculate sha1 in binary" do
103
+ digest = "\303\027\202\025s\325]\343\305r\234.\0248\350\237)\365\e\374"
104
+ "abcde\240z405".sha1(:encoding => :binary).should == digest
105
+ end
106
+
107
+ it "should calculate md5 in binary" do
108
+ digest = "N\024\364\313\262\233\002)\343.\233\202\206\253\021\271"
109
+ "abcde\240z405".md5(:encoding => :binary).should == digest
110
+ end
111
+ end
@@ -0,0 +1,26 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ require 'dionysus/string'
4
+ describe String do
5
+ it "should encode64s" do
6
+ "abcde\240z405".encode64s.should == "YWJjZGWgejQwNQ=="
7
+ end
8
+
9
+ it "should encode64" do
10
+ "abcde\240z405".encode64.should == "YWJjZGWgejQwNQ==\n"
11
+ end
12
+
13
+ it "should decode64" do
14
+ "YWJjZGWgejQwNQ==".decode64.should == "abcde\240z405"
15
+ "YWJjZGWgejQwNQ==\n".decode64.should == "abcde\240z405"
16
+ end
17
+
18
+ it "should encodeHex" do
19
+ "abcde\240z405".encodeHex.should == "6162636465a07a343035"
20
+ "abcde\240z405".encodeHexidecimal.should == "6162636465a07a343035"
21
+ end
22
+
23
+ it "should decodeHex" do
24
+ '6162636465a07a343035'.decodeHexidecimal.should == "abcde\240z405"
25
+ end
26
+ end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
- - 1
9
- version: 0.1.1
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Travis D. Warlick, Jr.
@@ -65,10 +65,15 @@ files:
65
65
  - dionysus.gemspec
66
66
  - lib/dionysus.rb
67
67
  - lib/dionysus/configuration.rb
68
+ - lib/dionysus/security.rb
69
+ - lib/dionysus/security/string.rb
70
+ - lib/dionysus/string.rb
68
71
  - spec/configuration_spec.rb
69
72
  - spec/dionysus_spec.rb
70
73
  - spec/spec.opts
71
74
  - spec/spec_helper.rb
75
+ - spec/string_security_spec.rb
76
+ - spec/string_spec.rb
72
77
  has_rdoc: true
73
78
  homepage: http://github.com/tekwiz/dionysus
74
79
  licenses: []
@@ -103,3 +108,5 @@ test_files:
103
108
  - spec/configuration_spec.rb
104
109
  - spec/dionysus_spec.rb
105
110
  - spec/spec_helper.rb
111
+ - spec/string_security_spec.rb
112
+ - spec/string_spec.rb