class OTP::Base
Constants
- DEFAULT_ALGORITHM
- DEFAULT_DIGITS
Attributes
accountname[RW]
algorithm[RW]
digits[RW]
issuer[RW]
secret[RW]
Public Class Methods
new(secret=nil, algorithm=nil, digits=nil)
click to toggle source
# File lib/otp/base.rb, line 15 def initialize(secret=nil, algorithm=nil, digits=nil) self.secret = secret self.algorithm = algorithm || DEFAULT_ALGORITHM self.digits = digits || DEFAULT_DIGITS end
Public Instance Methods
extract_uri_params(params)
click to toggle source
# File lib/otp/base.rb, line 61 def extract_uri_params(params) self.secret = params["secret"] self.issuer = issuer || params["issuer"] self.algorithm = params["algorithm"] || algorithm self.digits = (params["digits"] || digits).to_i end
moving_factor()
click to toggle source
# File lib/otp/base.rb, line 33 def moving_factor raise NotImplementedError end
new_secret(num_bytes=10)
click to toggle source
# File lib/otp/base.rb, line 21 def new_secret(num_bytes=10) self.raw_secret = OpenSSL::Random.random_bytes(num_bytes) end
password(generation=0)
click to toggle source
# File lib/otp/base.rb, line 37 def password(generation=0) return otp(algorithm, raw_secret, moving_factor+generation, digits) end
raw_secret()
click to toggle source
# File lib/otp/base.rb, line 29 def raw_secret return OTP::Base32.decode(secret) end
raw_secret=(bytes)
click to toggle source
# File lib/otp/base.rb, line 25 def raw_secret=(bytes) self.secret = OTP::Base32.encode(bytes) end
to_uri()
click to toggle source
# File lib/otp/base.rb, line 48 def to_uri return OTP::URI.format(self) end
uri_params()
click to toggle source
# File lib/otp/base.rb, line 52 def uri_params params = {} params[:secret] = secret params[:issuer] = issuer if issuer params[:algorithm] = algorithm if algorithm != DEFAULT_ALGORITHM params[:digits] = digits if digits != DEFAULT_DIGITS return params end
verify(given_pw, last:0, post:0)
click to toggle source
# File lib/otp/base.rb, line 41 def verify(given_pw, last:0, post:0) raise ArgumentError, "last must be greater than or equal to 0" if last < 0 raise ArgumentError, "post must be greater than or equal to 0" if post < 0 return false if given_pw.nil? || given_pw.empty? return (-last..post).any?{|i| otp_compare(password(i), given_pw) } end