Class: BaseConvert::Entropia
- Defined in:
- lib/base_convert/entropia.rb
Constant Summary
Constants inherited from Number
Instance Attribute Summary collapse
-
#bits ⇒ Object
readonly
Returns the value of attribute bits.
-
#entropy ⇒ Object
readonly
Returns the value of attribute entropy.
-
#length ⇒ Object
readonly
Returns the value of attribute length.
Attributes inherited from Number
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(counter = nil, base: 95, digits: :P95, validate: true, bits: 256, rng: Random) ⇒ Entropia
constructor
A new instance of Entropia.
- #to_s ⇒ Object
Methods inherited from Number
infer, #inspect, #to_base, #to_digits, #to_i, #validate?
Methods included from Methods
Constructor Details
#initialize(counter = nil, base: 95, digits: :P95, validate: true, bits: 256, rng: Random) ⇒ Entropia
Returns a new instance of Entropia.
15 16 17 18 19 20 |
# File 'lib/base_convert/entropia.rb', line 15 def initialize(counter=nil, base: 95, digits: :P95, validate: true, bits: 256, rng: Random) @bits,@length,@entropy = Entropia.bits_length_entropy(bits,base) counter = rng.random_number(@entropy) unless counter super(counter, base:base, digits:digits, validate:validate) end |
Instance Attribute Details
#bits ⇒ Object (readonly)
Returns the value of attribute bits.
14 15 16 |
# File 'lib/base_convert/entropia.rb', line 14 def bits @bits end |
#entropy ⇒ Object (readonly)
Returns the value of attribute entropy.
14 15 16 |
# File 'lib/base_convert/entropia.rb', line 14 def entropy @entropy end |
#length ⇒ Object (readonly)
Returns the value of attribute length.
14 15 16 |
# File 'lib/base_convert/entropia.rb', line 14 def length @length end |
Class Method Details
.bits_length_entropy(bits_request, base) ⇒ Object
3 4 5 6 7 8 9 10 11 12 |
# File 'lib/base_convert/entropia.rb', line 3 def Entropia.bits_length_entropy(bits_request, base) n,e,entropy = 0,1,2**bits_request while e < entropy n += 1 e *= base end b=Math.log(e,2) # Actual bits, length, and entropy return b,n,e end |
Instance Method Details
#to_s ⇒ Object
22 23 24 25 |
# File 'lib/base_convert/entropia.rb', line 22 def to_s (@length - (string=tos).length).times{string.prepend self.digits[0]} return string end |