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

SALT_CHARS =
(%w[ . / ] + ("0".."9").to_a + ('A'..'Z').to_a + ('a'..'z').to_a).freeze
DEFAULT =
( RUBY_PLATFORM !~ /mswin32/ ) ? "crypt" : "md5"
EXISTING =
"existing"

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) algorithm_from_name(a_name, params = {})



13
14
15
16
# File 'lib/htauth/algorithm.rb', line 13

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

+ (Object) algorithms_from_field(password_field)



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

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

+ (Object) inherited(sub_klass)



34
35
36
37
# File 'lib/htauth/algorithm.rb', line 34

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

+ (Object) sub_klasses



39
40
41
# File 'lib/htauth/algorithm.rb', line 39

def sub_klasses
  @sub_klasses ||= {}
end

Instance Method Details

- (Object) encode(password)



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

def encode(password) ; end

- (Object) gen_salt

8 bytes of random items from SALT_CHARS



48
49
50
51
52
# File 'lib/htauth/algorithm.rb', line 48

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

- (Object) prefix



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

def prefix ; end

- (Object) to_64(number, rounds)

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



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

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