Module: CommonMob::DigestHelper

Defined in:
lib/common_mob/digest.rb

Instance Method Summary collapse

Instance Method Details

#digest_with(digest, string_or_io) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/common_mob/digest.rb', line 17

def digest_with(digest,string_or_io)
  if Pathname === string_or_io
    if string_or_io.exist?
      digest << string_or_io.read
    else
      return nil
    end

  elsif string_or_io.nil?
    return nil
  elsif string_or_io.respond_to?(:read)
    digest << string_or_io.read
  else
    digest << string_or_io
  end

  digest.hexdigest
end

#generate_htpasswd(password) ⇒ Object



36
37
38
39
40
41
# File 'lib/common_mob/digest.rb', line 36

def generate_htpasswd(password)
  pool = [ 'A'..'Z', 'a'..'z', '0'..'9' ].map {|r| r.to_a}.flatten
  salt = [1,2].map {|_| pool[rand(pool.size)] }.join

  password.to_s.crypt(salt)
end

#sha(string_or_io) ⇒ Object



7
8
9
# File 'lib/common_mob/digest.rb', line 7

def sha(string_or_io)
  digest_with Digest::SHA1.new, string_or_io
end

#sha256(string_or_io) ⇒ Object



13
14
15
# File 'lib/common_mob/digest.rb', line 13

def sha256(string_or_io)
  digest_with Digest::SHA256.new, string_or_io
end

#sha512(string_or_io) ⇒ Object



10
11
12
# File 'lib/common_mob/digest.rb', line 10

def sha512(string_or_io)
  digest_with Digest::SHA512.new, string_or_io
end