dionysus 1.0.2 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +16 -9
- data/README.md +31 -34
- data/Rakefile +16 -79
- data/description.md +1 -0
- data/lib/dionysus.rb +3 -22
- data/lib/dionysus/configuration_callbacks.rb +93 -0
- data/lib/dionysus/redcarpet/includes.rb +41 -0
- data/lib/dionysus/string/version_match.rb +8 -0
- data/lib/dionysus/travisci/gemfile_generator.rb +67 -0
- data/lib/dionysus/version.rb +3 -0
- data/spec/lib/dionysus_spec.rb +18 -0
- data/spec/lib/string/version_match_spec.rb +17 -0
- data/spec/spec_helper.rb +17 -6
- metadata +54 -307
- data/.document +0 -5
- data/.gitignore +0 -22
- data/.rspec +0 -1
- data/Gemfile.lock +0 -41
- data/VERSION +0 -1
- data/dionysus.gemspec +0 -121
- data/lib/dionysus/configuration.rb +0 -200
- data/lib/dionysus/digest.rb +0 -135
- data/lib/dionysus/rdoc/markdown.rb +0 -64
- data/lib/dionysus/rdoc/no_markup.rb +0 -56
- data/lib/dionysus/security.rb +0 -3
- data/lib/dionysus/security/password_salt.rb +0 -138
- data/lib/dionysus/security/string.rb +0 -126
- data/lib/dionysus/string.rb +0 -48
- data/lib/dionysus/version_string.rb +0 -96
- data/spec/configuration_spec.rb +0 -176
- data/spec/digest_spec.rb +0 -183
- data/spec/dionysus_spec.rb +0 -8
- data/spec/password_salt_spec.rb +0 -169
- data/spec/string_security_spec.rb +0 -204
- data/spec/string_spec.rb +0 -46
- data/spec/version_string_spec.rb +0 -83
data/.document
DELETED
data/.gitignore
DELETED
data/.rspec
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
--color
|
data/Gemfile.lock
DELETED
@@ -1,41 +0,0 @@
|
|
1
|
-
GEM
|
2
|
-
remote: http://rubygems.org/
|
3
|
-
specs:
|
4
|
-
activesupport (3.0.0)
|
5
|
-
diff-lcs (1.1.2)
|
6
|
-
git (1.2.5)
|
7
|
-
jeweler (1.5.0.pre3)
|
8
|
-
bundler (~> 1.0.0)
|
9
|
-
git (>= 1.2.5)
|
10
|
-
rake
|
11
|
-
json (1.4.6)
|
12
|
-
rake (0.8.7)
|
13
|
-
rcov (0.9.9)
|
14
|
-
rdiscount (1.6.5)
|
15
|
-
rdoc (2.4.3)
|
16
|
-
rspec (2.0.0.beta.22)
|
17
|
-
rspec-core (= 2.0.0.beta.22)
|
18
|
-
rspec-expectations (= 2.0.0.beta.22)
|
19
|
-
rspec-mocks (= 2.0.0.beta.22)
|
20
|
-
rspec-core (2.0.0.beta.22)
|
21
|
-
rspec-expectations (2.0.0.beta.22)
|
22
|
-
diff-lcs (>= 1.1.2)
|
23
|
-
rspec-mocks (2.0.0.beta.22)
|
24
|
-
rspec-core (= 2.0.0.beta.22)
|
25
|
-
rspec-expectations (= 2.0.0.beta.22)
|
26
|
-
sdoc (0.2.20)
|
27
|
-
json (>= 1.1.3)
|
28
|
-
rdoc (= 2.4.3)
|
29
|
-
|
30
|
-
PLATFORMS
|
31
|
-
ruby
|
32
|
-
|
33
|
-
DEPENDENCIES
|
34
|
-
activesupport (~> 3.0.0)
|
35
|
-
bundler (~> 1.0)
|
36
|
-
jeweler (~> 1.5.0)
|
37
|
-
rake (~> 0.8.0)
|
38
|
-
rcov (~> 0.9.0)
|
39
|
-
rdiscount (~> 1.6.0)
|
40
|
-
rspec (~> 2.0.0)
|
41
|
-
sdoc (~> 0.2.0)
|
data/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
1.0.2
|
data/dionysus.gemspec
DELETED
@@ -1,121 +0,0 @@
|
|
1
|
-
# Generated by jeweler
|
2
|
-
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
-
# -*- encoding: utf-8 -*-
|
5
|
-
|
6
|
-
Gem::Specification.new do |s|
|
7
|
-
s.name = %q{dionysus}
|
8
|
-
s.version = "1.0.2"
|
9
|
-
|
10
|
-
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
-
s.authors = ["Travis D. Warlick, Jr."]
|
12
|
-
s.date = %q{2010-10-03}
|
13
|
-
s.email = %q{warlickt@operissystems.com}
|
14
|
-
s.extra_rdoc_files = [
|
15
|
-
"LICENSE",
|
16
|
-
"README.md"
|
17
|
-
]
|
18
|
-
s.files = [
|
19
|
-
".document",
|
20
|
-
".gitignore",
|
21
|
-
".rspec",
|
22
|
-
"Gemfile",
|
23
|
-
"Gemfile.lock",
|
24
|
-
"LICENSE",
|
25
|
-
"README.md",
|
26
|
-
"Rakefile",
|
27
|
-
"VERSION",
|
28
|
-
"dionysus.gemspec",
|
29
|
-
"lib/dionysus.rb",
|
30
|
-
"lib/dionysus/configuration.rb",
|
31
|
-
"lib/dionysus/digest.rb",
|
32
|
-
"lib/dionysus/rdoc/markdown.rb",
|
33
|
-
"lib/dionysus/rdoc/no_markup.rb",
|
34
|
-
"lib/dionysus/security.rb",
|
35
|
-
"lib/dionysus/security/password_salt.rb",
|
36
|
-
"lib/dionysus/security/string.rb",
|
37
|
-
"lib/dionysus/string.rb",
|
38
|
-
"lib/dionysus/version_string.rb",
|
39
|
-
"spec/configuration_spec.rb",
|
40
|
-
"spec/digest_spec.rb",
|
41
|
-
"spec/dionysus_spec.rb",
|
42
|
-
"spec/password_salt_spec.rb",
|
43
|
-
"spec/spec_helper.rb",
|
44
|
-
"spec/string_security_spec.rb",
|
45
|
-
"spec/string_spec.rb",
|
46
|
-
"spec/version_string_spec.rb"
|
47
|
-
]
|
48
|
-
s.homepage = %q{http://github.com/tekwiz/dionysus}
|
49
|
-
s.require_paths = ["lib"]
|
50
|
-
s.rubygems_version = %q{1.3.7}
|
51
|
-
s.summary = %q{A helpful set of utility classes, generators, and command-line tools.}
|
52
|
-
s.test_files = [
|
53
|
-
"spec/configuration_spec.rb",
|
54
|
-
"spec/digest_spec.rb",
|
55
|
-
"spec/dionysus_spec.rb",
|
56
|
-
"spec/password_salt_spec.rb",
|
57
|
-
"spec/spec_helper.rb",
|
58
|
-
"spec/string_security_spec.rb",
|
59
|
-
"spec/string_spec.rb",
|
60
|
-
"spec/version_string_spec.rb"
|
61
|
-
]
|
62
|
-
|
63
|
-
if s.respond_to? :specification_version then
|
64
|
-
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
65
|
-
s.specification_version = 3
|
66
|
-
|
67
|
-
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
68
|
-
s.add_runtime_dependency(%q<bundler>, ["~> 1.0"])
|
69
|
-
s.add_runtime_dependency(%q<activesupport>, ["~> 3.0.0"])
|
70
|
-
s.add_development_dependency(%q<rspec>, ["~> 2.0.0"])
|
71
|
-
s.add_development_dependency(%q<jeweler>, ["~> 1.5.0"])
|
72
|
-
s.add_development_dependency(%q<rake>, ["~> 0.8.0"])
|
73
|
-
s.add_development_dependency(%q<rcov>, ["~> 0.9.0"])
|
74
|
-
s.add_development_dependency(%q<sdoc>, ["~> 0.2.0"])
|
75
|
-
s.add_development_dependency(%q<rdiscount>, ["~> 1.6.0"])
|
76
|
-
s.add_runtime_dependency(%q<bundler>, ["~> 1.0"])
|
77
|
-
s.add_runtime_dependency(%q<activesupport>, ["~> 3.0.0"])
|
78
|
-
s.add_development_dependency(%q<rspec>, ["~> 2.0.0"])
|
79
|
-
s.add_development_dependency(%q<jeweler>, ["~> 1.5.0"])
|
80
|
-
s.add_development_dependency(%q<rake>, ["~> 0.8.0"])
|
81
|
-
s.add_development_dependency(%q<rcov>, ["~> 0.9.0"])
|
82
|
-
s.add_development_dependency(%q<sdoc>, ["~> 0.2.0"])
|
83
|
-
s.add_development_dependency(%q<rdiscount>, ["~> 1.6.0"])
|
84
|
-
else
|
85
|
-
s.add_dependency(%q<bundler>, ["~> 1.0"])
|
86
|
-
s.add_dependency(%q<activesupport>, ["~> 3.0.0"])
|
87
|
-
s.add_dependency(%q<rspec>, ["~> 2.0.0"])
|
88
|
-
s.add_dependency(%q<jeweler>, ["~> 1.5.0"])
|
89
|
-
s.add_dependency(%q<rake>, ["~> 0.8.0"])
|
90
|
-
s.add_dependency(%q<rcov>, ["~> 0.9.0"])
|
91
|
-
s.add_dependency(%q<sdoc>, ["~> 0.2.0"])
|
92
|
-
s.add_dependency(%q<rdiscount>, ["~> 1.6.0"])
|
93
|
-
s.add_dependency(%q<bundler>, ["~> 1.0"])
|
94
|
-
s.add_dependency(%q<activesupport>, ["~> 3.0.0"])
|
95
|
-
s.add_dependency(%q<rspec>, ["~> 2.0.0"])
|
96
|
-
s.add_dependency(%q<jeweler>, ["~> 1.5.0"])
|
97
|
-
s.add_dependency(%q<rake>, ["~> 0.8.0"])
|
98
|
-
s.add_dependency(%q<rcov>, ["~> 0.9.0"])
|
99
|
-
s.add_dependency(%q<sdoc>, ["~> 0.2.0"])
|
100
|
-
s.add_dependency(%q<rdiscount>, ["~> 1.6.0"])
|
101
|
-
end
|
102
|
-
else
|
103
|
-
s.add_dependency(%q<bundler>, ["~> 1.0"])
|
104
|
-
s.add_dependency(%q<activesupport>, ["~> 3.0.0"])
|
105
|
-
s.add_dependency(%q<rspec>, ["~> 2.0.0"])
|
106
|
-
s.add_dependency(%q<jeweler>, ["~> 1.5.0"])
|
107
|
-
s.add_dependency(%q<rake>, ["~> 0.8.0"])
|
108
|
-
s.add_dependency(%q<rcov>, ["~> 0.9.0"])
|
109
|
-
s.add_dependency(%q<sdoc>, ["~> 0.2.0"])
|
110
|
-
s.add_dependency(%q<rdiscount>, ["~> 1.6.0"])
|
111
|
-
s.add_dependency(%q<bundler>, ["~> 1.0"])
|
112
|
-
s.add_dependency(%q<activesupport>, ["~> 3.0.0"])
|
113
|
-
s.add_dependency(%q<rspec>, ["~> 2.0.0"])
|
114
|
-
s.add_dependency(%q<jeweler>, ["~> 1.5.0"])
|
115
|
-
s.add_dependency(%q<rake>, ["~> 0.8.0"])
|
116
|
-
s.add_dependency(%q<rcov>, ["~> 0.9.0"])
|
117
|
-
s.add_dependency(%q<sdoc>, ["~> 0.2.0"])
|
118
|
-
s.add_dependency(%q<rdiscount>, ["~> 1.6.0"])
|
119
|
-
end
|
120
|
-
end
|
121
|
-
|
@@ -1,200 +0,0 @@
|
|
1
|
-
require 'dionysus'
|
2
|
-
require 'dionysus/version_string'
|
3
|
-
|
4
|
-
##
|
5
|
-
# = Configuration
|
6
|
-
#
|
7
|
-
# require 'dionysus/configuration'
|
8
|
-
#
|
9
|
-
# A simple configuration system that can work in a "hard-keys" or "soft-keys"
|
10
|
-
# mode. In the "hard-keys" mode, the configuration will only allow a specific
|
11
|
-
# set of keys given on initialization. In "soft-keys" mode, the configuration
|
12
|
-
# will accept any keys, at any time, as long as they are valid.
|
13
|
-
#
|
14
|
-
# == Soft Keys Example:
|
15
|
-
#
|
16
|
-
# config = Configuration.new(false, :a_key => 'some value')
|
17
|
-
# config.soft_keys? #=> true
|
18
|
-
# config.hard_keys? #=> false
|
19
|
-
# config.a_key = 'new value'
|
20
|
-
# config.a_key #=> 'new value'
|
21
|
-
# config.another_key = 'some value'
|
22
|
-
# config.anoter_key #=> 'some value'
|
23
|
-
#
|
24
|
-
# == Hard Keys Example:
|
25
|
-
#
|
26
|
-
# config = Configuration.new(:foo, :bar, {:wowsers => 'wow', :foo => 'fooey'})
|
27
|
-
# config.hard_keys? #=> true
|
28
|
-
# config.soft_keys? #=> false
|
29
|
-
# config.a_key #=> raises NoMethodError
|
30
|
-
# config[:a_key] #=> raises ArgumentError
|
31
|
-
# config.foo = 'fooey'
|
32
|
-
# config.bar #=> nil
|
33
|
-
# config.wowsers #=> 'wow'
|
34
|
-
#
|
35
|
-
# == Multi Access
|
36
|
-
#
|
37
|
-
# Values are accessible through the "magic" methods, get() and set() methods,
|
38
|
-
# and hash brackets. These will all give the same result:
|
39
|
-
# config.a_key
|
40
|
-
# config[:a_key]
|
41
|
-
# config['a_key']
|
42
|
-
# config.get(:a_key)
|
43
|
-
# config.get('a_key')
|
44
|
-
#
|
45
|
-
# == Object Keys
|
46
|
-
#
|
47
|
-
# All keys are converted to strings and then to symbols:
|
48
|
-
# o = Object.new
|
49
|
-
# config.set(o, 'foo')
|
50
|
-
# config.get(o) #=> 'foo'
|
51
|
-
# config.keys #=> [:'#<Object:0x190544c>']
|
52
|
-
class Configuration
|
53
|
-
# Regex to define a valid key
|
54
|
-
VALID_KEY = '[A-Za-z0-9\-_]+'
|
55
|
-
|
56
|
-
##
|
57
|
-
# Create the configuration.
|
58
|
-
#
|
59
|
-
# Soft Keys mode:: Pass +false+ as the first argument.
|
60
|
-
# Hard Keys mode:: Default, or pass +true+ as the first argument.
|
61
|
-
def initialize( *args )
|
62
|
-
defaults = (args.last.is_a?(Hash) ? args.pop : {})
|
63
|
-
|
64
|
-
if _hard_keys?(defaults, args)
|
65
|
-
@keys = _normalize_hard_keys(defaults, args)
|
66
|
-
raise ArgumentError, 'Cannot have a hard keys configuration with no keys' if @keys.empty?
|
67
|
-
else
|
68
|
-
@keys = nil
|
69
|
-
raise ArgumentError, 'Cannot define hard keys in soft keys mode' if args.any?
|
70
|
-
end
|
71
|
-
|
72
|
-
_initialize_config(defaults)
|
73
|
-
end
|
74
|
-
|
75
|
-
##
|
76
|
-
# +true+ if the configuration is limited to a specific set of keys.
|
77
|
-
def hard_keys?() !!@keys; end
|
78
|
-
|
79
|
-
##
|
80
|
-
# +true+ if the configuration is NOT limited to a specific set of keys.
|
81
|
-
# (Inverse) of +hard_keys?+
|
82
|
-
def soft_keys?() !@keys; end
|
83
|
-
|
84
|
-
##
|
85
|
-
# The set of keys for the configuration. If this is a hard keys
|
86
|
-
# configuration, it returns the possible keys. Otherwise, it returns the
|
87
|
-
# set keys.
|
88
|
-
def keys() @keys || @config.keys; end
|
89
|
-
|
90
|
-
##
|
91
|
-
# +false+ if there are any values set.
|
92
|
-
def empty?() @config.empty?; end
|
93
|
-
|
94
|
-
##
|
95
|
-
# The number of configurations set.
|
96
|
-
def size() @config.size; end
|
97
|
-
|
98
|
-
##
|
99
|
-
# Set the configuration key to the given value.
|
100
|
-
def set(key, value) validate_key(key); @config[_normalize_key(key)] = value; end
|
101
|
-
alias_method :'[]=', :set
|
102
|
-
|
103
|
-
##
|
104
|
-
# Get the configuration value.
|
105
|
-
def get(key) validate_key(key); @config[_normalize_key(key)]; end
|
106
|
-
alias_method :'[]', :get
|
107
|
-
|
108
|
-
##
|
109
|
-
# Delete the configuration value.
|
110
|
-
def delete(key) @config.delete(key); end
|
111
|
-
|
112
|
-
##
|
113
|
-
# Convert the configuration to a hash.
|
114
|
-
def to_hash() @config.to_hash; end
|
115
|
-
|
116
|
-
##
|
117
|
-
# +true+ if the key is valid.
|
118
|
-
def valid_key?( key )
|
119
|
-
return false if key.blank?
|
120
|
-
key = _normalize_key(key).to_s
|
121
|
-
if RUBY_VERSION.satisfies?('>= 1.9.2')
|
122
|
-
key =~ /^#{VALID_KEY}$/ and !self.methods.include?(key.to_sym)
|
123
|
-
else
|
124
|
-
key =~ /^#{VALID_KEY}$/ and !self.methods.include?(key)
|
125
|
-
end
|
126
|
-
end
|
127
|
-
|
128
|
-
##
|
129
|
-
# +true+ if the key is allowed.
|
130
|
-
def allowed_key?( key )
|
131
|
-
@keys.nil? ? true : @keys.include?(_normalize_key(key))
|
132
|
-
end
|
133
|
-
|
134
|
-
private
|
135
|
-
|
136
|
-
##
|
137
|
-
# If the key is invalid, it raises an error.
|
138
|
-
def validate_key( key )
|
139
|
-
return true if valid_key?(key) and allowed_key?(key)
|
140
|
-
raise ArgumentError, "Invalid key: #{key}"
|
141
|
-
end
|
142
|
-
|
143
|
-
##
|
144
|
-
# Determine if the set of arguments is requesting a hard keys configuration.
|
145
|
-
#
|
146
|
-
# This is a hard key configuration set if
|
147
|
-
# 1) the first argument is NOT false
|
148
|
-
# 2) there are explicit keys defined
|
149
|
-
# 3) there is a default set of arguments
|
150
|
-
def _hard_keys?( defaults, args )
|
151
|
-
if [false, true].include?(args.first)
|
152
|
-
args.shift
|
153
|
-
else
|
154
|
-
args.any? or (defaults and defaults.any?)
|
155
|
-
end
|
156
|
-
end
|
157
|
-
|
158
|
-
##
|
159
|
-
# Normalize the keys and keys from the defaults hash into the hard keys
|
160
|
-
# array.
|
161
|
-
def _normalize_hard_keys( defaults, keys )
|
162
|
-
[].tap do |keys_|
|
163
|
-
(keys + defaults.keys).uniq.collect {|key_| key_.to_s}.sort.each do |key|
|
164
|
-
raise ArgumentError, "Invalid key: '#{key}'" unless valid_key?(key)
|
165
|
-
keys_ << _normalize_key(key)
|
166
|
-
end
|
167
|
-
end.freeze
|
168
|
-
end
|
169
|
-
|
170
|
-
##
|
171
|
-
# Normalize a key by converting it to a string and then a symbol.
|
172
|
-
def _normalize_key( key )
|
173
|
-
key.to_s.to_sym
|
174
|
-
end
|
175
|
-
|
176
|
-
##
|
177
|
-
# Initialize the default config hash
|
178
|
-
def _initialize_config( defaults )
|
179
|
-
@config = {}
|
180
|
-
defaults.each { |key, value| set(key, value) }
|
181
|
-
@config
|
182
|
-
end
|
183
|
-
|
184
|
-
##
|
185
|
-
# The magic. Enables +config.foo+ and +config.foo=+ to map to the getters
|
186
|
-
# and setters.
|
187
|
-
def method_missing( method_name, *args, &block )
|
188
|
-
match = method_name.to_s.match(/^(#{VALID_KEY})(=)??\Z/)
|
189
|
-
key, setter = _normalize_key(match[1]), (match[2] == '=')
|
190
|
-
|
191
|
-
if allowed_key?(key) and !block_given?
|
192
|
-
if setter and args.length == 1
|
193
|
-
return set(key, args.first)
|
194
|
-
elsif key and !setter and args.empty?
|
195
|
-
return get(key)
|
196
|
-
end
|
197
|
-
end
|
198
|
-
super
|
199
|
-
end
|
200
|
-
end
|
data/lib/dionysus/digest.rb
DELETED
@@ -1,135 +0,0 @@
|
|
1
|
-
require 'dionysus'
|
2
|
-
require 'digest'
|
3
|
-
|
4
|
-
##
|
5
|
-
# Convenience methods for the Digest module.
|
6
|
-
#
|
7
|
-
# require 'dionysus/digest'
|
8
|
-
#
|
9
|
-
# The <tt>Digest::DEFAULT_DIGESTS</tt> are automatically registered, if they
|
10
|
-
# exist. You can register additional digests with Digest.register_digest.
|
11
|
-
# The given class must give the digest with the <tt>digest(string)</tt>.
|
12
|
-
#
|
13
|
-
# TODO add digest detection -- by length and by proc on the digests hash
|
14
|
-
module Digest
|
15
|
-
DEFAULT_DIGESTS = [:md5, :sha1, :sha2, :sha256, :sha384, :sha512]
|
16
|
-
@digests = {}
|
17
|
-
|
18
|
-
##
|
19
|
-
# Register a digest. Raises an error if the interpreted class doesn't exist.
|
20
|
-
# It will interpret the klass as <tt>Digest::SYM</tt> if it's <tt>nil</tt>,
|
21
|
-
# and it will run the <tt>digest</tt> method on the klass to determine the
|
22
|
-
# digests bit length if bits is <tt>nil</tt>.
|
23
|
-
#
|
24
|
-
# This will register <tt>:my_digest</tt> and automatically determine the bit
|
25
|
-
# length by executing the class's <tt>digest</tt> method on the string
|
26
|
-
# <tt>'1'</tt>:
|
27
|
-
#
|
28
|
-
# Digest.register_digest!( :my_digest, :klass => MyDigestClass )
|
29
|
-
#
|
30
|
-
# Options:
|
31
|
-
# [klass] The digest class (also can be an arbitrary object). Default:
|
32
|
-
# <tt>Digest::#{sym.to_s.upcase}</tt>
|
33
|
-
# [bit_length] The bit length of the digest. Default: calculated by
|
34
|
-
# running the digest on the string <tt>'1'</tt>.
|
35
|
-
# [method] The calculation method for the digest. Default:
|
36
|
-
# <tt>:digest</tt>
|
37
|
-
def self.register_digest!( sym, options = {} )
|
38
|
-
options = options.with_indifferent_access
|
39
|
-
options[:method] ||= :digest
|
40
|
-
options[:klass] ||= "Digest::#{sym.to_s.upcase}".constantize
|
41
|
-
options[:bit_length] ||= options[:klass].send(options[:method], '1').length * 8
|
42
|
-
@digests[sym.to_sym] = options
|
43
|
-
end
|
44
|
-
|
45
|
-
##
|
46
|
-
# Register a digest. Returns nil if an error occurs.
|
47
|
-
def self.register_digest( sym, options = {} )
|
48
|
-
self.register_digest!(sym, options)
|
49
|
-
rescue LoadError
|
50
|
-
nil
|
51
|
-
end
|
52
|
-
|
53
|
-
##
|
54
|
-
# The hash of registered digests.
|
55
|
-
def self.digests
|
56
|
-
@digests
|
57
|
-
end
|
58
|
-
|
59
|
-
##
|
60
|
-
# The available digests.
|
61
|
-
def self.available_digests
|
62
|
-
self.digests.keys
|
63
|
-
end
|
64
|
-
|
65
|
-
##
|
66
|
-
# The lengths of the registered digests in the given encoding.
|
67
|
-
def self.digest_lengths( encoding = :binary )
|
68
|
-
if encoding == :bit or encoding == 1
|
69
|
-
_digest_lengths(1)
|
70
|
-
elsif encoding.is_a?(Symbol) and String::ENCODING_BITS_PER_CHAR[encoding]
|
71
|
-
_digest_lengths(String::ENCODING_BITS_PER_CHAR[encoding])
|
72
|
-
elsif encoding.is_a?(Integer) and encoding > 0
|
73
|
-
_digest_lengths(encoding)
|
74
|
-
else
|
75
|
-
raise ArgumentError, "Invalid encoding"
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
##
|
80
|
-
# Calculate the given digest of the given string.
|
81
|
-
#
|
82
|
-
# Examples:
|
83
|
-
#
|
84
|
-
# Digest.digest(:sha512, 'foobar') #=> binary digest
|
85
|
-
# Digest.digest(Digest::SHA512, 'foobar') #=> binary digest
|
86
|
-
def self.digest( sym, str )
|
87
|
-
Digest.const_get(sym.to_s.upcase).digest(str)
|
88
|
-
end
|
89
|
-
|
90
|
-
##
|
91
|
-
# Detect the digest of the string. Returns nil if the digest cannot be
|
92
|
-
# determined.
|
93
|
-
#
|
94
|
-
# Example:
|
95
|
-
# Digest.detect_digest("wxeCFXPVXePFcpwuFDjonyn1G/w=", :base64) #=> :sha1
|
96
|
-
# Digest.detect_digest("foobar", :hex) #=> nil
|
97
|
-
def self.detect_digest( string, encoding = :binary )
|
98
|
-
string = string.strip unless encoding == :binary
|
99
|
-
dig = self.digest_lengths(encoding).invert[string.length]
|
100
|
-
dig = :sha256 if dig == :sha2
|
101
|
-
dig
|
102
|
-
end
|
103
|
-
|
104
|
-
##
|
105
|
-
# Detect the digest of the string. Returns nil if the digest cannot be
|
106
|
-
# determined.
|
107
|
-
#
|
108
|
-
# Example:
|
109
|
-
# Digest.detect_digest!("wxeCFXPVXePFcpwuFDjonyn1G/w=", :base64) #=> :sha1
|
110
|
-
# Digest.detect_digest!("foobar", :hex) #=> RuntimeError
|
111
|
-
def self.detect_digest!( string, encoding = :binary )
|
112
|
-
self.detect_digest(string, encoding) or raise("Unknown digest")
|
113
|
-
end
|
114
|
-
|
115
|
-
private
|
116
|
-
|
117
|
-
def self._digest_lengths( bits_per_char ) # :nodoc:
|
118
|
-
padding_factor = (bits_per_char.lcm(8) / bits_per_char)
|
119
|
-
|
120
|
-
{}.tap do |result|
|
121
|
-
self.digests.each do |dig, info|
|
122
|
-
result[dig] = len = info[:bit_length] / bits_per_char
|
123
|
-
if (t_ = len % padding_factor) != 0
|
124
|
-
result[dig] = len + (padding_factor - t_)
|
125
|
-
end
|
126
|
-
end
|
127
|
-
end
|
128
|
-
end
|
129
|
-
end
|
130
|
-
|
131
|
-
# Register some default digests
|
132
|
-
Digest::DEFAULT_DIGESTS.each do |dig|
|
133
|
-
Digest.register_digest(dig)
|
134
|
-
end
|
135
|
-
|