Class: HTAuth::Algorithm

Inherits:
Object
  • Object
show all
Defined in:
lib/htauth/algorithm.rb

Overview

base class all the Passwd algorithms derive from

Direct Known Subclasses

Crypt, Md5, Plaintext, Sha1

Constant Summary collapse

SALT_CHARS =
(%w[ . / ] + ("0".."9").to_a + ('A'..'Z').to_a + ('a'..'z').to_a).freeze
DEFAULT =
"md5"
EXISTING =
"existing"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.algorithm_from_name(a_name, params = {}) ⇒ Object



11
12
13
14
# File 'lib/htauth/algorithm.rb', line 11

def algorithm_from_name(a_name, params = {})
  raise InvalidAlgorithmError, "`#{a_name}' is an invalid encryption algorithm, use one of #{sub_klasses.keys.join(', ')}" unless sub_klasses[a_name.downcase]
  sub_klasses[a_name.downcase].new(params)
end

.algorithms_from_field(password_field) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/htauth/algorithm.rb', line 16

def algorithms_from_field(password_field)
  matches = []

  if password_field.index(sub_klasses['sha1'].new.prefix) then
    matches << sub_klasses['sha1'].new
  elsif password_field.index(sub_klasses['md5'].new.prefix) then
    p = password_field.split("$")
    matches << sub_klasses['md5'].new( :salt => p[2] )
  else
    matches << sub_klasses['plaintext'].new
    matches << sub_klasses['crypt'].new( :salt => password_field[0,2] )
  end

  return matches
end

.inherited(sub_klass) ⇒ Object



32
33
34
35
# File 'lib/htauth/algorithm.rb', line 32

def inherited(sub_klass)
  k = sub_klass.name.split("::").last.downcase
  sub_klasses[k] = sub_klass
end

.sub_klassesObject



37
38
39
# File 'lib/htauth/algorithm.rb', line 37

def sub_klasses
  @sub_klasses ||= {}
end

Instance Method Details

#encode(password) ⇒ Object



43
# File 'lib/htauth/algorithm.rb', line 43

def encode(password) ; end

#gen_saltObject

8 bytes of random items from SALT_CHARS



46
47
48
49
50
# File 'lib/htauth/algorithm.rb', line 46

def gen_salt
  chars = []
  8.times { chars << SALT_CHARS[rand(SALT_CHARS.size)] }
  chars.join('')     
end

#prefixObject



42
# File 'lib/htauth/algorithm.rb', line 42

def prefix ; end

#to_64(number, rounds) ⇒ Object

this is not the Base64 encoding, this is the to64() method from apr



53
54
55
56
57
58
59
60
# File 'lib/htauth/algorithm.rb', line 53

def to_64(number, rounds)
  r = StringIO.new
  rounds.times do |x|
    r.print(SALT_CHARS[number % 64])
    number >>= 6
  end
  return r.string
end