Class: Ppp::Generator
- Inherits:
-
Object
- Object
- Ppp::Generator
- Defined in:
- lib/ppp/generator.rb
Overview
Generates passcodes.
Defined Under Namespace
Classes: NotHexKey
Constant Summary collapse
- @@HEX_PATTERN =
/[a-fA-F0-9]{64}/
Instance Attribute Summary collapse
-
#alphabet ⇒ Object
readonly
Returns the value of attribute alphabet.
-
#length ⇒ Object
readonly
Returns the value of attribute length.
-
#seed ⇒ Object
readonly
Returns the value of attribute seed.
Instance Method Summary collapse
-
#initialize(sha256_key, opts = {}) ⇒ Generator
constructor
A new instance of Generator.
-
#passcode(offset) ⇒ Object
(@see #passcodes).
-
#passcodes(offset, count) ⇒ Array
Creates passcodes seeded off the SHA-256 key this object was created with.
-
#verify(index, given_passcode) ⇒ Boolean
Check if a given passcode is correct.
Constructor Details
#initialize(sha256_key, opts = {}) ⇒ Generator
Returns a new instance of Generator.
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/ppp/generator.rb', line 15 def initialize sha256_key, opts={} raise NotHexKey.new( sha256_key ) if @@HEX_PATTERN.match( sha256_key ).nil? @seed = sha256_key = { :length => 4, :alphabet => :conservative }.merge opts @length = [ :length ] @alphabet = process_alphabet( [ :alphabet ] ).split( '' ).uniq.join raise ArgumentError.new( "alphabet cannot contain null character" ) if alphabet.include? ?\0 end |
Instance Attribute Details
#alphabet ⇒ Object (readonly)
Returns the value of attribute alphabet.
5 6 7 |
# File 'lib/ppp/generator.rb', line 5 def alphabet @alphabet end |
#length ⇒ Object (readonly)
Returns the value of attribute length.
5 6 7 |
# File 'lib/ppp/generator.rb', line 5 def length @length end |
#seed ⇒ Object (readonly)
Returns the value of attribute seed.
5 6 7 |
# File 'lib/ppp/generator.rb', line 5 def seed @seed end |
Instance Method Details
#passcode(offset) ⇒ Object
(@see #passcodes)
38 39 40 |
# File 'lib/ppp/generator.rb', line 38 def passcode offset passcodes( offset, 1 ).first end |
#passcodes(offset, count) ⇒ Array
Creates passcodes seeded off the SHA-256 key this object was created with.
Calling this method subsequent times with the same offset will return the
same passcodes, so you should increase the offset by count each time.
33 34 35 |
# File 'lib/ppp/generator.rb', line 33 def passcodes offset, count Cppp.passcodes @seed, offset, count, @length, @alphabet end |
#verify(index, given_passcode) ⇒ Boolean
Check if a given passcode is correct
45 46 47 |
# File 'lib/ppp/generator.rb', line 45 def verify index, given_passcode passcode( index ) == given_passcode end |