pixiedust 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +7 -0
- data/Manifest.txt +7 -0
- data/README.txt +51 -0
- data/Rakefile +17 -0
- data/lib/pixiedust.rb +77 -0
- data/test/number_dust.rb +26 -0
- data/test/random_dust.rb +29 -0
- metadata +63 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/README.txt
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
PixieDust
|
2
|
+
by Jonathan Wilkins
|
3
|
+
http://pixiedust.rubyforge.org
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
A collection of little utility functions (many security related) for assorted
|
7
|
+
random tasks in Ruby
|
8
|
+
|
9
|
+
Where applicable, they come with a standard version and a class mix in.
|
10
|
+
Ex:
|
11
|
+
NumberDust.nearest(value) is also available as Integer.pd_nearest
|
12
|
+
|
13
|
+
== FEATURES/PROBLEMS:
|
14
|
+
* Get random numbers in arbitrary ranges from OpenSSL (without skew due
|
15
|
+
to modulo)
|
16
|
+
|
17
|
+
== SYNOPSIS:
|
18
|
+
require 'pixiedust'
|
19
|
+
x = RandomDust.between(10,20)
|
20
|
+
|
21
|
+
== REQUIREMENTS:
|
22
|
+
None yet.
|
23
|
+
|
24
|
+
== INSTALL:
|
25
|
+
|
26
|
+
* sudo gem install pixiedust
|
27
|
+
|
28
|
+
== LICENSE:
|
29
|
+
|
30
|
+
(The MIT License)
|
31
|
+
|
32
|
+
Copyright (c) 2008
|
33
|
+
|
34
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
35
|
+
a copy of this software and associated documentation files (the
|
36
|
+
'Software'), to deal in the Software without restriction, including
|
37
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
38
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
39
|
+
permit persons to whom the Software is furnished to do so, subject to
|
40
|
+
the following conditions:
|
41
|
+
|
42
|
+
The above copyright notice and this permission notice shall be
|
43
|
+
included in all copies or substantial portions of the Software.
|
44
|
+
|
45
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
46
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
47
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
48
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
49
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
50
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
51
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'hoe'
|
5
|
+
require './lib/pixiedust.rb'
|
6
|
+
|
7
|
+
Hoe.new('pixiedust', PixieDust::VERSION) do |p|
|
8
|
+
p.rubyforge_name = 'pixiedust'
|
9
|
+
p.author = 'Jonathan Wilkins'
|
10
|
+
p.email = 'jwilkins[at]bitland[dot]net'
|
11
|
+
p.summary = 'A collection of little routines and functions'
|
12
|
+
p.description = p.paragraphs_of('README.txt', 2..5).join("\n\n")
|
13
|
+
p.url = p.paragraphs_of('README.txt', 0).first.split(/\n/)[1..-1]
|
14
|
+
p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
|
15
|
+
end
|
16
|
+
|
17
|
+
# vim: syntax=Ruby
|
data/lib/pixiedust.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
begin
|
2
|
+
require 'openssl'
|
3
|
+
rescue LoadError
|
4
|
+
require 'OpenSSL' # OS X
|
5
|
+
end
|
6
|
+
|
7
|
+
class PixieDust
|
8
|
+
VERSION = '0.0.3'
|
9
|
+
end
|
10
|
+
|
11
|
+
class PDRandOutOfRange < RangeError; end
|
12
|
+
|
13
|
+
class RandomDust
|
14
|
+
# XXX: Do something to make sure that OpenSSL is using a real RNG
|
15
|
+
def between(min, max)
|
16
|
+
RandomDust.between(min, max)
|
17
|
+
end
|
18
|
+
|
19
|
+
def RandomDust.between(min, max)
|
20
|
+
if min > max then raise "min > max in RandomDust.between"; end
|
21
|
+
|
22
|
+
value = 0
|
23
|
+
range = max - min
|
24
|
+
bytes_used = range.size
|
25
|
+
uints = range.size/4
|
26
|
+
|
27
|
+
delta = 2**(8*range.size) - range
|
28
|
+
|
29
|
+
if delta > 16777216 then bytes_used -= 3; unp="C"
|
30
|
+
elsif delta >= 65536 && delta < 16777216 then bytes_used -= 2; unp="S"
|
31
|
+
elsif delta >= 256 && range < 65536 then bytes_used -= 1; unp="SC"
|
32
|
+
else unp=""
|
33
|
+
end
|
34
|
+
|
35
|
+
begin
|
36
|
+
rand = OpenSSL::Random.random_bytes(bytes_used)
|
37
|
+
parts = rand.unpack(unp + "L"*(uints))
|
38
|
+
value = parts.shift
|
39
|
+
parts.length.upto(uints - 1){ |x|
|
40
|
+
value += parts[x] << 32*x
|
41
|
+
}
|
42
|
+
|
43
|
+
value += min
|
44
|
+
|
45
|
+
if value < min || value > max then raise PDRandOutOfRange; end
|
46
|
+
rescue PDRandOutOfRange
|
47
|
+
retry
|
48
|
+
end
|
49
|
+
value
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
class DateDust
|
54
|
+
def DateDust.now_str
|
55
|
+
str = sprintf("%d%02d%02d%02d%02d%02d", Time.now.year, Time.now.month,
|
56
|
+
Time.now.day, Time.now.hour, Time.now.min, Time.now.sec)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
class NumberDust
|
61
|
+
def NumberDust.nearest(val)
|
62
|
+
factor = val.abs.to_s.length - 2
|
63
|
+
case factor
|
64
|
+
when -1..2
|
65
|
+
return val
|
66
|
+
else
|
67
|
+
mult = (10 ** factor)
|
68
|
+
return mult * (val/ mult)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
class Integer
|
74
|
+
def pd_nearest
|
75
|
+
NumberDust.nearest(self)
|
76
|
+
end
|
77
|
+
end
|
data/test/number_dust.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'spec'
|
3
|
+
|
4
|
+
require "#{File.dirname(__FILE__)}/../lib/pixiedust"
|
5
|
+
|
6
|
+
describe NumberDust do
|
7
|
+
it "Shouldn't round small numbers" do
|
8
|
+
val = NumberDust.nearest(0)
|
9
|
+
val.should == 0
|
10
|
+
val = NumberDust.nearest(9)
|
11
|
+
val.should == 9
|
12
|
+
val = NumberDust.nearest(1005)
|
13
|
+
val.should == 1005
|
14
|
+
end
|
15
|
+
|
16
|
+
it "Should round big numbers" do
|
17
|
+
val = NumberDust.nearest(10005)
|
18
|
+
val.should == 10000
|
19
|
+
val = NumberDust.nearest(10805)
|
20
|
+
val.should == 10000
|
21
|
+
val = NumberDust.nearest(11805)
|
22
|
+
val.should == 11000
|
23
|
+
val = NumberDust.nearest(1181805)
|
24
|
+
val.should == 1100000
|
25
|
+
end
|
26
|
+
end
|
data/test/random_dust.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'spec'
|
3
|
+
|
4
|
+
require "#{File.dirname(__FILE__)}/../pixiedust"
|
5
|
+
|
6
|
+
describe RandomDust do
|
7
|
+
it "Shouldn't generate numbers outside of range" do
|
8
|
+
min = -5
|
9
|
+
max = 15
|
10
|
+
100.times {
|
11
|
+
val = RandomDust.between(min,max)
|
12
|
+
val.should <= max
|
13
|
+
val.should >= min
|
14
|
+
}
|
15
|
+
end
|
16
|
+
|
17
|
+
it "Should generate numbers at ends of range" do
|
18
|
+
min = -5
|
19
|
+
max = 5
|
20
|
+
hit_min = false
|
21
|
+
hit_max = false
|
22
|
+
100.times {
|
23
|
+
if RandomDust.between(min, max) == min; then hit_min = true; end
|
24
|
+
if RandomDust.between(min, max) == max; then hit_max = true; end
|
25
|
+
}
|
26
|
+
hit_min.should == true
|
27
|
+
hit_max.should == true
|
28
|
+
end
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.4
|
3
|
+
specification_version: 1
|
4
|
+
name: pixiedust
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.0.3
|
7
|
+
date: 2008-02-27 00:00:00 -08:00
|
8
|
+
summary: A collection of little routines and functions
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: jwilkins[at]bitland[dot]net
|
12
|
+
homepage: " by Jonathan Wilkins"
|
13
|
+
rubyforge_project: pixiedust
|
14
|
+
description: "Where applicable, they come with a standard version and a class mix in. Ex: NumberDust.nearest(value) is also available as Integer.pd_nearest == FEATURES/PROBLEMS: * Get random numbers in arbitrary ranges from OpenSSL (without skew due to modulo) == SYNOPSIS: require 'pixiedust' x = RandomDust.between(10,20) == REQUIREMENTS: None yet."
|
15
|
+
autorequire:
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
post_install_message:
|
29
|
+
authors:
|
30
|
+
- Jonathan Wilkins
|
31
|
+
files:
|
32
|
+
- History.txt
|
33
|
+
- Manifest.txt
|
34
|
+
- README.txt
|
35
|
+
- Rakefile
|
36
|
+
- lib/pixiedust.rb
|
37
|
+
- test/number_dust.rb
|
38
|
+
- test/random_dust.rb
|
39
|
+
test_files: []
|
40
|
+
|
41
|
+
rdoc_options:
|
42
|
+
- --main
|
43
|
+
- README.txt
|
44
|
+
extra_rdoc_files:
|
45
|
+
- History.txt
|
46
|
+
- Manifest.txt
|
47
|
+
- README.txt
|
48
|
+
executables: []
|
49
|
+
|
50
|
+
extensions: []
|
51
|
+
|
52
|
+
requirements: []
|
53
|
+
|
54
|
+
dependencies:
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: hoe
|
57
|
+
version_requirement:
|
58
|
+
version_requirements: !ruby/object:Gem::Version::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 1.4.0
|
63
|
+
version:
|