class ADAM6050::Password

Usage

The following example creates a password and uses it to validate an encoded string.

password = Password.new 'b6TSkfr6'

password == 'b6TSkfr6' # => false
password == "]\tklTYM\t" # => true

The next example creates a password that will match any string.

password = Password.new

password == 'anything' # => true

Public Class Methods

new(plain = nil) click to toggle source

@raise [FormatError] if the plain text password is longer than 8

characters.

@param plain [String] the plain text version of the password.

# File lib/adam6050/password.rb, line 32
def initialize(plain = nil)
  if plain
    password = obfuscate plain
    define_singleton_method(:==) { |text| password == text }
  else
    define_singleton_method(:==) { |_| true }
  end

  freeze
end

Private Instance Methods

obfuscate(plain) click to toggle source

Transforms a plain text password into an 8 character string recognised by the ADAM-6050. The algorithm, if you can even call it that, used to perform the transformation was found by trial and error.

@raise [FormatError] if the plain text password is longer than 8

characters.

@param plain [String] the plain text version of the password. @return [String] the obfuscated, 8 character password.

# File lib/adam6050/password.rb, line 54
def obfuscate(plain)
  codepoints = plain.codepoints

  raise FormatError if codepoints.length > 8

  password = Array.new(8, 0x0E)
  codepoints.each_with_index do |c, i|
    password[i] = (c & 0x40) | (~c & 0x3F)
  end
  password.pack 'c*'
end