Module: Wristband::Support

Defined in:
lib/wristband/support.rb

Constant Summary collapse

CONSONANTS =
%w( b c d f g h j k l m n p qu r s t v w x z ch cr fr nd ng nk nt ph pr rd sh sl sp st th tr )
VOWELS =
%w( a e i o u y )

Class Method Summary collapse

Class Method Details

.encrypt_with_salt(password, salt, encryption_type = :bcrypt) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/wristband/support.rb', line 15

def encrypt_with_salt(password, salt, encryption_type = :bcrypt)
  return password unless (salt and !salt.empty?)

  case encryption_type
  when :bcrypt
    BCrypt::Engine.hash_secret([password, salt].join, salt)
  when :sha1
    Digest::SHA1.hexdigest([ password, salt ].join)
  end
end

.matches?(attempt, password, salt, encryption_type = :bcrypt) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
# File 'lib/wristband/support.rb', line 39

def matches?(attempt, password, salt, encryption_type = :bcrypt)
  case encryption_type
  when :bcrypt
    BCrypt::Password.new(password) == [attempt, salt].join
  when :sha1
    Digest::SHA1.hexdigest([ attempt, salt ].join) == password
  end
end

.random_salt(length = nil, encryption_type = :bcrypt) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/wristband/support.rb', line 27

def random_salt(length = nil, encryption_type = :bcrypt)
  salt = case encryption_type
  when :bcrypt
    BCrypt::Engine.generate_salt
  when :sha1
    Digest::SHA1.hexdigest([ rand, rand, random_string(64), rand, rand ].join)
  end

  length ? salt[0, length] : salt
end

.random_string(length = 8) ⇒ Object



8
9
10
11
12
# File 'lib/wristband/support.rb', line 8

def random_string(length = 8)
  (1 .. length).collect { |n|
    (n % 2 != 0) ? CONSONANTS[rand(CONSONANTS.size)] : VOWELS[rand(VOWELS.size)]
  }.to_s[0, length]
end