Top Level Namespace

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#generate_saltObject



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

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

#hash_fn(str) ⇒ Object



6
7
8
# File 'lib/salty.rb', line 6

def hash_fn(str)
  Digest::SHA1.hexdigest str
end

#salted_hash(str, salt) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/salty.rb', line 14

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

#salty(str) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/salty.rb', line 22

def salty(str)
  salt = generate_salt

  res = salted_hash(str,salt)

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

#salty_eq(unhashed, hashed) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/salty.rb', line 31

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

  myhashed == salted_hash(unhashed,salt)
end