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



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/salty.rb', line 34

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



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

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

.hash(str) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/salty.rb', line 25

def Salty.hash(str)
  salt = generate_salt

  res = salted_hash(str,salt)

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

.hash_fn(str) ⇒ Object



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

def Salty.hash_fn(str)
  sha512 = Digest::SHA2.new(512)
  sha512.hexdigest(str)
end

.salted_hash(str, salt) ⇒ Object



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

def Salty.salted_hash(str,salt)
  res = str
  100.times do
    res = hash_fn(res+salt)
  end
  res
end