Class: BaseConvert::Entropia

Inherits:
Number
  • Object
show all
Defined in:
lib/base_convert/entropia.rb

Constant Summary

Constants inherited from Number

Number::INDEXa

Instance Attribute Summary collapse

Attributes inherited from Number

#base, #digits

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Number

infer, #inspect, #to_base, #to_digits, #to_i, #validate?

Methods included from Methods

#chars_ordered?, #toi, #tos

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

#bitsObject (readonly)

Returns the value of attribute bits.



14
15
16
# File 'lib/base_convert/entropia.rb', line 14

def bits
  @bits
end

#entropyObject (readonly)

Returns the value of attribute entropy.



14
15
16
# File 'lib/base_convert/entropia.rb', line 14

def entropy
  @entropy
end

#lengthObject (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_sObject



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