Class: Ppp::Generator

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(sha256_key, opts = {}) ⇒ Generator

Returns a new instance of Generator.

Parameters:

  • sha256_key (String)

    a 64 hex-digit string representation of a SHA-256 hash. This hash will seed all the generated passcodes.

  • opts (Hash) (defaults to: {})

    the options to create the generator with.

Options Hash (opts):

  • :length (Fixnum) — default: 4

    the number of characters in each generated passcode

  • :alphabet (String) — default: :conservative

    a string containing the characters passcodes will be comprised of. Cannot contain null characters and duplicate characters will be removed.

Raises:



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

  options   = { :length => 4, :alphabet => :conservative }.merge opts
  @length   = options[ :length ]
  @alphabet = process_alphabet( options[ :alphabet ] ).split( '' ).uniq.join

  raise ArgumentError.new( "alphabet cannot contain null character" ) if alphabet.include? ?\0
end

Instance Attribute Details

#alphabetObject (readonly)

Returns the value of attribute alphabet.



5
6
7
# File 'lib/ppp/generator.rb', line 5

def alphabet
  @alphabet
end

#lengthObject (readonly)

Returns the value of attribute length.



5
6
7
# File 'lib/ppp/generator.rb', line 5

def length
  @length
end

#seedObject (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.

Parameters:

  • offset (Fixnum)

    the number of passcodes to skip

  • count (Fixnum)

    the number of passcodes to return

Returns:

  • (Array)

    an array of passcodes



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

Parameters:

  • index (Fixnum)

    the index of the passcode to check against

Returns:

  • (Boolean)

    if the given passcode matches the passcode at the given offset



45
46
47
# File 'lib/ppp/generator.rb', line 45

def verify index, given_passcode
  passcode( index ) == given_passcode
end