rack_csrf 2.4.0 → 2.5.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.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/.travis.yml +15 -0
- data/Changelog.md +12 -0
- data/Gemfile +2 -12
- data/LICENSE.rdoc +1 -1
- data/README.rdoc +10 -6
- data/Rakefile +10 -32
- data/examples/camping/Gemfile +2 -0
- data/examples/camping/app.rb +0 -2
- data/examples/cuba/Gemfile +1 -0
- data/examples/cuba/app.rb +2 -0
- data/examples/cuba/config-with-raise.ru +1 -2
- data/examples/cuba/config.ru +1 -2
- data/examples/innate/Gemfile +1 -0
- data/examples/innate/start-with-raise.rb +1 -3
- data/examples/innate/start.rb +1 -3
- data/examples/rack/Gemfile +2 -1
- data/examples/rack/app.rb +8 -3
- data/examples/rack/config-with-raise.ru +1 -2
- data/examples/rack/config.ru +1 -2
- data/examples/sinatra/Gemfile +2 -1
- data/examples/sinatra/config-with-raise.ru +1 -4
- data/examples/sinatra/config.ru +1 -4
- data/features/step_definitions/setup_steps.rb +11 -7
- data/lib/rack/csrf.rb +3 -8
- data/lib/rack/csrf/version.rb +5 -0
- data/rack_csrf.gemspec +41 -112
- data/spec/csrf_spec.rb +6 -6
- data/spec/spec_helper.rb +7 -0
- metadata +164 -143
- data/VERSION +0 -1
- data/lib/rack/vendor/securerandom.rb +0 -256
data/spec/csrf_spec.rb
CHANGED
@@ -185,7 +185,7 @@ describe Rack::Csrf do
|
|
185
185
|
let(:csrf) { Rack::Csrf.new nil }
|
186
186
|
|
187
187
|
it 'should run the check' do
|
188
|
-
csrf.send(:skip_checking, request).should
|
188
|
+
csrf.send(:skip_checking, request).should be false
|
189
189
|
end
|
190
190
|
end
|
191
191
|
|
@@ -193,7 +193,7 @@ describe Rack::Csrf do
|
|
193
193
|
let(:csrf) { Rack::Csrf.new nil, :skip => ['POST:/hello'] }
|
194
194
|
|
195
195
|
it 'should not run the check' do
|
196
|
-
csrf.send(:skip_checking, request).should
|
196
|
+
csrf.send(:skip_checking, request).should be true
|
197
197
|
end
|
198
198
|
end
|
199
199
|
|
@@ -202,7 +202,7 @@ describe Rack::Csrf do
|
|
202
202
|
let(:csrf) { Rack::Csrf.new nil, :skip_if => lambda { |req| req.env.key?('HTTP_X_VERY_SPECIAL_HEADER') } }
|
203
203
|
|
204
204
|
it 'should not run the check' do
|
205
|
-
csrf.send(:skip_checking, request).should
|
205
|
+
csrf.send(:skip_checking, request).should be true
|
206
206
|
end
|
207
207
|
end
|
208
208
|
|
@@ -211,7 +211,7 @@ describe Rack::Csrf do
|
|
211
211
|
let(:csrf) { Rack::Csrf.new nil, :check_only => [] }
|
212
212
|
|
213
213
|
it 'should run the check' do
|
214
|
-
csrf.send(:skip_checking, request).should
|
214
|
+
csrf.send(:skip_checking, request).should be false
|
215
215
|
end
|
216
216
|
end
|
217
217
|
|
@@ -220,7 +220,7 @@ describe Rack::Csrf do
|
|
220
220
|
let(:csrf) { Rack::Csrf.new nil, :check_only => ['POST:/hello'] }
|
221
221
|
|
222
222
|
it 'should run the check' do
|
223
|
-
csrf.send(:skip_checking, request).should
|
223
|
+
csrf.send(:skip_checking, request).should be false
|
224
224
|
end
|
225
225
|
end
|
226
226
|
|
@@ -228,7 +228,7 @@ describe Rack::Csrf do
|
|
228
228
|
let(:csrf) { Rack::Csrf.new nil, :check_only => ['POST:/ciao'] }
|
229
229
|
|
230
230
|
it 'should not run the check' do
|
231
|
-
csrf.send(:skip_checking, request).should
|
231
|
+
csrf.send(:skip_checking, request).should be true
|
232
232
|
end
|
233
233
|
end
|
234
234
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,146 +1,158 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack_csrf
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
6
|
-
segments:
|
7
|
-
- 2
|
8
|
-
- 4
|
9
|
-
- 0
|
10
|
-
version: 2.4.0
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.5.0
|
11
5
|
platform: ruby
|
12
|
-
authors:
|
6
|
+
authors:
|
13
7
|
- Emanuele Vicentini
|
14
8
|
autorequire:
|
15
9
|
bindir: bin
|
16
10
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
- !ruby/object:Gem::Dependency
|
21
|
-
prerelease: false
|
22
|
-
type: :runtime
|
23
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
-
none: false
|
25
|
-
requirements:
|
26
|
-
- - ">="
|
27
|
-
- !ruby/object:Gem::Version
|
28
|
-
hash: 25
|
29
|
-
segments:
|
30
|
-
- 0
|
31
|
-
- 9
|
32
|
-
version: "0.9"
|
33
|
-
version_requirements: *id001
|
11
|
+
date: 2014-06-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
34
14
|
name: rack
|
35
|
-
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.1.0
|
20
|
+
type: :runtime
|
36
21
|
prerelease: false
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
hash: 23
|
44
|
-
segments:
|
45
|
-
- 1
|
46
|
-
- 0
|
47
|
-
- 0
|
48
|
-
version: 1.0.0
|
49
|
-
version_requirements: *id002
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.1.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
50
28
|
name: bundler
|
51
|
-
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.0.0
|
34
|
+
type: :development
|
52
35
|
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.0.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
53
48
|
type: :development
|
54
|
-
requirement: &id003 !ruby/object:Gem::Requirement
|
55
|
-
none: false
|
56
|
-
requirements:
|
57
|
-
- - ">="
|
58
|
-
- !ruby/object:Gem::Version
|
59
|
-
hash: 17
|
60
|
-
segments:
|
61
|
-
- 1
|
62
|
-
- 1
|
63
|
-
- 1
|
64
|
-
version: 1.1.1
|
65
|
-
version_requirements: *id003
|
66
|
-
name: cucumber
|
67
|
-
- !ruby/object:Gem::Dependency
|
68
49
|
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: cucumber
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.1.1
|
69
62
|
type: :development
|
70
|
-
requirement: &id004 !ruby/object:Gem::Requirement
|
71
|
-
none: false
|
72
|
-
requirements:
|
73
|
-
- - ">="
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
hash: 3
|
76
|
-
segments:
|
77
|
-
- 0
|
78
|
-
version: "0"
|
79
|
-
version_requirements: *id004
|
80
|
-
name: rack-test
|
81
|
-
- !ruby/object:Gem::Dependency
|
82
63
|
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 1.1.1
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rack-test
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
83
76
|
type: :development
|
84
|
-
|
85
|
-
|
86
|
-
requirements:
|
87
|
-
- - ">="
|
88
|
-
- !ruby/object:Gem::Version
|
89
|
-
|
90
|
-
|
91
|
-
- 2
|
92
|
-
- 0
|
93
|
-
- 0
|
94
|
-
version: 2.0.0
|
95
|
-
version_requirements: *id005
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
96
84
|
name: rspec
|
97
|
-
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.0'
|
90
|
+
type: :development
|
98
91
|
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rspec-collection_matchers
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
99
104
|
type: :development
|
100
|
-
|
101
|
-
|
102
|
-
requirements:
|
103
|
-
- - ">="
|
104
|
-
- !ruby/object:Gem::Version
|
105
|
-
|
106
|
-
|
107
|
-
- 2
|
108
|
-
- 4
|
109
|
-
- 2
|
110
|
-
version: 2.4.2
|
111
|
-
version_requirements: *id006
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
112
|
name: rdoc
|
113
|
-
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 2.4.2
|
118
|
+
type: :development
|
114
119
|
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 2.4.2
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: git
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: 1.2.5
|
115
132
|
type: :development
|
116
|
-
|
117
|
-
|
118
|
-
requirements:
|
119
|
-
- - ">="
|
120
|
-
- !ruby/object:Gem::Version
|
121
|
-
|
122
|
-
segments:
|
123
|
-
- 0
|
124
|
-
version: "0"
|
125
|
-
version_requirements: *id007
|
126
|
-
name: jeweler
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: 1.2.5
|
127
139
|
description: Anti-CSRF Rack middleware
|
128
|
-
email:
|
140
|
+
email:
|
141
|
+
- emanuele.vicentini@gmail.com
|
129
142
|
executables: []
|
130
|
-
|
131
143
|
extensions: []
|
132
|
-
|
133
|
-
extra_rdoc_files:
|
144
|
+
extra_rdoc_files:
|
134
145
|
- LICENSE.rdoc
|
135
146
|
- README.rdoc
|
136
|
-
files:
|
137
|
-
- .
|
147
|
+
files:
|
148
|
+
- ".gitignore"
|
149
|
+
- ".rspec"
|
150
|
+
- ".travis.yml"
|
138
151
|
- Changelog.md
|
139
152
|
- Gemfile
|
140
153
|
- LICENSE.rdoc
|
141
154
|
- README.rdoc
|
142
155
|
- Rakefile
|
143
|
-
- VERSION
|
144
156
|
- cucumber.yml
|
145
157
|
- examples/camping/Gemfile
|
146
158
|
- examples/camping/README.rdoc
|
@@ -192,47 +204,56 @@ files:
|
|
192
204
|
- features/variation_on_header_name.feature
|
193
205
|
- features/variation_on_key_name.feature
|
194
206
|
- lib/rack/csrf.rb
|
195
|
-
- lib/rack/
|
207
|
+
- lib/rack/csrf/version.rb
|
196
208
|
- rack_csrf.gemspec
|
197
209
|
- spec/csrf_spec.rb
|
198
210
|
- spec/spec_helper.rb
|
199
211
|
homepage: https://github.com/baldowl/rack_csrf
|
200
|
-
licenses:
|
212
|
+
licenses:
|
201
213
|
- MIT
|
214
|
+
metadata: {}
|
202
215
|
post_install_message:
|
203
|
-
rdoc_options:
|
204
|
-
- --line-numbers
|
205
|
-
- --inline-source
|
206
|
-
- --title
|
207
|
-
- Rack::Csrf 2.
|
208
|
-
- --main
|
216
|
+
rdoc_options:
|
217
|
+
- "--line-numbers"
|
218
|
+
- "--inline-source"
|
219
|
+
- "--title"
|
220
|
+
- Rack::Csrf 2.5.0
|
221
|
+
- "--main"
|
209
222
|
- README.rdoc
|
210
|
-
require_paths:
|
223
|
+
require_paths:
|
211
224
|
- lib
|
212
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
213
|
-
|
214
|
-
requirements:
|
225
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
226
|
+
requirements:
|
215
227
|
- - ">="
|
216
|
-
- !ruby/object:Gem::Version
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
version: "0"
|
221
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
222
|
-
none: false
|
223
|
-
requirements:
|
228
|
+
- !ruby/object:Gem::Version
|
229
|
+
version: 1.8.7
|
230
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
231
|
+
requirements:
|
224
232
|
- - ">="
|
225
|
-
- !ruby/object:Gem::Version
|
226
|
-
|
227
|
-
segments:
|
228
|
-
- 0
|
229
|
-
version: "0"
|
233
|
+
- !ruby/object:Gem::Version
|
234
|
+
version: '0'
|
230
235
|
requirements: []
|
231
|
-
|
232
|
-
|
233
|
-
rubygems_version: 1.8.17
|
236
|
+
rubyforge_project:
|
237
|
+
rubygems_version: 2.3.0
|
234
238
|
signing_key:
|
235
|
-
specification_version:
|
239
|
+
specification_version: 4
|
236
240
|
summary: Anti-CSRF Rack middleware
|
237
|
-
test_files:
|
238
|
-
|
241
|
+
test_files:
|
242
|
+
- features/check_only_some_specific_requests.feature
|
243
|
+
- features/custom_http_methods.feature
|
244
|
+
- features/empty_responses.feature
|
245
|
+
- features/inspecting_also_get_requests.feature
|
246
|
+
- features/raising_exception.feature
|
247
|
+
- features/setup.feature
|
248
|
+
- features/skip_if_block_passes.feature
|
249
|
+
- features/skip_some_routes.feature
|
250
|
+
- features/step_definitions/request_steps.rb
|
251
|
+
- features/step_definitions/response_steps.rb
|
252
|
+
- features/step_definitions/setup_steps.rb
|
253
|
+
- features/support/env.rb
|
254
|
+
- features/support/fake_session.rb
|
255
|
+
- features/variation_on_field_name.feature
|
256
|
+
- features/variation_on_header_name.feature
|
257
|
+
- features/variation_on_key_name.feature
|
258
|
+
- spec/csrf_spec.rb
|
259
|
+
- spec/spec_helper.rb
|
data/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
2.4.0
|
@@ -1,256 +0,0 @@
|
|
1
|
-
# Library taken from Ruby 1.9 SVN repository on 2009-04-15T10:25Z
|
2
|
-
# For copyright and license see http://www.ruby-lang.org
|
3
|
-
|
4
|
-
# = Secure random number generator interface.
|
5
|
-
#
|
6
|
-
# This library is an interface for secure random number generator which is
|
7
|
-
# suitable for generating session key in HTTP cookies, etc.
|
8
|
-
#
|
9
|
-
# It supports following secure random number generators.
|
10
|
-
#
|
11
|
-
# * openssl
|
12
|
-
# * /dev/urandom
|
13
|
-
# * Win32
|
14
|
-
#
|
15
|
-
# == Example
|
16
|
-
#
|
17
|
-
# # random hexadecimal string.
|
18
|
-
# p SecureRandom.hex(10) #=> "52750b30ffbc7de3b362"
|
19
|
-
# p SecureRandom.hex(10) #=> "92b15d6c8dc4beb5f559"
|
20
|
-
# p SecureRandom.hex(11) #=> "6aca1b5c58e4863e6b81b8"
|
21
|
-
# p SecureRandom.hex(12) #=> "94b2fff3e7fd9b9c391a2306"
|
22
|
-
# p SecureRandom.hex(13) #=> "39b290146bea6ce975c37cfc23"
|
23
|
-
# ...
|
24
|
-
#
|
25
|
-
# # random base64 string.
|
26
|
-
# p SecureRandom.base64(10) #=> "EcmTPZwWRAozdA=="
|
27
|
-
# p SecureRandom.base64(10) #=> "9b0nsevdwNuM/w=="
|
28
|
-
# p SecureRandom.base64(10) #=> "KO1nIU+p9DKxGg=="
|
29
|
-
# p SecureRandom.base64(11) #=> "l7XEiFja+8EKEtY="
|
30
|
-
# p SecureRandom.base64(12) #=> "7kJSM/MzBJI+75j8"
|
31
|
-
# p SecureRandom.base64(13) #=> "vKLJ0tXBHqQOuIcSIg=="
|
32
|
-
# ...
|
33
|
-
#
|
34
|
-
# # random binary string.
|
35
|
-
# p SecureRandom.random_bytes(10) #=> "\016\t{\370g\310pbr\301"
|
36
|
-
# p SecureRandom.random_bytes(10) #=> "\323U\030TO\234\357\020\a\337"
|
37
|
-
# ...
|
38
|
-
|
39
|
-
begin
|
40
|
-
require 'openssl'
|
41
|
-
rescue LoadError
|
42
|
-
end
|
43
|
-
|
44
|
-
module SecureRandom
|
45
|
-
# SecureRandom.random_bytes generates a random binary string.
|
46
|
-
#
|
47
|
-
# The argument n specifies the length of the result string.
|
48
|
-
#
|
49
|
-
# If n is not specified, 16 is assumed.
|
50
|
-
# It may be larger in future.
|
51
|
-
#
|
52
|
-
# The result may contain any byte: "\x00" - "\xff".
|
53
|
-
#
|
54
|
-
# p SecureRandom.random_bytes #=> "\xD8\\\xE0\xF4\r\xB2\xFC*WM\xFF\x83\x18\xF45\xB6"
|
55
|
-
# p SecureRandom.random_bytes #=> "m\xDC\xFC/\a\x00Uf\xB2\xB2P\xBD\xFF6S\x97"
|
56
|
-
#
|
57
|
-
# If secure random number generator is not available,
|
58
|
-
# NotImplementedError is raised.
|
59
|
-
def self.random_bytes(n=nil)
|
60
|
-
n ||= 16
|
61
|
-
|
62
|
-
if defined? OpenSSL::Random
|
63
|
-
return OpenSSL::Random.random_bytes(n)
|
64
|
-
end
|
65
|
-
|
66
|
-
if !defined?(@has_urandom) || @has_urandom
|
67
|
-
flags = File::RDONLY
|
68
|
-
flags |= File::NONBLOCK if defined? File::NONBLOCK
|
69
|
-
flags |= File::NOCTTY if defined? File::NOCTTY
|
70
|
-
flags |= File::NOFOLLOW if defined? File::NOFOLLOW
|
71
|
-
begin
|
72
|
-
File.open("/dev/urandom", flags) {|f|
|
73
|
-
unless f.stat.chardev?
|
74
|
-
raise Errno::ENOENT
|
75
|
-
end
|
76
|
-
@has_urandom = true
|
77
|
-
ret = f.readpartial(n)
|
78
|
-
if ret.length != n
|
79
|
-
raise NotImplementedError, "Unexpected partial read from random device"
|
80
|
-
end
|
81
|
-
return ret
|
82
|
-
}
|
83
|
-
rescue Errno::ENOENT
|
84
|
-
@has_urandom = false
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
if !defined?(@has_win32)
|
89
|
-
begin
|
90
|
-
require 'Win32API'
|
91
|
-
|
92
|
-
crypt_acquire_context = Win32API.new("advapi32", "CryptAcquireContext", 'PPPII', 'L')
|
93
|
-
@crypt_gen_random = Win32API.new("advapi32", "CryptGenRandom", 'LIP', 'L')
|
94
|
-
|
95
|
-
hProvStr = " " * 4
|
96
|
-
prov_rsa_full = 1
|
97
|
-
crypt_verifycontext = 0xF0000000
|
98
|
-
|
99
|
-
if crypt_acquire_context.call(hProvStr, nil, nil, prov_rsa_full, crypt_verifycontext) == 0
|
100
|
-
raise SystemCallError, "CryptAcquireContext failed: #{lastWin32ErrorMessage}"
|
101
|
-
end
|
102
|
-
@hProv, = hProvStr.unpack('L')
|
103
|
-
|
104
|
-
@has_win32 = true
|
105
|
-
rescue LoadError
|
106
|
-
@has_win32 = false
|
107
|
-
end
|
108
|
-
end
|
109
|
-
if @has_win32
|
110
|
-
bytes = " " * n
|
111
|
-
if @crypt_gen_random.call(@hProv, bytes.size, bytes) == 0
|
112
|
-
raise SystemCallError, "CryptGenRandom failed: #{lastWin32ErrorMessage}"
|
113
|
-
end
|
114
|
-
return bytes
|
115
|
-
end
|
116
|
-
|
117
|
-
raise NotImplementedError, "No random device"
|
118
|
-
end
|
119
|
-
|
120
|
-
# SecureRandom.hex generates a random hex string.
|
121
|
-
#
|
122
|
-
# The argument n specifies the length of the random length.
|
123
|
-
# The length of the result string is twice of n.
|
124
|
-
#
|
125
|
-
# If n is not specified, 16 is assumed.
|
126
|
-
# It may be larger in future.
|
127
|
-
#
|
128
|
-
# The result may contain 0-9 and a-f.
|
129
|
-
#
|
130
|
-
# p SecureRandom.hex #=> "eb693ec8252cd630102fd0d0fb7c3485"
|
131
|
-
# p SecureRandom.hex #=> "91dc3bfb4de5b11d029d376634589b61"
|
132
|
-
#
|
133
|
-
# If secure random number generator is not available,
|
134
|
-
# NotImplementedError is raised.
|
135
|
-
def self.hex(n=nil)
|
136
|
-
random_bytes(n).unpack("H*")[0]
|
137
|
-
end
|
138
|
-
|
139
|
-
# SecureRandom.base64 generates a random base64 string.
|
140
|
-
#
|
141
|
-
# The argument n specifies the length of the random length.
|
142
|
-
# The length of the result string is about 4/3 of n.
|
143
|
-
#
|
144
|
-
# If n is not specified, 16 is assumed.
|
145
|
-
# It may be larger in future.
|
146
|
-
#
|
147
|
-
# The result may contain A-Z, a-z, 0-9, "+", "/" and "=".
|
148
|
-
#
|
149
|
-
# p SecureRandom.base64 #=> "/2BuBuLf3+WfSKyQbRcc/A=="
|
150
|
-
# p SecureRandom.base64 #=> "6BbW0pxO0YENxn38HMUbcQ=="
|
151
|
-
#
|
152
|
-
# If secure random number generator is not available,
|
153
|
-
# NotImplementedError is raised.
|
154
|
-
#
|
155
|
-
# See RFC 3548 for base64.
|
156
|
-
def self.base64(n=nil)
|
157
|
-
[random_bytes(n)].pack("m*").delete("\n")
|
158
|
-
end
|
159
|
-
|
160
|
-
# SecureRandom.urlsafe_base64 generates a random URL-safe base64 string.
|
161
|
-
#
|
162
|
-
# The argument _n_ specifies the length of the random length.
|
163
|
-
# The length of the result string is about 4/3 of _n_.
|
164
|
-
#
|
165
|
-
# If _n_ is not specified, 16 is assumed.
|
166
|
-
# It may be larger in future.
|
167
|
-
#
|
168
|
-
# The boolean argument _padding_ specifies the padding.
|
169
|
-
# If it is false or nil, padding is not generated.
|
170
|
-
# Otherwise padding is generated.
|
171
|
-
# By default, padding is not generated because "=" may be used as a URL delimiter.
|
172
|
-
#
|
173
|
-
# The result may contain A-Z, a-z, 0-9, "-" and "_".
|
174
|
-
# "=" is also used if _padding_ is true.
|
175
|
-
#
|
176
|
-
# p SecureRandom.urlsafe_base64 #=> "b4GOKm4pOYU_-BOXcrUGDg"
|
177
|
-
# p SecureRandom.urlsafe_base64 #=> "UZLdOkzop70Ddx-IJR0ABg"
|
178
|
-
#
|
179
|
-
# p SecureRandom.urlsafe_base64(nil, true) #=> "i0XQ-7gglIsHGV2_BNPrdQ=="
|
180
|
-
# p SecureRandom.urlsafe_base64(nil, true) #=> "-M8rLhr7JEpJlqFGUMmOxg=="
|
181
|
-
#
|
182
|
-
# If secure random number generator is not available,
|
183
|
-
# NotImplementedError is raised.
|
184
|
-
#
|
185
|
-
# See RFC 3548 for URL-safe base64.
|
186
|
-
def self.urlsafe_base64(n=nil, padding=false)
|
187
|
-
s = [random_bytes(n)].pack("m*")
|
188
|
-
s.delete!("\n")
|
189
|
-
s.tr!("+/", "-_")
|
190
|
-
s.delete!("=") if !padding
|
191
|
-
s
|
192
|
-
end
|
193
|
-
|
194
|
-
# SecureRandom.random_number generates a random number.
|
195
|
-
#
|
196
|
-
# If an positive integer is given as n,
|
197
|
-
# SecureRandom.random_number returns an integer:
|
198
|
-
# 0 <= SecureRandom.random_number(n) < n.
|
199
|
-
#
|
200
|
-
# p SecureRandom.random_number(100) #=> 15
|
201
|
-
# p SecureRandom.random_number(100) #=> 88
|
202
|
-
#
|
203
|
-
# If 0 is given or an argument is not given,
|
204
|
-
# SecureRandom.random_number returns an float:
|
205
|
-
# 0.0 <= SecureRandom.random_number() < 1.0.
|
206
|
-
#
|
207
|
-
# p SecureRandom.random_number #=> 0.596506046187744
|
208
|
-
# p SecureRandom.random_number #=> 0.350621695741409
|
209
|
-
#
|
210
|
-
def self.random_number(n=0)
|
211
|
-
if 0 < n
|
212
|
-
hex = n.to_s(16)
|
213
|
-
hex = '0' + hex if (hex.length & 1) == 1
|
214
|
-
bin = [hex].pack("H*")
|
215
|
-
mask = bin[0].ord
|
216
|
-
mask |= mask >> 1
|
217
|
-
mask |= mask >> 2
|
218
|
-
mask |= mask >> 4
|
219
|
-
begin
|
220
|
-
rnd = SecureRandom.random_bytes(bin.length)
|
221
|
-
rnd[0] = (rnd[0].ord & mask).chr
|
222
|
-
end until rnd < bin
|
223
|
-
rnd.unpack("H*")[0].hex
|
224
|
-
else
|
225
|
-
# assumption: Float::MANT_DIG <= 64
|
226
|
-
i64 = SecureRandom.random_bytes(8).unpack("Q")[0]
|
227
|
-
Math.ldexp(i64 >> (64-Float::MANT_DIG), -Float::MANT_DIG)
|
228
|
-
end
|
229
|
-
end
|
230
|
-
|
231
|
-
# SecureRandom.uuid generates a v4 random UUID (Universally Unique IDentifier).
|
232
|
-
#
|
233
|
-
# p SecureRandom.uuid #=> "2d931510-d99f-494a-8c67-87feb05e1594"
|
234
|
-
# p SecureRandom.uuid #=> "62936e70-1815-439b-bf89-8492855a7e6b"
|
235
|
-
#
|
236
|
-
# See RFC 4122 for UUID.
|
237
|
-
def self.uuid
|
238
|
-
ary = self.random_bytes(16).unpack("NnnnnN")
|
239
|
-
ary[2] = (ary[2] & 0x0fff) | 0x4000
|
240
|
-
ary[3] = (ary[3] & 0x3fff) | 0x8000
|
241
|
-
"%08x-%04x-%04x-%04x-%04x%08x" % ary
|
242
|
-
end
|
243
|
-
|
244
|
-
# Following code is based on David Garamond's GUID library for Ruby.
|
245
|
-
def self.lastWin32ErrorMessage # :nodoc:
|
246
|
-
get_last_error = Win32API.new("kernel32", "GetLastError", '', 'L')
|
247
|
-
format_message = Win32API.new("kernel32", "FormatMessageA", 'LPLLPLPPPPPPPP', 'L')
|
248
|
-
format_message_ignore_inserts = 0x00000200
|
249
|
-
format_message_from_system = 0x00001000
|
250
|
-
|
251
|
-
code = get_last_error.call
|
252
|
-
msg = "\0" * 1024
|
253
|
-
len = format_message.call(format_message_ignore_inserts + format_message_from_system, 0, code, 0, msg, 1024, nil, nil, nil, nil, nil, nil, nil, nil)
|
254
|
-
msg[0, len].tr("\r", '').chomp
|
255
|
-
end
|
256
|
-
end
|