Class: VertxAuthCommon::VertxContextPRNG

Inherits:
Object
  • Object
show all
Defined in:
/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/vertx-lang-ruby/target/classes/vertx-auth-common/vertx_context_prng.rb

Overview

A secure non blocking random number generator isolated to the current context. The PRNG is bound to the vert.x context and setup to close when the context shuts down.

When applicable, use of VertxContextPRNG rather than create new PRNG objects is helpful to keep the system entropy usage to the minimum avoiding potential blocking across the application.

The use of VertxContextPRNG is particularly appropriate when multiple handlers use random numbers.

Constant Summary

@@j_api_type =
Object.new

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Boolean) accept?(obj)

Returns:

  • (Boolean)


24
25
26
# File '/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/vertx-lang-ruby/target/classes/vertx-auth-common/vertx_context_prng.rb', line 24

def @@j_api_type.accept?(obj)
  obj.class == VertxContextPRNG
end

+ (::VertxAuthCommon::VertxContextPRNG) current(vertx = nil)

Get or create a secure non blocking random number generator using the current vert.x instance. Since the context might be different this method will attempt to use the current context first if available and then fall back to create a new instance of the PRNG.

Parameters:

Returns:

Raises:

  • (ArgumentError)


44
45
46
47
48
49
50
51
# File '/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/vertx-lang-ruby/target/classes/vertx-auth-common/vertx_context_prng.rb', line 44

def self.current(vertx=nil)
  if !block_given? && vertx == nil
    return ::Vertx::Util::Utils.safe_create(Java::IoVertxExtAuth::VertxContextPRNG.java_method(:current, []).call(),::VertxAuthCommon::VertxContextPRNG)
  elsif vertx.class.method_defined?(:j_del) && !block_given?
    return ::Vertx::Util::Utils.safe_create(Java::IoVertxExtAuth::VertxContextPRNG.java_method(:current, [Java::IoVertxCore::Vertx.java_class]).call(vertx.j_del),::VertxAuthCommon::VertxContextPRNG)
  end
  raise ArgumentError, "Invalid arguments when calling current(#{vertx})"
end

+ (Object) j_api_type



33
34
35
# File '/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/vertx-lang-ruby/target/classes/vertx-auth-common/vertx_context_prng.rb', line 33

def self.j_api_type
  @@j_api_type
end

+ (Object) j_class



36
37
38
# File '/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/vertx-lang-ruby/target/classes/vertx-auth-common/vertx_context_prng.rb', line 36

def self.j_class
  Java::IoVertxExtAuth::VertxContextPRNG.java_class
end

+ (Object) unwrap(obj)



30
31
32
# File '/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/vertx-lang-ruby/target/classes/vertx-auth-common/vertx_context_prng.rb', line 30

def @@j_api_type.unwrap(obj)
  obj.j_del
end

+ (Object) wrap(obj)



27
28
29
# File '/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/vertx-lang-ruby/target/classes/vertx-auth-common/vertx_context_prng.rb', line 27

def @@j_api_type.wrap(obj)
  VertxContextPRNG.new(obj)
end

Instance Method Details

- (Fixnum) next_int(bound = nil)

Returns a secure random int, between 0 (inclusive) and the specified bound (exclusive).

Parameters:

  • bound (Fixnum) (defaults to: nil)
    the upper bound (exclusive), which must be positive.

Returns:

  • (Fixnum)
    random int.

Raises:

  • (ArgumentError)


65
66
67
68
69
70
71
72
# File '/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/vertx-lang-ruby/target/classes/vertx-auth-common/vertx_context_prng.rb', line 65

def next_int(bound=nil)
  if !block_given? && bound == nil
    return @j_del.java_method(:nextInt, []).call()
  elsif bound.class == Fixnum && !block_given?
    return @j_del.java_method(:nextInt, [Java::int.java_class]).call(bound)
  end
  raise ArgumentError, "Invalid arguments when calling next_int(#{bound})"
end

- (String) next_string(length = nil)

Returns a Base64 mime encoded String of random data with the given length. The length parameter refers to the length of the String before the encoding step.

Parameters:

  • length (Fixnum) (defaults to: nil)
    the desired string length before Base64 encoding.

Returns:

  • (String)
    A base 64 encoded string.

Raises:

  • (ArgumentError)


56
57
58
59
60
61
# File '/Users/julien/java/vertx-aggregator/modules/vertx-lang-ruby/vertx-lang-ruby/target/classes/vertx-auth-common/vertx_context_prng.rb', line 56

def next_string(length=nil)
  if length.class == Fixnum && !block_given?
    return @j_del.java_method(:nextString, [Java::int.java_class]).call(length)
  end
  raise ArgumentError, "Invalid arguments when calling next_string(#{length})"
end