Module: InsecureRandom

Defined in:
lib/insecure_random.rb,
lib/insecure_random/version.rb

Overview

The InsecureRandom module is the interface for enabling and disabling the ability to seed SecureRandom’s output. Outside of enabling or disabling this ability, there should be no need to call methods on the InsecureRandom module directly. Simply use SecureRandom as you normally would, with the confidence that its output is now repeatable by seeding via Kernel.srand.

Defined Under Namespace

Modules: Hook, Overrides

Constant Summary collapse

VERSION =
Gem::Version.new("2.1.0")

Class Method Summary collapse

Class Method Details

.disableObject

Disables SecureRandom’s repeatable behavior for the duration of the given block, then reliably restores SecureRandom’s original enablement.

Returns the return value of the given block.



79
80
81
82
83
84
# File 'lib/insecure_random.rb', line 79

def self.disable
  toggled = disable!
  yield
ensure
  enable! if toggled
end

.disable!Object

Reverts SecureRandom’s behavior to no longer be repeatable by seeding. Disablement occurs globally and remains disabled until explicity enabled. See: InsecureRandom.enable! above.

Returns true if disabled successfully or false if already disabled.



54
55
56
57
58
59
60
61
62
# File 'lib/insecure_random.rb', line 54

def self.disable!
  return false unless enabled?

  Hook.instance_methods.each do |method|
    Hook.remove_method(method)
  end

  true
end

.disabled?Boolean

Returns whether SecureRandom’s behavior is not currently repeatable.

Returns:

  • (Boolean)


30
31
32
# File 'lib/insecure_random.rb', line 30

def self.disabled?
  !enabled?
end

.enableObject

Enables SecureRandom’s repeatable behavior for the duration of the given block, then reliably restores SecureRandom’s original enablement.

Returns the return value of the given block.



68
69
70
71
72
73
# File 'lib/insecure_random.rb', line 68

def self.enable
  toggled = enable!
  yield
ensure
  disable! if toggled
end

.enable!Object

Change SecureRandom’s behavior to be repeatable by seeding. Enablement occurs globally and remains enabled until explicitly disabled. See: InsecureRandom.disable! below.

Returns true if enabled successfully or false if already enabled.



39
40
41
42
43
44
45
46
47
# File 'lib/insecure_random.rb', line 39

def self.enable!
  return false if enabled?

  Overrides.instance_methods.each do |method|
    Hook.define_method(method, Overrides.instance_method(method))
  end

  true
end

.enabled?Boolean

Returns whether SecureRandom’s behavior is currently repeatable by seeding.

Returns:

  • (Boolean)


25
26
27
# File 'lib/insecure_random.rb', line 25

def self.enabled?
  Hook.instance_methods.any?
end