-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
153 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
module Budik | ||
class Rng | ||
include Singleton | ||
|
||
def generate(items, method_override = nil) | ||
options = Config.instance.options['rng'] | ||
|
||
if method_override != nil | ||
method = method_override | ||
else | ||
method = options['method'] | ||
end | ||
|
||
case method | ||
when 'hwrng' | ||
hwrng(options['hwrng'], items) | ||
when 'random.org' | ||
random_org(options['random.org'], items) | ||
when 'rand-hwrng-seed' | ||
swrng(items, hwrng(options['hwrng'], 2**64)) | ||
when 'rand' | ||
swrng(items) | ||
else | ||
puts 'Invalid RNG method specified. Using rand() with default seed.' | ||
swrng(items) | ||
end | ||
end | ||
|
||
private | ||
|
||
def hwrng(options, items) | ||
begin | ||
source = File.new(options['source'], 'r') | ||
max = 2**64 | ||
threshold = (max - items) % items | ||
number = source.read(8).unpack('Q') | ||
while number.first < threshold do | ||
number = source.read(8).unpack('Q') | ||
end | ||
number.first % items | ||
rescue | ||
puts "Error: Couldn't obtain random number from #{options['source']}." | ||
puts 'Falling back to rand() with default seed.' | ||
swrng(items) | ||
end | ||
end | ||
|
||
def random_org(options, items) | ||
uri = URI.parse('http://api.random.org/json-rpc/1/invoke') | ||
header = { 'Content-Type' => 'application/json-rpc' } | ||
data = { | ||
jsonrpc: '2.0', | ||
method: 'generateIntegers', | ||
params: { | ||
apiKey: options['apikey'], | ||
n: 1, | ||
min: 0, | ||
max: items | ||
}, | ||
id: 29 | ||
} | ||
|
||
http = Net::HTTP.new(uri.host, uri.port) | ||
request = Net::HTTP::Post.new(uri.request_uri, header) | ||
request.body = data.to_json | ||
|
||
begin | ||
response = http.request(request) | ||
rescue | ||
puts "Error: Couldn't obtain random number from Random.org." | ||
puts 'Falling back to rand() with default seed.' | ||
swrng(items) | ||
end | ||
|
||
if response.code.to_i == 200 | ||
JSON.parse(response.body)['result']['random']['data'].first | ||
else | ||
puts "Error: Couldn't obtain random number from Random.org." | ||
puts "Response code: #{response.code}." | ||
puts "Response body: #{response.body}." | ||
puts 'Falling back to rand() with default seed.' | ||
swrng(items) | ||
end | ||
end | ||
|
||
def swrng(items, seed = nil) | ||
seed == nil ? srand : srand(seed) # TODO: Test this | ||
rand(0...items) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
require 'singleton' | ||
require 'spec_helper' | ||
|
||
require 'budik/rng' | ||
|
||
describe Budik::Rng, '#generate' do | ||
context 'using hwrng' do | ||
it 'generates random number' do | ||
config = Budik::Config.instance | ||
config.load(Hash.new) | ||
|
||
config.options['rng']['hwrng']['source'] = '/dev/hwrng' | ||
5.times do | ||
num = Budik::Rng.instance.generate(100, 'hwrng') | ||
expect(num >= 0 && num <= 100).to eq true | ||
end | ||
|
||
config.options['rng']['hwrng']['source'] = '/dev/random' | ||
5.times do | ||
num = Budik::Rng.instance.generate(100, 'hwrng') | ||
expect(num >= 0 && num <= 100).to eq true | ||
end | ||
|
||
config.options['rng']['hwrng']['source'] = '/dev/urandom' | ||
100.times do | ||
num = Budik::Rng.instance.generate(100, 'hwrng') | ||
expect(num >= 0 && num <= 100).to eq true | ||
end | ||
end | ||
end | ||
|
||
context 'using random.org' do | ||
it 'generates random number' do | ||
config = Budik::Config.instance | ||
config.load(Hash.new) | ||
|
||
if config.options['rng']['random.org']['apikey'] | ||
num = Budik::Rng.instance.generate(100, 'random.org') | ||
expect(num >= 0 && num <= 100).to eq true | ||
end | ||
end | ||
end | ||
|
||
context 'using srand' do | ||
it 'generates random number' do | ||
100.times do | ||
num = Budik::Rng.instance.generate(100, 'rand') | ||
expect(num >= 0 && num <= 100).to eq true | ||
end | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
#require 'r18n-core' | ||
require 'fileutils' | ||
require 'singleton' | ||
require 'spec_helper' | ||
|