Sha256: 41e288a0163cf5d49c61235ce2a11cef1f2d38bb6a8a42c3ebe0aec74b90964f

Contents?: true

Size: 1010 Bytes

Versions: 1

Compression:

Stored size: 1010 Bytes

Contents

require 'digest/md5'
require 'base64'

module Oplop
  class V1
    attr_reader :master, :label, :length

    LEGACY_LENGTH = 8
    DEFAULT_LENGTH = 16

    def self.password(args={})
      self.new(args).password
    end

    def initialize(args={})
      @master = args[:master]
      @label = args[:label]
      @length = args[:length] || DEFAULT_LENGTH

      if @label =~ /^([0-9]*)?\*/
        (@length, @label) = @label.split('*')
        @length = length.to_i
        @length = LEGACY_LENGTH if length <= 0
      end
    end

    def master_label
      @master_label ||= '%s%s' % [ master, label ]
    end

    def password
      password = Base64.urlsafe_encode64(Digest::MD5.digest(master_label))

      if password.respond_to?(:encode)
        password = password.encode('UTF-8')
      end

      if m = password.match(/\d+/)
        password = '%s%s' % [ m[0], password ] if (m.begin(0) >= length)
      else
        password = '1%s' % password
      end

      password[0,length]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
oplop-2.1.0 lib/oplop/v1.rb