Class: Phpass

Inherits:
Object
  • Object
show all
Defined in:
lib/phpass.rb,
lib/phpass/md5.rb

Defined Under Namespace

Classes: Md5

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(iter = 8) ⇒ Phpass

Returns a new instance of Phpass.



8
9
10
11
# File 'lib/phpass.rb', line 8

def initialize(iter=8)
  iter = 8 unless (8..30).include?(iter)
  @iter = iter
end

Class Method Details

.random_bytes(length) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/phpass.rb', line 23

def self.random_bytes(length)
  out = ''
  if File.readable?('/dev/urandom')
    out = File.read('/dev/urandom', length)
  end

  if(out.length < length)
    random_state = '%s%s' % [Time.now.to_f, $$]
    out = ''
    while out.length < length
      rnd = Digest::MD5.hexdigest(Time.now.to_f.to_s + random_state)
      out << Digest::MD5.digest(rnd)
    end
    out = out[0..length]
  end
  out
end

Instance Method Details

#check(pw, hash) ⇒ Object



18
19
20
21
# File 'lib/phpass.rb', line 18

def check(pw, hash)
  hasher = Md5.new(@iter)
  hasher.check(pw, hash)
end

#hash(pw, alg = :md5) ⇒ Object



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

def hash(pw, alg=:md5)
  hasher = Md5.new(@iter)
  hasher.hash(pw)
end