dionysus 1.0.2 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,64 +0,0 @@
1
- require 'active_support/concern'
2
- begin
3
- Markdown
4
- rescue NameError
5
- require 'rdiscount'
6
- end
7
-
8
- module RDoc # :nodoc:
9
- ##
10
- # Extend RDoc to support Markdown files. It will automatically use Markdown
11
- # for any files with the `.md` or `.markdown` extension. You may also specify
12
- # files with `RDoc::Markdown.files`
13
- #
14
- # After RDoc is loaded:
15
- #
16
- # require 'dionysus/rdoc/markdown'
17
- # RDoc::Markdown.files.include('README')
18
- module Markdown
19
- FILE_NAMES = Rake::FileList.new
20
-
21
- def self.files() FILE_NAMES; end
22
-
23
- def self.markdown_for_file?(file_name)
24
- %w(.md .markdown).include?(File.extname(file_name)) or
25
- self.files.resolve.to_a.include?(file_name)
26
- end
27
-
28
- module Parser # :nodoc:
29
- extend ActiveSupport::Concern
30
-
31
- included do
32
- class << self
33
- alias_method :can_parse_without_markdown, :can_parse
34
- alias_method :can_parse, :can_parse_with_markdown
35
- end
36
- end
37
-
38
- module ClassMethods # :nodoc:
39
- def can_parse_with_markdown(file_name)
40
- result = can_parse_without_markdown(file_name)
41
- if result.nil? and Markdown.markdown_for_file?(file_name)
42
- ::RDoc::Parser::Simple
43
- else
44
- result
45
- end
46
- end
47
- end
48
- end
49
-
50
- module TopLevel # :nodoc:
51
- extend ActiveSupport::Concern
52
-
53
- module InstanceMethods # :nodoc:
54
- def description
55
- return super unless Markdown.markdown_for_file?(full_name)
56
- ::Markdown.new(File.read(::RDoc::RDoc.current.generator.basedir + full_name)).to_html
57
- end
58
- end
59
- end
60
- end
61
- end
62
-
63
- RDoc::TopLevel.send(:include, RDoc::Markdown::TopLevel)
64
- RDoc::Parser.send(:include, RDoc::Markdown::Parser)
@@ -1,56 +0,0 @@
1
- require 'active_support/concern'
2
-
3
- module RDoc # :nodoc:
4
- ##
5
- # Extend RDoc to support plaintext files that will be displayed inside a &lt;pre&gt; block.
6
- #
7
- # After RDoc is loaded:
8
- #
9
- # require 'dionysus/rdoc/no_markup'
10
- # RDoc::NoMarkup.files.include('LICENSE','VERSION')
11
- module NoMarkup
12
- FILE_NAMES = Rake::FileList.new
13
-
14
- def self.files() FILE_NAMES; end
15
-
16
- def self.no_markup_for_file?(file_name)
17
- self.files.resolve.to_a.include?(file_name)
18
- end
19
-
20
- module Parser # :nodoc:
21
- extend ActiveSupport::Concern
22
-
23
- included do
24
- class << self
25
- alias_method :can_parse_with_markup, :can_parse
26
- alias_method :can_parse, :can_parse_without_markup
27
- end
28
- end
29
-
30
- module ClassMethods # :nodoc:
31
- def can_parse_without_markup(file_name)
32
- if NoMarkup.no_markup_for_file?(file_name)
33
- ::RDoc::Parser::Simple
34
- else
35
- can_parse_with_markup(file_name)
36
- end
37
- end
38
- end
39
- end
40
-
41
- module TopLevel # :nodoc:
42
- extend ActiveSupport::Concern
43
-
44
- module InstanceMethods # :nodoc:
45
- def description
46
- return super unless NoMarkup.no_markup_for_file?(full_name)
47
- txt = File.read(::RDoc::RDoc.current.generator.basedir + full_name)
48
- %(<pre>#{txt}</pre>)
49
- end
50
- end
51
- end
52
- end
53
- end
54
-
55
- RDoc::TopLevel.send(:include, RDoc::NoMarkup::TopLevel)
56
- RDoc::Parser.send(:include, RDoc::NoMarkup::Parser)
@@ -1,3 +0,0 @@
1
- require 'dionysus'
2
- require 'dionysus/security/string'
3
- require 'dionysus/security/password_salt'
@@ -1,138 +0,0 @@
1
- require 'dionysus'
2
- require 'active_support/secure_random'
3
-
4
- ##
5
- # Encapsulates a password salt. This is usually not required specifically,
6
- # instead it's required as a part of the security module:
7
- #
8
- # require 'dionysus/security'
9
- #
10
- # However, you may require it specifically:
11
- #
12
- # require 'dionysus/security/password_salt'
13
- #
14
- # Examples
15
- #
16
- # salt = PasswordSalt.new
17
- # #=> generates random salt of 8 characters
18
- # salt.salt_password('foobar') #=> 'foobar0PD0oKAj'
19
- #
20
- # salt = PasswordSalt.new(:length => 20)
21
- # #=> generates random salt of 20 characters
22
- # salt.salt_password('foobar') #=> 'foobar7qvFpfi+3jGVFaA5TaE7'
23
- #
24
- # salt = PasswordSalt.new('ABCDEFG', :beginning)
25
- # #=> generates salt 'abcdef' with beginning placement
26
- # salt.salt_password('foobar')
27
- # #=> 'ABCDEFGfoobar'
28
- #
29
- # salt = PasswordSalt.new(:split, :length => 10)
30
- # #=> generates random salt of 10 characters with split placement
31
- # salt.salt_password('foobar')
32
- # #=> 'WyVjpfoobarjGYXJ'
33
- class PasswordSalt
34
- PLACEMENTS = [:before, :after, :split]
35
- DEFAULT_LENGTH = 8
36
- DEFAULT_PLACEMENT = :after
37
-
38
- @@secure_random = SecureRandom
39
- cattr_accessor :secure_random
40
-
41
- attr_accessor :string
42
- attr_reader :placement
43
-
44
- ##
45
- # Generate a salt string of the given length (Default: 8) with the base64
46
- # character set. Optionally, you may pass the format as <tt>:binary</tt> to generate
47
- # a binary salt.
48
- def self.generate( length = DEFAULT_LENGTH, format = :base64 )
49
- if length < 0
50
- raise ArgumentError, "Invalid length: #{length}"
51
- end
52
-
53
- case format.to_sym
54
- when :base64
55
- self.secure_random.base64(length)[0...length]
56
- when :binary
57
- self.secure_random.random_bytes(length)
58
- else
59
- raise ArgumentError, "Invalid format: #{format}"
60
- end
61
- end
62
-
63
- ##
64
- # Initialize a new PasswordSalt
65
- #
66
- # If you pass the first argument as a string, the string will be set as
67
- # the salt:
68
- # PasswordSalt.new('ABCDEFG')
69
- #
70
- # If the first argument is <tt>:new</tt> or is an option hash, a random salt
71
- # will be generated:
72
- # PasswordSalt.new(:new)
73
- # PasswordSalt.new(:length => 20)
74
- # PasswordSalt.new
75
- #
76
- # If the first or second argument is a Symbol (other than <tt>:new</tt>), it
77
- # will be interpreted as the placement:
78
- # PasswordSalt.new(:before)
79
- # PasswordSalt.new(:new, :before)
80
- # PasswordSalt.new('ABCDEFG', :before)
81
- #
82
- # You may always pass in an options hash as the last argument.
83
- # [length] Length of salt to be generated.
84
- # Default: 8
85
- def initialize( *args )
86
- options = args.extract_options!
87
-
88
- self.string = args.detect { |val| val.is_a?(String) or val == :new }
89
- self.placement = args.detect { |val| PLACEMENTS.include?(val) } || DEFAULT_PLACEMENT
90
-
91
- if self.string == :new or self.string.nil?
92
- self.string = self.class.generate(options[:length] || DEFAULT_LENGTH)
93
- end
94
- end
95
-
96
- ##
97
- # Returns the given password, but salted.
98
- def salt_password( password )
99
- case self.placement
100
- when :after
101
- password.to_s + string
102
- when :before
103
- string + password.to_s
104
- when :split
105
- string[0...(string.length/2).floor] +
106
- password.to_s +
107
- string[(string.length/2).floor...string.length]
108
- else
109
- raise "Invalid salt placement: #{self.placement}"
110
- end
111
- end
112
-
113
- ##
114
- # Set the salt's placement
115
- #
116
- # [after] Place the salt after the password.
117
- # [before] Place the salt before the password.
118
- # [split] Place half of the salt before and half after the password. For
119
- # salts of odd length, the shorter half will be in front.
120
- def placement=( sym )
121
- unless PLACEMENTS.include?(sym.to_sym)
122
- raise ArgumentError, "Invalid salt placement: #{sym}"
123
- end
124
- @placement = sym.to_sym
125
- end
126
-
127
- ##
128
- # Returns the salt string.
129
- def to_s
130
- self.string
131
- end
132
-
133
- ##
134
- # Returns +true+ if the given salt is equivilent to this salt, +false+ otherwise
135
- def eql?( salt )
136
- self.to_s.eql?(salt.to_s) && self.placement.eql?(salt.placement)
137
- end
138
- end
@@ -1,126 +0,0 @@
1
- require 'dionysus'
2
- require 'dionysus/digest'
3
- require 'dionysus/string'
4
- require 'dionysus/security/password_salt'
5
- require 'dionysus/version_string'
6
-
7
- ##
8
- # Adds String hashing and salting convenience methods.
9
- #
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
19
- class String
20
- ##
21
- # Generate a random, binary salt with the given length (default 16)
22
- def self.salt( length = 16 )
23
- PasswordSalt.generate(length, :binary).to_s
24
- end
25
-
26
- ##
27
- # Sanitize the String from memory. This is non-reversible. Runs 7 passes
28
- # by default.
29
- #
30
- # NOTE: In Ruby 1.9, this uses the String#setbyte(index, int) method. In Ruby < 1.9, this
31
- # uses String#[]=(int)
32
- #
33
- def sanitize( passes = 7 )
34
- if RUBY_VERSION.satisfies?('< 1.9')
35
- passes.times do
36
- (0...self.length).each { |i| self[i] = rand(256) }
37
- end
38
- (0...self.length).each { |i| self[i] = 0 }
39
- self.delete!("\000")
40
- elsif RUBY_VERSION.satisfies?('>= 1.9')
41
- passes.times do
42
- (0...self.length).each { |i| self.setbyte(i, rand(256)) }
43
- end
44
- (0...self.length).each { |i| self.setbyte(i, 0) }
45
- self.clear
46
- end
47
- end
48
-
49
- ##
50
- # Generate the given digest hash.
51
- #
52
- # Options:
53
- # [salt] Salt to append to the string. (This may be a String or a
54
- # PasswordSalt object) Default: ''
55
- # [encoding] Encoding for the hash: <tt>:binary</tt>, <tt>:hex</tt>,
56
- # <tt>:hexidecimal</tt>, <tt>:base64</tt>
57
- # Default: <tt>:base64</tt>
58
- def digest( klass, options = {} )
59
- digest = Digest.digest(klass, _salted(options[:salt]))
60
- _encode(digest, options[:encoding] || :base64)
61
- end
62
-
63
- ##
64
- # Call the appropriate digest method.
65
- def method_missing( method, *args )
66
- super if block_given?
67
-
68
- if Digest.available_digests.include?(method)
69
- self.digest(method, *args)
70
- else
71
- super
72
- end
73
- end
74
-
75
- ##
76
- # Detect the digest of the string. Returns nil if the digest cannot be
77
- # determined.
78
- #
79
- # Example:
80
- # "wxeCFXPVXePFcpwuFDjonyn1G/w=".detect_digest(:base64) #=> :sha1
81
- # "foobar".detect_digest(:hex) #=> nil
82
- def detect_digest( encoding )
83
- Digest.detect_digest(self, encoding)
84
- end
85
-
86
- ##
87
- # Detect the digest of the string. Raises "Unknown digest" if the digest
88
- # cannot be determined.
89
- #
90
- # Example:
91
- # "wxeCFXPVXePFcpwuFDjonyn1G/w=".detect_digest!(:base64) #=> :sha1
92
- # "foobar".detect_digest!(:hex) #=> RuntimeError
93
- def detect_digest!( encoding )
94
- Digest.detect_digest!(self, encoding)
95
- end
96
-
97
- private
98
-
99
- ##
100
- # Salt the string by appending it to the end or utilizing the
101
- # PasswordSalt's salt_password method
102
- def _salted( salt )
103
- return self if salt.blank?
104
-
105
- unless salt.is_a?(PasswordSalt)
106
- salt = PasswordSalt.new(salt)
107
- end
108
-
109
- salt.salt_password(self)
110
- end
111
-
112
- ##
113
- # Encode the value to the given encoding
114
- def _encode( value, encoding )
115
- case encoding
116
- when :binary
117
- value
118
- when :base64
119
- value.encode64s
120
- when :hex, :hexidecimal
121
- value.encode_hex
122
- else
123
- raise ArgumentError, "Invalid encoding: #{encoding}"
124
- end
125
- end
126
- end
@@ -1,48 +0,0 @@
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
- HEX_REGEXP = /^[a-f0-9]+$/
10
- BASE64_REGEXP = /^[A-Za-z0-9\+\/\=]+$/
11
- ENCODING_BITS_PER_CHAR = {:binary => 8, :base64 => 6, :hex => 4, :hexidecimal => 4}
12
-
13
- ##
14
- # Encode the Base64 (without newlines)
15
- def encode64s() Base64.encode64s(self); end
16
-
17
- ##
18
- # Encode to Base 64
19
- def encode64() Base64.encode64(self); end
20
-
21
- ##
22
- # Decode from Base 64
23
- def decode64() Base64.decode64(self); end
24
-
25
- ##
26
- # Encode to hexidecimal
27
- def encode_hexidecimal() self.unpack('H*').first; end
28
- alias_method :encode_hex, :encode_hexidecimal
29
-
30
- ##
31
- # Decode from hexidecimal
32
- def decode_hexidecimal() [self].pack('H*'); end
33
- alias_method :decode_hex, :decode_hexidecimal
34
-
35
- ##
36
- # Detect the encoding of the string
37
- def detect_encoding
38
- return nil if blank?
39
-
40
- if match(HEX_REGEXP)
41
- :hex
42
- elsif match(BASE64_REGEXP)
43
- :base64
44
- else
45
- :binary
46
- end
47
- end
48
- end
@@ -1,96 +0,0 @@
1
- require 'dionysus'
2
- require 'active_support/core_ext/kernel/reporting'
3
- require 'active_support/core_ext/module/delegation'
4
-
5
- module Dionysus
6
- ##
7
- # = VersonString
8
- #
9
- # require 'dionysus/version_string'
10
- #
11
- # VersionString is a proxy to Gem::Version that modifies the comparable
12
- # interface on String to compare based on the Gem::Version comparator.
13
- # It also adds a +satisfies?+ method to check for a Gem::Requirement.
14
- #
15
- # In addition, requiring VersionString changes the +RUBY_VERSION+ constant
16
- # into a VersionString. Because VersionString "is a" String, there should be
17
- # no ill-effects with this change since only the Comparable interface is
18
- # overriden and this does not change the original operation of the == method.
19
- #
20
- # The comparator works by converting strings into Gem::Version objects, so
21
- # this still works as you would expect:
22
- #
23
- # RUBY_VERSION < '1.9'
24
- class VersionString < String
25
- include Comparable
26
-
27
- ##
28
- # Initializes a VersionString and freezes itself.
29
- def initialize(*args)
30
- super(*args)
31
- to_rubygem_version
32
- freeze
33
- end
34
-
35
- ##
36
- # Compares this VersionString to another String, VersionString, or
37
- # Gem::Version.
38
- #
39
- # Examples:
40
- #
41
- # Dionysus::VersionString.new('1.0') > '1.0' => false
42
- # Dionysus::VersionString.new('1.0') == Gem::Version('1.0') => true
43
- # Dionysus::VersionString.new('1.0') <=> Dionysus::VersionString.new('1.0') => 0
44
- #
45
- # See http://rubygems.rubyforge.org/rubygems-update/Gem/Version.html
46
- def <=>( obj )
47
- obj = obj.dup
48
- if obj.is_a?(String) or obj.is_a?(Gem::Version)
49
- obj = Gem::Version.create(obj)
50
- else
51
- raise ArgumentError, "Can only compare to a Dionysus::VersionString/String or Gem::Version"
52
- end
53
-
54
- self.to_rubygem_version <=> obj
55
- end
56
- alias_method :"eql?", :"=="
57
-
58
- ##
59
- # Determines if the given requirement String or Gem::Requirement is
60
- # satisfied by this version.
61
- #
62
- # Examples:
63
- #
64
- # Dionysus::VersionString.new('1.6.7').satisfies? '~> 1.6.0' => true
65
- # Dionysus::VersionString.new('1.6.7').satisfies? Gem::Requirement('< 1.6') => false
66
- #
67
- # See http://rubygems.rubyforge.org/rubygems-update/Gem/Requirement.html
68
- def satisfies?( requirement )
69
- requirement = requirement.dup
70
- if requirement.is_a?(String) or requirement.is_a?(Gem::Requirement)
71
- requirement = Gem::Requirement.create(requirement)
72
- else
73
- raise ArgumentError, "Can only compare to a String or Gem::Requirement"
74
- end
75
-
76
- requirement.satisfied_by?(to_rubygem_version)
77
- end
78
-
79
- ##
80
- # Converts this VersionString into a Gem::Version
81
- def to_rubygem_version
82
- @rubygem_version ||= Gem::Version.create(self)
83
- end
84
- alias_method :to_gem_version, :to_rubygem_version
85
-
86
- ##
87
- # Indicates whether this VersionString is a prerelease.
88
- #
89
- # See http://rubygems.rubyforge.org/rubygems-update/Gem/Version.html#method-i-prerelease%3F
90
- def prerelease?() to_rubygem_version.prerelease?; end
91
- end
92
- end
93
-
94
- silence_warnings do
95
- RUBY_VERSION = Dionysus::VersionString.new(RUBY_VERSION)
96
- end