Module: Salty

Defined in:
lib/salty.rb

Constant Summary collapse

SALT_LENGTH =
30
ALPHA =
('A'..'Z').to_a + ('a'..'z').to_a + ('0'..'9').to_a

Class Method Summary collapse

Class Method Details

.check(unhashed, hashed) ⇒ Object



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

def Salty.check(unhashed,hashed)
  begin 
    n = unhashed.length
    salt = hashed[n,SALT_LENGTH]
    myhashed = hashed[0...n] + hashed[n+SALT_LENGTH..-1]

    return myhashed == salted_hash(unhashed,salt)
  rescue
    return false
  end
end

.generate_saltObject



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

def Salty.generate_salt
  (1..SALT_LENGTH).map{ALPHA.sample}.join
end

.hash(str) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/salty.rb', line 17

def Salty.hash(str)
  salt = generate_salt

  res = salted_hash(str,salt)

  n = str.length
  res[0...n] + salt + res[n..-1]
end

.salted_hash(str, salt) ⇒ Object



12
13
14
15
# File 'lib/salty.rb', line 12

def Salty.salted_hash(str,salt)
  pbkdf2 = PBKDF2.new(:password => str, :salt => salt, :iterations => 1000)
  pbkdf2.hex_string
end